From 4421b8eb18523f74d098f6ec0817f99bd73927a4 Mon Sep 17 00:00:00 2001 From: Jason Raveling Date: Fri, 31 Oct 2025 09:34:51 -0500 Subject: [PATCH 1/7] refactor(functions): Adds wp_skip_all() and wp_on_site() functions that extend wp command. --- empty-site-all.sh | 2 +- post-meta-update.sh | 4 ++-- replace-gravity-form-contact.sh | 13 +++++-------- source/README.md | 1 + source/wp-cli-override.sh | 15 +++++++++++++++ 5 files changed, 24 insertions(+), 11 deletions(-) create mode 100644 source/README.md create mode 100644 source/wp-cli-override.sh diff --git a/empty-site-all.sh b/empty-site-all.sh index a660c27..6a8fa61 100755 --- a/empty-site-all.sh +++ b/empty-site-all.sh @@ -125,7 +125,7 @@ for site_url in $(wp site list --spam=1 --field=url); do done; -# wp-cli with extra flags and targetting one site. +# wp-cli with extra flags and targeting one site. wp_on_site () { wp --skip-themes --url="${site_url}" "$@" } diff --git a/post-meta-update.sh b/post-meta-update.sh index 5ed9a8e..de9081e 100755 --- a/post-meta-update.sh +++ b/post-meta-update.sh @@ -15,7 +15,7 @@ old_value2='templateincludes-default.php'; new_value2='templateincludes-default-classic-editor.php'; echo; -for site_id in $(wp site list --allow-root --deleted=0 --archived=0 --spam=0 --field=blog_id); do +for site_id in $(wp site list --deleted=0 --archived=0 --spam=0 --field=blog_id); do echo "Replacing on site ID ${site_id}"; echo "meta_key: ${meta_key}"; echo "meta_value: ${old_value1} -> ${new_value1}"; @@ -29,7 +29,7 @@ UPDATE ${postmeta_table} SET meta_value = '${new_value1}' WHERE meta_key = '${me UPDATE ${postmeta_table} SET meta_value = '${new_value2}' WHERE meta_key = '${meta_key}' AND meta_value = '${old_value2}'; "; - wp db query --allow-root --skip-themes --skip-plugins "${query}"; + wp db query --skip-themes --skip-plugins "${query}"; echo "Done"; echo; diff --git a/replace-gravity-form-contact.sh b/replace-gravity-form-contact.sh index 7e03e93..a9f1e45 100755 --- a/replace-gravity-form-contact.sh +++ b/replace-gravity-form-contact.sh @@ -3,19 +3,16 @@ # This script loops through each network site and replace an email contact with another for Gravity # Forms notifications. +source 'source/wp-cli-override.sh'; + old_contacts=("some-person@somesite.com"); new_contact="different-person@anothersite.com"; -# wp-cli with extra flags and targeting one site. -wp_on_site () { - wp --skip-themes --allow-root --url="${site_url}" "$@" -} - # Loop over every network site. -for site_id in $(wp site list --allow-root --deleted=0 --archived=0 --spam=0 --field=site_id); do +for site_id in $(wp_skip_all site list --deleted=0 --archived=0 --spam=0 --field=site_id); do echo - echo "Replacing Gravity Forms contact on site ID ${site_id}"; + echo "Replacing Gravity Forms contact on site ID: ${site_id}"; # Loop $old_contacts. x=1; @@ -36,7 +33,7 @@ WHERE echo $query; - wp db query "${query}" --allow-root --skip-themes --skip-plugins + wp_skip_all db query "${query}" done; echo; diff --git a/source/README.md b/source/README.md new file mode 100644 index 0000000..05fcfe7 --- /dev/null +++ b/source/README.md @@ -0,0 +1 @@ +Files here are included using `source [filename]` in other scripts. These scripts are used globally. diff --git a/source/wp-cli-override.sh b/source/wp-cli-override.sh new file mode 100644 index 0000000..af0c910 --- /dev/null +++ b/source/wp-cli-override.sh @@ -0,0 +1,15 @@ +# Overrides the default wp command for more readable scripts and more flexible modification of +# the scripts usage of the wp command. + +# Usage of the wp command on the main site. Skips themes and plugins. +wp_skip_all () { + wp --skip-themes --skip-plugins "$@" +} + + +# Usage of the wp command for a specific site. +# +# It expects $site_url to be set. This is useful in a for loop of every site. +wp_on_site () { + wp --skip-themes --url="${site_url}" "$@" +} From 531720c6c6b7e1ee5c8bfe1fe0115b55f824ad66 Mon Sep 17 00:00:00 2001 From: Jason Raveling Date: Fri, 31 Oct 2025 10:30:41 -0500 Subject: [PATCH 2/7] feat(counting): Adds counting scripts for plugins and themes. --- count-plugin.sh | 78 +++++++++++++++++++++++++++++++++++++++++++++++++ count-themes.sh | 72 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 150 insertions(+) create mode 100644 count-plugin.sh create mode 100644 count-themes.sh diff --git a/count-plugin.sh b/count-plugin.sh new file mode 100644 index 0000000..d0f4cb5 --- /dev/null +++ b/count-plugin.sh @@ -0,0 +1,78 @@ +#!/bin/sh + +# Set the exact slug of the plugin you want to check +PLUGIN_SLUG='classic-editor' + +# Requires 'bc' for floating-point percentage calculation +if ! command -v bc &> /dev/null; then + echo "Error: 'bc' (basic calculator) is not installed." + echo "Please install 'bc' to run this script." + exit 1 +fi + +# Initialize counters +active_count=0 +failed_sites=0 + +# Get all site URLs into a bash array (ignores 'Loading...' line) +mapfile -t sites < <(wp site list --skip-themes --skip-plugins --field=url --quiet) + +# Get total site count +total_sites=${#sites[@]} + +if [[ $total_sites -eq 0 ]]; then + echo "No sites found." + exit 0 +fi + +echo "Auditing '$PLUGIN_SLUG' plugin status across $total_sites sites..." +echo "---" + +# Loop through each site and check plugin status. +for s in "${sites[@]}"; do + + # Check if the plugin is active. Suppress errors for archived/deleted sites. + # Exits with 0 if active, 1 if inactive/not-installed. + wp plugin is-active "$PLUGIN_SLUG" --skip-themes --skip-plugins --url="$s" >/dev/null 2>&1 + plugin_status=$? + + if [[ $plugin_status -eq 0 ]]; then + # Status 0: Plugin is ACTIVE + ((active_count++)) + echo "Site: $s | ACTIVE" + elif [[ $plugin_status -eq 1 ]]; then + # Status 1: Plugin is INACTIVE or not installed + echo "Site: $s | INACTIVE" + else + # Any other status: WP-CLI command failed + echo "Site: $s | FAILED to get status (site may be archived or deleted)" + ((failed_sites++)) + fi +done + +# Calculate results. Base percentages only on sites we could successfully check. +total_checked=$((total_sites - failed_sites)) +inactive_count=$((total_checked - active_count)) + +if [[ $total_checked -gt 0 ]]; then + # Use 'bc' for floating-point percentage calculation + active_perc=$(echo "scale=2; ($active_count / $total_checked) * 100" | bc) + inactive_perc=$(echo "scale=2; ($inactive_count / $total_checked) * 100" | bc) +else + active_perc="0.00" + inactive_perc="0.00" +fi + +echo +echo "##################################################" +echo "📊 Plugin Usage Report for: $PLUGIN_SLUG" +echo "##################################################" + +# Print the report +printf "Active: %-5s sites (%s%%)\n" "$active_count" "$active_perc" +printf "Inactive: %-5s sites (%s%%)\n" "$inactive_count" "$inactive_perc" +echo "---" +printf "Total Sites Checked: %s\n" "$total_checked" +if [[ $failed_sites -gt 0 ]]; then + printf "Sites Failed (skipped): %s\n" "$failed_sites" +fi diff --git a/count-themes.sh b/count-themes.sh new file mode 100644 index 0000000..c4a4b39 --- /dev/null +++ b/count-themes.sh @@ -0,0 +1,72 @@ +#!/bin/sh + +# This script audits all sites in a WordPress Multisite network and provides a count and percentage +# for every active theme. + +# Requires 'bc' for floating-point percentage calculation. +if ! command -v bc &> /dev/null; then + echo "Error: 'bc' (basic calculator) is not installed." + echo "Please install 'bc' to run this script." + exit 1 +fi + +# Declare an associative array to hold theme counts +declare -A theme_counts + +# Get all site URLs into a bash array (ignores 'Loading...' line) +mapfile -t sites < <(wp site list --skip-themes --skip-plugins --field=url --deleted=0 --archived=0 --quiet) + +# Get total site count +total_sites=${#sites[@]} +failed_sites=0 + +if [[ $total_sites -eq 0 ]]; then + echo "No sites found." + exit 0 +fi + +echo "Auditing $total_sites sites..." +echo "---" + +# Loop through each site and get its active theme +for s in "${sites[@]}"; do + # Get the slug of the active theme. + # '2>/dev/null' suppresses errors from archived/deleted sites. + active_theme=$(wp theme list --skip-themes --skip-plugins --status=active --field=name --url="$s" 2>/dev/null) + + # Check if the command was successful and returned a theme name + if [[ -n "$active_theme" ]]; then + # Increment the count for this theme in the array + ((theme_counts[$active_theme]++)) + echo "Site: $s | Theme: $active_theme" + else + echo "Site: $s | FAILED to get theme (site may be archived or deleted)" + ((failed_sites++)) + fi +done + +echo +echo "########################################" +echo "📊 Theme Usage Report" +echo "########################################" + +# Loop through the counted themes and print the report + +# Get all unique theme names (the array keys) and sort them +sorted_themes=($(printf "%s\n" "${!theme_counts[@]}" | sort)) + +for theme in "${sorted_themes[@]}"; do + count=${theme_counts[$theme]} + + # Use 'bc' for floating-point percentage calculation + percentage=$(echo "scale=2; ($count / $total_sites) * 100" | bc) + + # Use 'printf' for clean, aligned columns + printf "Theme: %-30s | Count: %-5s | Percentage: %s%%\n" "$theme" "$count" "$percentage" +done + +echo "---" +printf "Total Sites Checked: %-5s\n" "$total_sites" +if [[ $failed_sites -gt 0 ]]; then + printf "Sites Failed: %-5s (not included in percentage)\n" "$failed_sites" +fi From d782242cd9e6178fca210a2b890891f6ecb1ec96 Mon Sep 17 00:00:00 2001 From: Jason Raveling Date: Fri, 31 Oct 2025 15:02:05 -0500 Subject: [PATCH 3/7] feat(counting): Adds wp function override to counting scripts. --- count-plugin.sh | 12 +++++++----- count-themes.sh | 8 +++++--- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/count-plugin.sh b/count-plugin.sh index d0f4cb5..ba69050 100644 --- a/count-plugin.sh +++ b/count-plugin.sh @@ -1,7 +1,9 @@ #!/bin/sh # Set the exact slug of the plugin you want to check -PLUGIN_SLUG='classic-editor' +plugin_slug='classic-editor'; + +source 'source/wp-cli-override.sh'; # Requires 'bc' for floating-point percentage calculation if ! command -v bc &> /dev/null; then @@ -15,7 +17,7 @@ active_count=0 failed_sites=0 # Get all site URLs into a bash array (ignores 'Loading...' line) -mapfile -t sites < <(wp site list --skip-themes --skip-plugins --field=url --quiet) +mapfile -t sites < <(wp_skip_all site list --field=url --quiet) # Get total site count total_sites=${#sites[@]} @@ -25,7 +27,7 @@ if [[ $total_sites -eq 0 ]]; then exit 0 fi -echo "Auditing '$PLUGIN_SLUG' plugin status across $total_sites sites..." +echo "Auditing '$plugin_slug' plugin status across $total_sites sites..." echo "---" # Loop through each site and check plugin status. @@ -33,7 +35,7 @@ for s in "${sites[@]}"; do # Check if the plugin is active. Suppress errors for archived/deleted sites. # Exits with 0 if active, 1 if inactive/not-installed. - wp plugin is-active "$PLUGIN_SLUG" --skip-themes --skip-plugins --url="$s" >/dev/null 2>&1 + wp_skip_all plugin is-active "$plugin_slug" --url="$s" >/dev/null 2>&1 plugin_status=$? if [[ $plugin_status -eq 0 ]]; then @@ -65,7 +67,7 @@ fi echo echo "##################################################" -echo "📊 Plugin Usage Report for: $PLUGIN_SLUG" +echo " Plugin Usage Report for: $plugin_slug" echo "##################################################" # Print the report diff --git a/count-themes.sh b/count-themes.sh index c4a4b39..511a494 100644 --- a/count-themes.sh +++ b/count-themes.sh @@ -3,6 +3,8 @@ # This script audits all sites in a WordPress Multisite network and provides a count and percentage # for every active theme. +source 'source/wp-cli-override.sh'; + # Requires 'bc' for floating-point percentage calculation. if ! command -v bc &> /dev/null; then echo "Error: 'bc' (basic calculator) is not installed." @@ -14,7 +16,7 @@ fi declare -A theme_counts # Get all site URLs into a bash array (ignores 'Loading...' line) -mapfile -t sites < <(wp site list --skip-themes --skip-plugins --field=url --deleted=0 --archived=0 --quiet) +mapfile -t sites < <(wp_skip_all site list --field=url --deleted=0 --archived=0 --quiet) # Get total site count total_sites=${#sites[@]} @@ -32,7 +34,7 @@ echo "---" for s in "${sites[@]}"; do # Get the slug of the active theme. # '2>/dev/null' suppresses errors from archived/deleted sites. - active_theme=$(wp theme list --skip-themes --skip-plugins --status=active --field=name --url="$s" 2>/dev/null) + active_theme=$(wp_skip_all theme list --status=active --field=name --url="$s" 2>/dev/null) # Check if the command was successful and returned a theme name if [[ -n "$active_theme" ]]; then @@ -47,7 +49,7 @@ done echo echo "########################################" -echo "📊 Theme Usage Report" +echo " Theme Usage Report" echo "########################################" # Loop through the counted themes and print the report From 9189c77a29ae1b064a961ac0596556627213e8f1 Mon Sep 17 00:00:00 2001 From: Jason Raveling Date: Mon, 3 Nov 2025 15:35:43 -0600 Subject: [PATCH 4/7] refactor(includes): Adds file to `source` configs and wp-cli overrides. --- README.md | 3 +++ count-plugin.sh | 2 +- count-themes.sh | 2 +- replace-gravity-form-contact.sh | 2 +- search-posts-content.sh | 6 +++--- source/configs.sh | 10 ++++++++++ source/includes.sh | 4 ++++ source/wp-cli-override.sh | 4 ++-- 8 files changed, 25 insertions(+), 8 deletions(-) create mode 100644 source/configs.sh create mode 100644 source/includes.sh diff --git a/README.md b/README.md index 6d6ba90..a811fa8 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,5 @@ # WP-CLI scripts This is a collection of miscellaneous scripts that utilize [WP-CLI](https://wp-cli.org/), the command line interface for WordPress. + +# Usage +Clone this repo and configure the path in `source/configs.sh`. Make the scripts executable as needed (i.e. `chmod +x wpcli-scripts/*.sh`). diff --git a/count-plugin.sh b/count-plugin.sh index ba69050..ebd70e6 100644 --- a/count-plugin.sh +++ b/count-plugin.sh @@ -3,7 +3,7 @@ # Set the exact slug of the plugin you want to check plugin_slug='classic-editor'; -source 'source/wp-cli-override.sh'; +source 'source/includes.sh'; # Requires 'bc' for floating-point percentage calculation if ! command -v bc &> /dev/null; then diff --git a/count-themes.sh b/count-themes.sh index 511a494..c81c9c7 100644 --- a/count-themes.sh +++ b/count-themes.sh @@ -3,7 +3,7 @@ # This script audits all sites in a WordPress Multisite network and provides a count and percentage # for every active theme. -source 'source/wp-cli-override.sh'; +source 'source/includes.sh'; # Requires 'bc' for floating-point percentage calculation. if ! command -v bc &> /dev/null; then diff --git a/replace-gravity-form-contact.sh b/replace-gravity-form-contact.sh index a9f1e45..c5cf03d 100755 --- a/replace-gravity-form-contact.sh +++ b/replace-gravity-form-contact.sh @@ -3,7 +3,7 @@ # This script loops through each network site and replace an email contact with another for Gravity # Forms notifications. -source 'source/wp-cli-override.sh'; +source 'source/includes.sh'; old_contacts=("some-person@somesite.com"); new_contact="different-person@anothersite.com"; diff --git a/search-posts-content.sh b/search-posts-content.sh index 8a8a879..d35589f 100755 --- a/search-posts-content.sh +++ b/search-posts-content.sh @@ -19,7 +19,7 @@ echo; echo 'Beginning search...'; # Get every network site. -all_site_ids=$(wp --allow-root --skip-themes --skip-plugins site list --field="blog_id" --archived=0); +all_site_ids=$(wp_skip_all site list --field="blog_id"); # Loop over every network site. for site_id in $all_site_ids; do @@ -32,10 +32,10 @@ for site_id in $all_site_ids; do echo "Results for Site ID ${site_id}" >> $output_file; # Using WP built in search. But "My favorite book" would turn up pages with "my" or "favorite" or "book". - #wp post list --skip-themes --skip-plugins --post_type='page' --search="${term}" --fields="${fields}" --format=csv --url="${site}" | awk 'NR>1' >> $output_file; + #wp_skip_all post list --post_type='page' --search="${term}" --fields="${fields}" --format=csv --url="${site}" | awk 'NR>1' >> $output_file; # Search for multi term phrases. Cases sensitive. - wp db query --skip-themes --skip-plugins --skip-column-names "SELECT ${fields} FROM wp_${site_id}_posts WHERE post_content REGEXP '${search_regex}' AND post_status='publish';" >> $output_file; + wp_skip_all db query --skip-column-names "SELECT ${fields} FROM wp_${site_id}_posts WHERE post_content REGEXP '${search_regex}' AND post_status='publish';" >> $output_file; done; diff --git a/source/configs.sh b/source/configs.sh new file mode 100644 index 0000000..89f2268 --- /dev/null +++ b/source/configs.sh @@ -0,0 +1,10 @@ +# Global configs for these scripts. + +config=( + + # The path to the root of your WordPress installation. + # + # This is set so that these scripts can live outside of the WordPress installation. + [wp_path]='/var/www/public_html/' + +); diff --git a/source/includes.sh b/source/includes.sh new file mode 100644 index 0000000..689363a --- /dev/null +++ b/source/includes.sh @@ -0,0 +1,4 @@ +# This file is source'd / included with every script. + +source 'configs.sh'; +source 'wp-cli-overrides.sh'; diff --git a/source/wp-cli-override.sh b/source/wp-cli-override.sh index af0c910..826b1dd 100644 --- a/source/wp-cli-override.sh +++ b/source/wp-cli-override.sh @@ -3,7 +3,7 @@ # Usage of the wp command on the main site. Skips themes and plugins. wp_skip_all () { - wp --skip-themes --skip-plugins "$@" + wp --skip-themes --skip-plugins --path=$config[wp_path] "$@" } @@ -11,5 +11,5 @@ wp_skip_all () { # # It expects $site_url to be set. This is useful in a for loop of every site. wp_on_site () { - wp --skip-themes --url="${site_url}" "$@" + wp --skip-themes --path=$config[wp_path] --url="${site_url}" "$@" } From 6930032804983aa0f070397867607bbb8296e3dc Mon Sep 17 00:00:00 2001 From: Jason Raveling Date: Mon, 3 Nov 2025 15:41:50 -0600 Subject: [PATCH 5/7] Adds .gitignore. --- .gitignore | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c23eb8b --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.watchsyncrc* +watchsync.sh From 2c27e4b321f1bc180bcf1981bf772f60e8d88368 Mon Sep 17 00:00:00 2001 From: Jason Raveling Date: Mon, 3 Nov 2025 16:34:25 -0600 Subject: [PATCH 6/7] Gets configs and includes as source working. --- .gitignore | 2 +- count-plugin.sh | 0 count-themes.sh | 0 delete-transient-every-network-site.sh | 14 +++++++------- delete-user.sh | 16 +++++++++------- source/configs.sh | 2 +- source/includes.sh | 12 ++++++++++-- .../{wp-cli-override.sh => wp-cli-overrides.sh} | 4 ++-- 8 files changed, 30 insertions(+), 20 deletions(-) mode change 100644 => 100755 count-plugin.sh mode change 100644 => 100755 count-themes.sh rename source/{wp-cli-override.sh => wp-cli-overrides.sh} (73%) diff --git a/.gitignore b/.gitignore index c23eb8b..c1ee9c6 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,2 @@ .watchsyncrc* -watchsync.sh +watch-sync.sh diff --git a/count-plugin.sh b/count-plugin.sh old mode 100644 new mode 100755 diff --git a/count-themes.sh b/count-themes.sh old mode 100644 new mode 100755 diff --git a/delete-transient-every-network-site.sh b/delete-transient-every-network-site.sh index 191a119..61d25a9 100755 --- a/delete-transient-every-network-site.sh +++ b/delete-transient-every-network-site.sh @@ -1,14 +1,16 @@ #!/bin/sh # Loop through each network site and delete a transient. - +# # The transient to search for (can use wild cards *). transient_search_term='*events_*'; +source 'source/includes.sh'; + echo; -for site_url in $(wp site list --allow-root --skip-themes --skip-plugins --deleted=0 --archived=0 --spam=0 --field=url); do +for site_url in $(wp_skip_all site list --field=url); do - found_transients=$(wp transient list --allow-root --skip-themes --skip-plugins --format=csv --fields=name --search=$transient_search_term --url=$site_url | awk 'NR>1 {print}' + found_transients=$(wp_skip_all transient list --format=csv --fields=name --search=$transient_search_term --url=$site_url | awk 'NR>1 {print}' ); echo '################################################' @@ -18,13 +20,11 @@ for site_url in $(wp site list --allow-root --skip-themes --skip-plugins --delet # Check if it is NOT empty. if [[ -z $found_transients ]]; then - echo 'none'; + echo ' none'; else - echo $found_transients; + echo " $found_transients"; fi - echo; - for transient in $found_transients; do wp transient delete --allow-root --skip-themes --skip-plugins --url=$site_url $transient; diff --git a/delete-user.sh b/delete-user.sh index 53be690..8d365c8 100755 --- a/delete-user.sh +++ b/delete-user.sh @@ -3,6 +3,8 @@ # This interactive script deletes a given user by their ID and reattributes their content to another # user based on the provided ID. +source 'source/includes.sh'; + echo; echo 'You are about to delete a user and attribute any content they may have to another use.'; echo; @@ -28,22 +30,22 @@ done; # Prompt for the site ID to delete the user from. if [[ 999 = $site_id ]]; then - sites=$(wp --allow-root --skip-themes --skip-plugins site list --field="url" --archived=0); + sites=$(wp_skip_all site list --field="url" --archived=0); sites_name='All sites in the WP network'; else - sites=$(wp --allow-root --skip-themes --skip-plugins site list --field="url" --site__in=${site_id}); + sites=$(wp_skip_all site list --field="url" --site__in=${site_id}); sites_name="$sites"; fi echo; # Add human readable info for the user to be deleted. -user_name_delete=$(wp --allow-root --skip-themes --skip-plugins user get ${user_id_delete} --field="display_name"); -# user_login_delete=$(wp --allow-root --skip-themes --skip-plugins user get ${user_id_delete} --field="user_login"); +user_name_delete=$(wp_skip_all user get ${user_id_delete} --field="display_name"); +# user_login_delete=$(wp_skip_all user get ${user_id_delete} --field="user_login"); # Add human readable info for the user to get attribution of content. -user_name_attribute=$(wp --allow-root --skip-themes --skip-plugins user get ${user_id_attribute} --field="display_name"); -# user_login_attribute=$(wp --allow-root --skip-themes --skip-plugins user get ${user_id_attribute} --field="user_login"); +user_name_attribute=$(wp_skip_all user get ${user_id_attribute} --field="display_name"); +# user_login_attribute=$(wp_skip_all user get ${user_id_attribute} --field="user_login"); # Confirm before moving on. while [[ -z "$confirmed" ]]; do @@ -72,5 +74,5 @@ esac # you reattribute content. echo 'Removing the user...'; for site in $sites; do - wp --allow-root --skip-themes --skip-plugins user delete $user_id_delete --reassign="${user_id_attribute}" --url="${site}" ; + wp_skip_all user delete $user_id_delete --reassign="${user_id_attribute}" --url="${site}" ; done; diff --git a/source/configs.sh b/source/configs.sh index 89f2268..299ba03 100644 --- a/source/configs.sh +++ b/source/configs.sh @@ -1,6 +1,6 @@ # Global configs for these scripts. -config=( +declare -A config=( # The path to the root of your WordPress installation. # diff --git a/source/includes.sh b/source/includes.sh index 689363a..bae4b6f 100644 --- a/source/includes.sh +++ b/source/includes.sh @@ -1,4 +1,12 @@ # This file is source'd / included with every script. -source 'configs.sh'; -source 'wp-cli-overrides.sh'; +# The path to this file. +# path_to_this_file=$(realpath "${BASH_SOURCE[0]}"); +directory_path=$(dirname "$(realpath "${BASH_SOURCE[0]}")") + +source "$directory_path/configs.sh"; +source "$directory_path/wp-cli-overrides.sh"; + +echo '########################################################################'; +echo "Working with the site at ${config[wp_path]}"; +echo '########################################################################'; diff --git a/source/wp-cli-override.sh b/source/wp-cli-overrides.sh similarity index 73% rename from source/wp-cli-override.sh rename to source/wp-cli-overrides.sh index 826b1dd..effbf32 100644 --- a/source/wp-cli-override.sh +++ b/source/wp-cli-overrides.sh @@ -3,7 +3,7 @@ # Usage of the wp command on the main site. Skips themes and plugins. wp_skip_all () { - wp --skip-themes --skip-plugins --path=$config[wp_path] "$@" + wp --skip-themes --skip-plugins --path="${config[wp_path]}" "$@" } @@ -11,5 +11,5 @@ wp_skip_all () { # # It expects $site_url to be set. This is useful in a for loop of every site. wp_on_site () { - wp --skip-themes --path=$config[wp_path] --url="${site_url}" "$@" + wp --skip-themes --path="${config[wp_path]}" --url="${site_url}" "$@" } From d49fab39d1d0f63cf32b71e3267567008e9812f0 Mon Sep 17 00:00:00 2001 From: Jason Raveling Date: Tue, 4 Nov 2025 14:55:36 -0600 Subject: [PATCH 7/7] Adds linting. Finalizes source'd files. Replaces shebang. --- .gitignore | 1 + .shellcheckrc | 1 + README.md | 2 +- count-plugin.sh | 2 +- count-themes.sh | 2 +- delete-transient-every-network-site.sh | 2 +- delete-user.sh | 2 +- empty-site-all.sh | 64 ++++++++++++------------- empty-site-single.sh | 26 ++++------ plugin-activate-based-on-theme.sh | 21 ++++---- post-meta-update.sh | 8 ++-- replace-gravity-form-contact.sh | 23 +++++---- search-posts-content.sh | 6 ++- source/{configs.sh => config-sample.sh} | 7 +++ source/config.sh | 15 ++++++ source/includes.sh | 17 ++++++- source/wp-cli-overrides.sh | 3 ++ theme-used-change-theme.sh | 22 +++++---- theme-used-generate-sites-list.sh | 24 ++++++---- update-gf-recaptcha.sh | 18 +++---- 20 files changed, 162 insertions(+), 104 deletions(-) create mode 100644 .shellcheckrc rename source/{configs.sh => config-sample.sh} (51%) create mode 100644 source/config.sh diff --git a/.gitignore b/.gitignore index c1ee9c6..1eb58e4 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ .watchsyncrc* watch-sync.sh +config.sh diff --git a/.shellcheckrc b/.shellcheckrc new file mode 100644 index 0000000..8226afb --- /dev/null +++ b/.shellcheckrc @@ -0,0 +1 @@ +external-sources=true diff --git a/README.md b/README.md index a811fa8..8bea843 100644 --- a/README.md +++ b/README.md @@ -2,4 +2,4 @@ This is a collection of miscellaneous scripts that utilize [WP-CLI](https://wp-cli.org/), the command line interface for WordPress. # Usage -Clone this repo and configure the path in `source/configs.sh`. Make the scripts executable as needed (i.e. `chmod +x wpcli-scripts/*.sh`). +Clone this repo and configure the path in `source/config.sh`. Make the scripts executable as needed (i.e. `chmod +x wpcli-scripts/*.sh`). diff --git a/count-plugin.sh b/count-plugin.sh index ebd70e6..3566dfd 100755 --- a/count-plugin.sh +++ b/count-plugin.sh @@ -1,4 +1,4 @@ -#!/bin/sh +#!/bin/env bash # Set the exact slug of the plugin you want to check plugin_slug='classic-editor'; diff --git a/count-themes.sh b/count-themes.sh index c81c9c7..8edcb7a 100755 --- a/count-themes.sh +++ b/count-themes.sh @@ -1,4 +1,4 @@ -#!/bin/sh +#!/bin/env bash # This script audits all sites in a WordPress Multisite network and provides a count and percentage # for every active theme. diff --git a/delete-transient-every-network-site.sh b/delete-transient-every-network-site.sh index 61d25a9..1eb3a98 100755 --- a/delete-transient-every-network-site.sh +++ b/delete-transient-every-network-site.sh @@ -1,4 +1,4 @@ -#!/bin/sh +#!/bin/env bash # Loop through each network site and delete a transient. # diff --git a/delete-user.sh b/delete-user.sh index 8d365c8..05a30da 100755 --- a/delete-user.sh +++ b/delete-user.sh @@ -1,4 +1,4 @@ -#!/bin/sh +#!/bin/env bash # This interactive script deletes a given user by their ID and reattributes their content to another # user based on the provided ID. diff --git a/empty-site-all.sh b/empty-site-all.sh index 6a8fa61..28b3a47 100755 --- a/empty-site-all.sh +++ b/empty-site-all.sh @@ -1,9 +1,11 @@ -#!/bin/bash +#!/bin/env bash # A script for emptying each network site and leaving just the network structure. # # DO NOT RUN THIS AS ROOT since some commands could potentially delete unwanted data if run as root. +source 'source/includes.sh'; + # Script version version='1.0.3'; @@ -12,7 +14,7 @@ version='1.0.3'; production_dir='www'; # The theme name/slug to activate. -theme_to_use='my-theme-name'; +theme_to_use='some-theme'; # Gravity forms license key. Leave empty if you do not use it. gf_license_key="" @@ -26,7 +28,7 @@ if [[ $current_dir =~ "$production_dir" ]]; then fi # Make sure that WP-CLI is installed. -if [[ "$(which wp 2>/dev/null)" == "" ]]; then +if [[ "$(which wp_skip_all 2>/dev/null)" == "" ]]; then echo echo "The WP-CLI command line interface could not be found. It is required for this script to run." echo @@ -52,13 +54,13 @@ echo read -r -p "Continue? [y/N] " user_consent echo -# Pause before continuing to be sure the user understands this is desructive. +# Pause before continuing to be sure the user understands this is destructive. case $user_consent in [yY]) echo "You have agreed to destroy the entire site. Continuing." ;; *) - echo "You have chosen not to contiune. Exiting." + echo "You have chosen not to continue. Exiting." exit; ;; esac @@ -71,7 +73,7 @@ esac echo $SECONDS > /dev/null # start the timer echo -wp maintenance-mode activate +wp_skip_all maintenance-mode activate # Site options to retain when wiping a site. site_options_to_retain=( @@ -82,7 +84,7 @@ site_options_to_retain=( "admin_email" ); -# Build the string of --exclude args for `wp option list` +# Build the string of --exclude args for `wp_skip_all option list` site_options_exclude_regex=$(printf "^%s$|" "${site_options_to_retain[@]}") site_options_exclude_regex=${site_options_exclude_regex::-1} # remove the last pipe @@ -91,7 +93,7 @@ site_options_exclude_regex=${site_options_exclude_regex::-1} # remove the last p # Destroy on a network level. ################################################################################################### echo -echo "Starting sitewide destruction..." +echo "Starting multisite destruction..." echo ################################################################################################### @@ -102,37 +104,31 @@ echo "Destroying and removing archived and deleted sites." delete_site() { - site_id=$(wp site list | awk -v site_url=$site_url '{ if( $2 == site_url ) print $1; }') + site_id=$(wp_skip_all site list | awk -v site_url=$site_url '{ if( $2 == site_url ) print $1; }') echo "Wiping site ID: ${site_id}" - wp site delete --yes --skip-themes $site_id + wp_skip_all site delete --yes --skip-themes $site_id } -for site_url in $(wp site list --deleted=1 --field=url); do +for site_url in $(wp_skip_all site list --deleted=1 --field=url); do echo "DELETED site found: ${site_url}" delete_site; done; -for site_url in $(wp site list --archived=1 --field=url); do +for site_url in $(wp_skip_all site list --archived=1 --field=url); do echo "ARCHIVED site found: ${site_url}" delete_site; done; -for site_url in $(wp site list --spam=1 --field=url); do +for site_url in $(wp_skip_all site list --spam=1 --field=url); do echo "SPAM site found: ${site_url}" delete_site; done; - -# wp-cli with extra flags and targeting one site. -wp_on_site () { - wp --skip-themes --url="${site_url}" "$@" -} - echo echo 'Emptying active sites.' -for site_url in $(wp site list --deleted=0 --archived=0 --spam=0 --field=url); do +for site_url in $(wp_skip_all site list --deleted=0 --archived=0 --spam=0 --field=url); do # Get the site nice name. blogname=$(wp_on_site option get blogname) @@ -208,18 +204,18 @@ rm -rf "${current_dir}/wp-content/uploads/*" 2>/dev/null wp_on_site transient delete --all # Disable caching -wp update option wp_cache_enabled "0" +wp_skip_all update option wp_cache_enabled "0" # For some reason network meta also gets changed. ms_files_rewrite # should be set to 0 otherwise WP reverts back to uploads file # structure from WP versions pre-3.5. -wp network meta set 1 ms_files_rewriting 0 +wp_skip_all network meta set 1 ms_files_rewriting 0 -wp network meta set 1 subdomain_install 0 # We are using sub dirs -wp network meta set 1 global_terms_enabled 0 +wp_skip_all network meta set 1 subdomain_install 0 # We are using sub dirs +wp_skip_all network meta set 1 global_terms_enabled 0 # wp_rg_* are deprecated tables https://docs.gravityforms.com/deprecated-database-tables/ -for table in $(wp db tables wp_gf_* wp_*_gf_* wp_rg_* wp_*_rg_* --all-tables); do +for table in $(wp_skip_all db tables wp_gf_* wp_*_gf_* wp_rg_* wp_*_rg_* --all-tables); do # Create a comma separated list of tables to be dropped for a MySQL qeury tables_to_remove_sql="${tables_to_remove_sql}, ${table}" done; @@ -227,21 +223,21 @@ done; tables_to_remove_sql=${tables_to_remove_sql:2} # remove the first two chars (, ) # Install and activate Gravity Forms. -wp plugin deactivate gravityforms --network -wp plugin uninstall gravityforms +wp_skip_all plugin deactivate gravityforms --network +wp_skip_all plugin uninstall gravityforms echo "Deleting tables created by Gravity Forms." -wp db query "DROP TABLE IF EXISTS ${tables_to_remove_sql}" -wp maintenance-mode deactivate +wp_skip_all db query "DROP TABLE IF EXISTS ${tables_to_remove_sql}" +wp_skip_all maintenance-mode deactivate # Check if a Gravity Forms license key was provided. if [[ -n "$gf_license_key" ]]; then - wp plugin install gravityforms - wp plugin install gravityformscli --activate-network || echo -e "\e[1;31mFailed to install Gravity Forms CLI.\e[0m" - wp gf install --skip-themes --key="${gf_license_key}" --activate-network - wp gf setup --skip-themes --force + wp_skip_all plugin install gravityforms + wp_skip_all plugin install gravityformscli --activate-network || echo -e "\e[1;31mFailed to install Gravity Forms CLI.\e[0m" + wp_skip_all gf install --skip-themes --key="${gf_license_key}" --activate-network + wp_skip_all gf setup --skip-themes --force fi -home_page_url=$(wp site list --field="url" | head -n 1) +home_page_url=$(wp_skip_all site list --field="url" | head -n 1) echo "Running wp-cron.php for the first time. (${home_page_url}wp-cron.php)" curl -s "${home_page_url}wp-cron.php" -o /dev/null diff --git a/empty-site-single.sh b/empty-site-single.sh index f419e43..7109988 100755 --- a/empty-site-single.sh +++ b/empty-site-single.sh @@ -1,4 +1,4 @@ -#!/bin/bash +#!/bin/env bash # # A script for emptying an individual network site and leaving an empty site. # @@ -6,6 +6,8 @@ # check if 'cli' === php_sapi_name() and return out of a function. If you see any PHP errors while # running this script, you may want to start there. +source 'source/includes.sh'; + # Script version version='1.0.3'; @@ -30,21 +32,11 @@ if [[ $current_dir =~ "$production_dir" ]]; then echo fi -# Make sure that WP-CLI is installed. -if [[ "$(which wp 2>/dev/null)" == "" ]]; then - echo - echo "The WP-CLI command line interface could not be found. It is required for this script to run." - echo - echo "Visit https://wp-cli.org/ to get WP-CLI." - echo - exit -fi - while [[ -z $site_id ]]; do read -p "Please provide the Site ID: " site_id; done; -site_url="$(wp db query "SELECT option_value FROM wp_${site_id}_options WHERE option_name = 'siteurl'" --skip-themes --skip-plugins --allow-root --skip-column-names)"; +site_url="$(wp_skip_all db query "SELECT option_value FROM wp_${site_id}_options WHERE option_name = 'siteurl'" --skip-column-names)"; site_url="${site_url}/"; if [[ -z $site_url ]]; then @@ -110,13 +102,13 @@ site_options_exclude_regex=${site_options_exclude_regex::-1} # remove the last p # Destroy the individual site. ################################################################################################### -wp --allow-root maintenance-mode activate +wp_skip_all maintenance-mode activate echo "Destroying site ID: ${site_id}" -# wp-cli with extra flags and targetting one site. +# wp-cli with extra flags and targeting one site. wp_on_site () { - wp --allow-root --url="${site_url}" "$@" + wp_skip_all --url="${site_url}" "$@" } echo @@ -193,10 +185,10 @@ wp_on_site rewrite structure '/%year%/%monthnum%/%day%/%postname%/' # Final Cleanup ################################################################################################### -wp --allow-root maintenance-mode deactivate +wp_skip_all maintenance-mode deactivate # Run wp-cron now that all of these changes were made. -home_page_url=$(wp site list --allow-root --field="url" | head -n 1) +home_page_url=$(wp_skip_all site list --field="url" | head -n 1) echo "Running wp-cron.php for the first time. (${home_page_url}wp-cron.php)" curl -s "${home_page_url}wp-cron.php" -o /dev/null diff --git a/plugin-activate-based-on-theme.sh b/plugin-activate-based-on-theme.sh index 55cc330..9eeccaf 100755 --- a/plugin-activate-based-on-theme.sh +++ b/plugin-activate-based-on-theme.sh @@ -1,30 +1,35 @@ -#!/bin/sh +#!/bin/env bash -# Activates or deactivates a plugin based on the currently installed theme. +# Activates or deactivates a plugin based on the currently activated theme. +source 'source/includes.sh'; + +# The plugin slug to activate. +plugin='classic-editor'; + +# The theme to check for when activating the plugin. theme='some-theme'; -plugin='a-plugin'; -wp plugin deactivate --network $plugin; +wp_skip_all plugin deactivate --network $plugin; # Get every network site. -for s in $(wp site list --field=url); do +for s in $(wp_skip_all site list --field=url); do echo; # Run the command but we really want the return status: $? - wp theme is-active $theme --url="$s"; + wp_skip_all theme is-active $theme --url="$s"; theme_is_not_active=$?; echo "Site: $s"; if [[ 1 -eq $theme_is_not_active ]]; then echo "$theme is NOT active."; echo "Activating $plugin plugin..."; - wp plugin activate $plugin --url="$s"; + wp_skip_all plugin activate $plugin --url="$s"; else echo "$theme is active. Skipping plugin activation."; echo "Deactivating $plugin plugin..."; - wp plugin deactivate $plugin --url="$s"; + wp_skip_all plugin deactivate $plugin --url="$s"; fi; done; diff --git a/post-meta-update.sh b/post-meta-update.sh index de9081e..171374d 100755 --- a/post-meta-update.sh +++ b/post-meta-update.sh @@ -1,10 +1,12 @@ -#!/bin/sh +#!/bin/env bash # Loop through each network site and update a single meta key value pair. # The meta key to update values for. meta_key='_wp_page_template'; +source 'source/includes.sh'; + # # The value pairs below will be searched and replaced. # @@ -15,7 +17,7 @@ old_value2='templateincludes-default.php'; new_value2='templateincludes-default-classic-editor.php'; echo; -for site_id in $(wp site list --deleted=0 --archived=0 --spam=0 --field=blog_id); do +for site_id in $(wp_skip_all site list --deleted=0 --archived=0 --spam=0 --field=blog_id); do echo "Replacing on site ID ${site_id}"; echo "meta_key: ${meta_key}"; echo "meta_value: ${old_value1} -> ${new_value1}"; @@ -29,7 +31,7 @@ UPDATE ${postmeta_table} SET meta_value = '${new_value1}' WHERE meta_key = '${me UPDATE ${postmeta_table} SET meta_value = '${new_value2}' WHERE meta_key = '${meta_key}' AND meta_value = '${old_value2}'; "; - wp db query --skip-themes --skip-plugins "${query}"; + wp_skip_all db query "${query}"; echo "Done"; echo; diff --git a/replace-gravity-form-contact.sh b/replace-gravity-form-contact.sh index c5cf03d..01cb132 100755 --- a/replace-gravity-form-contact.sh +++ b/replace-gravity-form-contact.sh @@ -1,15 +1,22 @@ -#!/bin/sh +#!/bin/env bash # This script loops through each network site and replace an email contact with another for Gravity # Forms notifications. source 'source/includes.sh'; -old_contacts=("some-person@somesite.com"); -new_contact="different-person@anothersite.com"; +# One or more contacts to search for. +old_contacts=( + 'some-person@somesite.com' + 'another-person@somesite.com' +); + +# A contact to replace old contacts with. +new_contact='different-person@anothersite.com'; + # Loop over every network site. -for site_id in $(wp_skip_all site list --deleted=0 --archived=0 --spam=0 --field=site_id); do +for site_id in $(wp_skip_all site list --deleted=0 --archived=0 --spam=0 --field=blog_id); do echo echo "Replacing Gravity Forms contact on site ID: ${site_id}"; @@ -20,6 +27,8 @@ for site_id in $(wp_skip_all site list --deleted=0 --archived=0 --spam=0 --field echo "${old_contact} >>> ${new_contact}"; +# We want quotes treated literally. +# shellcheck disable=SC2089 query=" UPDATE wp_${site_id}_gf_form_meta SET @@ -31,11 +40,9 @@ SET WHERE notifications IS NOT NULL;"; - echo $query; - wp_skip_all db query "${query}" - done; + wp_skip_all db query "$query" - echo; + done; done; diff --git a/search-posts-content.sh b/search-posts-content.sh index d35589f..28eaae1 100755 --- a/search-posts-content.sh +++ b/search-posts-content.sh @@ -1,10 +1,12 @@ -#!/bin/bash +#!/bin/env bash # Search post content for every post on every network site. Return as # a CSV with ID, post_title, and url. +source 'source/includes.sh'; + # The text to search for. -search_regex='find this in content'; +search_regex='some text'; # A comma separated list of fields to return. fields='ID,post_title,guid'; diff --git a/source/configs.sh b/source/config-sample.sh similarity index 51% rename from source/configs.sh rename to source/config-sample.sh index 299ba03..8946ca0 100644 --- a/source/configs.sh +++ b/source/config-sample.sh @@ -1,5 +1,12 @@ +# Rename this file to `config.sh` and modify as needed. + # Global configs for these scripts. +# +# No shebang needed as this is an include. +# shellcheck disable=SC2148 +# Disable unused error since this file is included in other files. +# shellcheck disable=SC2034 declare -A config=( # The path to the root of your WordPress installation. diff --git a/source/config.sh b/source/config.sh new file mode 100644 index 0000000..efe1c16 --- /dev/null +++ b/source/config.sh @@ -0,0 +1,15 @@ +# Global configs for these scripts. +# +# No shebang needed as this is an include. +# shellcheck disable=SC2148 + +# Disable unused error since this file is included in other files. +# shellcheck disable=SC2034 +declare -A config=( + + # The path to the root of your WordPress installation. + # + # This is set so that these scripts can live outside of the WordPress installation. + [wp_path]='/data/domains/dev.bemidjistate.edu/public_html/' + +); diff --git a/source/includes.sh b/source/includes.sh index bae4b6f..6beffe0 100644 --- a/source/includes.sh +++ b/source/includes.sh @@ -1,11 +1,24 @@ # This file is source'd / included with every script. +# +# No shebang needed as this is an include. +# shellcheck disable=SC2148 # The path to this file. # path_to_this_file=$(realpath "${BASH_SOURCE[0]}"); directory_path=$(dirname "$(realpath "${BASH_SOURCE[0]}")") -source "$directory_path/configs.sh"; -source "$directory_path/wp-cli-overrides.sh"; +source "${directory_path}/config.sh"; +source "${directory_path}/wp-cli-overrides.sh"; + +# Make sure that WP-CLI is installed. +if [[ "$(which wp 2>/dev/null)" == "" ]]; then + echo + echo "The WP-CLI command line interface could not be found. It is required for this script to run." + echo + echo "Visit https://wp-cli.org/ to get WP-CLI." + echo + exit 1; # Exit as a fail. +fi echo '########################################################################'; echo "Working with the site at ${config[wp_path]}"; diff --git a/source/wp-cli-overrides.sh b/source/wp-cli-overrides.sh index effbf32..39f2476 100644 --- a/source/wp-cli-overrides.sh +++ b/source/wp-cli-overrides.sh @@ -1,5 +1,8 @@ # Overrides the default wp command for more readable scripts and more flexible modification of # the scripts usage of the wp command. +# +# No shebang needed as this is an include. +# shellcheck disable=SC2148 # Usage of the wp command on the main site. Skips themes and plugins. wp_skip_all () { diff --git a/theme-used-change-theme.sh b/theme-used-change-theme.sh index 264bee5..56afc74 100755 --- a/theme-used-change-theme.sh +++ b/theme-used-change-theme.sh @@ -1,8 +1,10 @@ -#!/bin/bash +#!/bin/env bash # Loops through all network sites. If the current theme matches the specified name, a different # theme is activated. +source 'source/includes.sh'; + # Set the string of the theme names. current_theme_name='Old Theme'; new_theme_name='New Theme'; @@ -11,17 +13,19 @@ new_theme_name='New Theme'; output_file="theme-changed-sites-log-$(date +'%s')"; # Empty/create the output file. -echo '' > $output_file; +echo "###################################################################" > $output_file; +echo "Sites with theme updated from ${current_theme_name} to ${new_theme_name}" >> $output_file; +echo "###################################################################" >> $output_file; # Network enable the new theme. -wp theme enable $new_theme_name --network --allow-root --skip-themes --skip-plugins; +wp_skip_all theme enable $new_theme_name --network; -for site in $(wp site list --archived=0 --deleted=0 --field=url --allow-root --skip-themes --skip-plugins); do +for site in $(wp_skip_all site list --archived=0 --deleted=0 --field=url); do echo "Site: $site"; # Get the name of the currently active theme of the current network site. - active_theme=$(wp option get current_theme --allow-root --skip-plugins --skip-themes --allow-root --url="${site}"); + active_theme=$(wp_skip_all option get current_theme --url="${site}"); # Check if the current site uses the specified theme and flag $uses_theme as such. [[ "$active_theme" =~ "$current_theme_name" ]] && uses_theme=true || uses_theme=false; @@ -31,7 +35,7 @@ for site in $(wp site list --archived=0 --deleted=0 --field=url --allow-root --s # Add the site URL to the log file. echo "$site" >> $output_file; - echo $(wp theme activate "$new_theme_name" --skip-themes --skip-plugins --url="$site"); + echo $(wp_skip_all theme activate "$new_theme_name" --url="$site"); fi @@ -41,6 +45,6 @@ for site in $(wp site list --archived=0 --deleted=0 --field=url --allow-root --s done; echo; -wp theme delete ${current_theme_name,,} --force --skip-plugins --skip-themes --allow-root; -wp transient delete --all --network --allow-root --skip-themes --skip-plugins; -wp transient delete --all --allow-root --skip-themes --skip-plugins; +wp_skip_all theme delete ${current_theme_name} --force; +wp_skip_all transient delete --all --network; +wp_skip_all transient delete --all; diff --git a/theme-used-generate-sites-list.sh b/theme-used-generate-sites-list.sh index 4fa78d5..59c1cf7 100755 --- a/theme-used-generate-sites-list.sh +++ b/theme-used-generate-sites-list.sh @@ -1,31 +1,39 @@ -#!/bin/bash +#!/bin/env bash # Loops through all sites. If the specified theme is enabled then it gets added to a list. -# Set the string of the -theme_name='My Cool Theme Name'; +source 'source/includes.sh'; + +# Set the string of the theme to look for. Case sensitive! +theme_name='A Cool Theme'; # Set the output file. output_file='theme-activated-log.txt'; # Empty/create the output file. -echo '' > $output_file; +echo "#############################################" > $output_file; +echo "Sites using the ${theme_name} theme" >> $output_file; +echo "#############################################" >> $output_file; -for site in $(wp site list --archived=0 --deleted=0 --field=url --allow-root); do +for site in $(wp_skip_all site list --archived=0 --deleted=0 --spam=0 --field=url); do # Get the WP option current_theme for comparison - active_theme=$(wp option get current_theme --allow-root --url="${site}"); + active_theme=$(wp_skip_all option get current_theme --url="${site}"); + # Using quotes since the var is a config above that accepts regex. + # shellcheck disable=SC2076 [[ "$active_theme" =~ "$theme_name" ]] && uses_theme=true || uses_theme=false; if [ true = "$uses_theme" ]; then echo 'Adding following site to an output file.'; - echo $site >> $output_file; + echo "$site" >> $output_file; fi - echo $site; + echo "$site"; echo "Uses theme: ${active_theme}"; echo "Matched: ${uses_theme}"; echo '==============================================='; done; + + diff --git a/update-gf-recaptcha.sh b/update-gf-recaptcha.sh index 4580cfc..0f5e2ac 100755 --- a/update-gf-recaptcha.sh +++ b/update-gf-recaptcha.sh @@ -1,15 +1,17 @@ -#!/bin/sh +#!/bin/env bash -# Adds your reCAPTCHA keys in Gravity Forms for every site on the WP multisite/network. +# Adds your reCAPTCHA keys in Gravity Forms for every site. + +source 'source/includes.sh'; site_key=''; secret_key=''; -type='checkbox'; # Options: checkbox or invisible +type='checkbox'; # Can be `checkbox` or `invisible`. -for s in $(wp site list --allow-root --field='url'); do +for s in $(wp_skip_all site list --field='url'); do echo "Current site being updated: $s" - wp --allow-root option update rg_gforms_captcha_public_key $site_key --url="${s}"; - wp --allow-root option update rg_gforms_captcha_private_key $secret_key --url="${s}"; - wp --allow-root option update rg_gforms_captcha_type $type --url="${s}"; - wp --allow-root option update gform_recaptcha_keys_status 1 --url="${s}"; + wp_skip_all option update rg_gforms_captcha_public_key "$site_key" --url="${s}"; + wp_skip_all option update rg_gforms_captcha_private_key "$secret_key" --url="${s}"; + wp_skip_all option update rg_gforms_captcha_type $type --url="${s}"; + wp_skip_all option update gform_recaptcha_keys_status 1 --url="${s}"; done;