Skip to content

Commit ce5463d

Browse files
Merge pull request #1 from JasonRaveling/source-functions
Adds source functions and linting
2 parents 39b7bc7 + d49fab3 commit ce5463d

21 files changed

+375
-121
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.watchsyncrc*
2+
watch-sync.sh
3+
config.sh

.shellcheckrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
external-sources=true

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
# WP-CLI scripts
22
This is a collection of miscellaneous scripts that utilize [WP-CLI](https://wp-cli.org/), the command line interface for WordPress.
3+
4+
# Usage
5+
Clone this repo and configure the path in `source/config.sh`. Make the scripts executable as needed (i.e. `chmod +x wpcli-scripts/*.sh`).

count-plugin.sh

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
#!/bin/env bash
2+
3+
# Set the exact slug of the plugin you want to check
4+
plugin_slug='classic-editor';
5+
6+
source 'source/includes.sh';
7+
8+
# Requires 'bc' for floating-point percentage calculation
9+
if ! command -v bc &> /dev/null; then
10+
echo "Error: 'bc' (basic calculator) is not installed."
11+
echo "Please install 'bc' to run this script."
12+
exit 1
13+
fi
14+
15+
# Initialize counters
16+
active_count=0
17+
failed_sites=0
18+
19+
# Get all site URLs into a bash array (ignores 'Loading...' line)
20+
mapfile -t sites < <(wp_skip_all site list --field=url --quiet)
21+
22+
# Get total site count
23+
total_sites=${#sites[@]}
24+
25+
if [[ $total_sites -eq 0 ]]; then
26+
echo "No sites found."
27+
exit 0
28+
fi
29+
30+
echo "Auditing '$plugin_slug' plugin status across $total_sites sites..."
31+
echo "---"
32+
33+
# Loop through each site and check plugin status.
34+
for s in "${sites[@]}"; do
35+
36+
# Check if the plugin is active. Suppress errors for archived/deleted sites.
37+
# Exits with 0 if active, 1 if inactive/not-installed.
38+
wp_skip_all plugin is-active "$plugin_slug" --url="$s" >/dev/null 2>&1
39+
plugin_status=$?
40+
41+
if [[ $plugin_status -eq 0 ]]; then
42+
# Status 0: Plugin is ACTIVE
43+
((active_count++))
44+
echo "Site: $s | ACTIVE"
45+
elif [[ $plugin_status -eq 1 ]]; then
46+
# Status 1: Plugin is INACTIVE or not installed
47+
echo "Site: $s | INACTIVE"
48+
else
49+
# Any other status: WP-CLI command failed
50+
echo "Site: $s | FAILED to get status (site may be archived or deleted)"
51+
((failed_sites++))
52+
fi
53+
done
54+
55+
# Calculate results. Base percentages only on sites we could successfully check.
56+
total_checked=$((total_sites - failed_sites))
57+
inactive_count=$((total_checked - active_count))
58+
59+
if [[ $total_checked -gt 0 ]]; then
60+
# Use 'bc' for floating-point percentage calculation
61+
active_perc=$(echo "scale=2; ($active_count / $total_checked) * 100" | bc)
62+
inactive_perc=$(echo "scale=2; ($inactive_count / $total_checked) * 100" | bc)
63+
else
64+
active_perc="0.00"
65+
inactive_perc="0.00"
66+
fi
67+
68+
echo
69+
echo "##################################################"
70+
echo " Plugin Usage Report for: $plugin_slug"
71+
echo "##################################################"
72+
73+
# Print the report
74+
printf "Active: %-5s sites (%s%%)\n" "$active_count" "$active_perc"
75+
printf "Inactive: %-5s sites (%s%%)\n" "$inactive_count" "$inactive_perc"
76+
echo "---"
77+
printf "Total Sites Checked: %s\n" "$total_checked"
78+
if [[ $failed_sites -gt 0 ]]; then
79+
printf "Sites Failed (skipped): %s\n" "$failed_sites"
80+
fi

count-themes.sh

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
#!/bin/env bash
2+
3+
# This script audits all sites in a WordPress Multisite network and provides a count and percentage
4+
# for every active theme.
5+
6+
source 'source/includes.sh';
7+
8+
# Requires 'bc' for floating-point percentage calculation.
9+
if ! command -v bc &> /dev/null; then
10+
echo "Error: 'bc' (basic calculator) is not installed."
11+
echo "Please install 'bc' to run this script."
12+
exit 1
13+
fi
14+
15+
# Declare an associative array to hold theme counts
16+
declare -A theme_counts
17+
18+
# Get all site URLs into a bash array (ignores 'Loading...' line)
19+
mapfile -t sites < <(wp_skip_all site list --field=url --deleted=0 --archived=0 --quiet)
20+
21+
# Get total site count
22+
total_sites=${#sites[@]}
23+
failed_sites=0
24+
25+
if [[ $total_sites -eq 0 ]]; then
26+
echo "No sites found."
27+
exit 0
28+
fi
29+
30+
echo "Auditing $total_sites sites..."
31+
echo "---"
32+
33+
# Loop through each site and get its active theme
34+
for s in "${sites[@]}"; do
35+
# Get the slug of the active theme.
36+
# '2>/dev/null' suppresses errors from archived/deleted sites.
37+
active_theme=$(wp_skip_all theme list --status=active --field=name --url="$s" 2>/dev/null)
38+
39+
# Check if the command was successful and returned a theme name
40+
if [[ -n "$active_theme" ]]; then
41+
# Increment the count for this theme in the array
42+
((theme_counts[$active_theme]++))
43+
echo "Site: $s | Theme: $active_theme"
44+
else
45+
echo "Site: $s | FAILED to get theme (site may be archived or deleted)"
46+
((failed_sites++))
47+
fi
48+
done
49+
50+
echo
51+
echo "########################################"
52+
echo " Theme Usage Report"
53+
echo "########################################"
54+
55+
# Loop through the counted themes and print the report
56+
57+
# Get all unique theme names (the array keys) and sort them
58+
sorted_themes=($(printf "%s\n" "${!theme_counts[@]}" | sort))
59+
60+
for theme in "${sorted_themes[@]}"; do
61+
count=${theme_counts[$theme]}
62+
63+
# Use 'bc' for floating-point percentage calculation
64+
percentage=$(echo "scale=2; ($count / $total_sites) * 100" | bc)
65+
66+
# Use 'printf' for clean, aligned columns
67+
printf "Theme: %-30s | Count: %-5s | Percentage: %s%%\n" "$theme" "$count" "$percentage"
68+
done
69+
70+
echo "---"
71+
printf "Total Sites Checked: %-5s\n" "$total_sites"
72+
if [[ $failed_sites -gt 0 ]]; then
73+
printf "Sites Failed: %-5s (not included in percentage)\n" "$failed_sites"
74+
fi

delete-transient-every-network-site.sh

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
1-
#!/bin/sh
1+
#!/bin/env bash
22

33
# Loop through each network site and delete a transient.
4-
4+
#
55
# The transient to search for (can use wild cards *).
66
transient_search_term='*events_*';
77

8+
source 'source/includes.sh';
9+
810
echo;
9-
for site_url in $(wp site list --allow-root --skip-themes --skip-plugins --deleted=0 --archived=0 --spam=0 --field=url); do
11+
for site_url in $(wp_skip_all site list --field=url); do
1012

11-
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}'
13+
found_transients=$(wp_skip_all transient list --format=csv --fields=name --search=$transient_search_term --url=$site_url | awk 'NR>1 {print}'
1214
);
1315

1416
echo '################################################'
@@ -18,13 +20,11 @@ for site_url in $(wp site list --allow-root --skip-themes --skip-plugins --delet
1820

1921
# Check if it is NOT empty.
2022
if [[ -z $found_transients ]]; then
21-
echo 'none';
23+
echo ' none';
2224
else
23-
echo $found_transients;
25+
echo " $found_transients";
2426
fi
2527

26-
echo;
27-
2828
for transient in $found_transients; do
2929

3030
wp transient delete --allow-root --skip-themes --skip-plugins --url=$site_url $transient;

delete-user.sh

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
#!/bin/sh
1+
#!/bin/env bash
22

33
# This interactive script deletes a given user by their ID and reattributes their content to another
44
# user based on the provided ID.
55

6+
source 'source/includes.sh';
7+
68
echo;
79
echo 'You are about to delete a user and attribute any content they may have to another use.';
810
echo;
@@ -28,22 +30,22 @@ done;
2830

2931
# Prompt for the site ID to delete the user from.
3032
if [[ 999 = $site_id ]]; then
31-
sites=$(wp --allow-root --skip-themes --skip-plugins site list --field="url" --archived=0);
33+
sites=$(wp_skip_all site list --field="url" --archived=0);
3234
sites_name='All sites in the WP network';
3335
else
34-
sites=$(wp --allow-root --skip-themes --skip-plugins site list --field="url" --site__in=${site_id});
36+
sites=$(wp_skip_all site list --field="url" --site__in=${site_id});
3537
sites_name="$sites";
3638
fi
3739

3840
echo;
3941

4042
# Add human readable info for the user to be deleted.
41-
user_name_delete=$(wp --allow-root --skip-themes --skip-plugins user get ${user_id_delete} --field="display_name");
42-
# user_login_delete=$(wp --allow-root --skip-themes --skip-plugins user get ${user_id_delete} --field="user_login");
43+
user_name_delete=$(wp_skip_all user get ${user_id_delete} --field="display_name");
44+
# user_login_delete=$(wp_skip_all user get ${user_id_delete} --field="user_login");
4345

4446
# Add human readable info for the user to get attribution of content.
45-
user_name_attribute=$(wp --allow-root --skip-themes --skip-plugins user get ${user_id_attribute} --field="display_name");
46-
# user_login_attribute=$(wp --allow-root --skip-themes --skip-plugins user get ${user_id_attribute} --field="user_login");
47+
user_name_attribute=$(wp_skip_all user get ${user_id_attribute} --field="display_name");
48+
# user_login_attribute=$(wp_skip_all user get ${user_id_attribute} --field="user_login");
4749

4850
# Confirm before moving on.
4951
while [[ -z "$confirmed" ]]; do
@@ -72,5 +74,5 @@ esac
7274
# you reattribute content.
7375
echo 'Removing the user...';
7476
for site in $sites; do
75-
wp --allow-root --skip-themes --skip-plugins user delete $user_id_delete --reassign="${user_id_attribute}" --url="${site}" ;
77+
wp_skip_all user delete $user_id_delete --reassign="${user_id_attribute}" --url="${site}" ;
7678
done;

0 commit comments

Comments
 (0)