Skip to content

Commit d49fab3

Browse files
committed
Adds linting. Finalizes source'd files. Replaces shebang.
1 parent 2c27e4b commit d49fab3

20 files changed

Lines changed: 162 additions & 104 deletions

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
.watchsyncrc*
22
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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
This is a collection of miscellaneous scripts that utilize [WP-CLI](https://wp-cli.org/), the command line interface for WordPress.
33

44
# Usage
5-
Clone this repo and configure the path in `source/configs.sh`. Make the scripts executable as needed (i.e. `chmod +x wpcli-scripts/*.sh`).
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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/bin/sh
1+
#!/bin/env bash
22

33
# Set the exact slug of the plugin you want to check
44
plugin_slug='classic-editor';

count-themes.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/bin/sh
1+
#!/bin/env bash
22

33
# This script audits all sites in a WordPress Multisite network and provides a count and percentage
44
# for every active theme.

delete-transient-every-network-site.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/bin/sh
1+
#!/bin/env bash
22

33
# Loop through each network site and delete a transient.
44
#

delete-user.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
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.

empty-site-all.sh

Lines changed: 30 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
#!/bin/bash
1+
#!/bin/env bash
22

33
# A script for emptying each network site and leaving just the network structure.
44
#
55
# DO NOT RUN THIS AS ROOT since some commands could potentially delete unwanted data if run as root.
66

7+
source 'source/includes.sh';
8+
79
# Script version
810
version='1.0.3';
911

@@ -12,7 +14,7 @@ version='1.0.3';
1214
production_dir='www';
1315

1416
# The theme name/slug to activate.
15-
theme_to_use='my-theme-name';
17+
theme_to_use='some-theme';
1618

1719
# Gravity forms license key. Leave empty if you do not use it.
1820
gf_license_key=""
@@ -26,7 +28,7 @@ if [[ $current_dir =~ "$production_dir" ]]; then
2628
fi
2729

2830
# Make sure that WP-CLI is installed.
29-
if [[ "$(which wp 2>/dev/null)" == "" ]]; then
31+
if [[ "$(which wp_skip_all 2>/dev/null)" == "" ]]; then
3032
echo
3133
echo "The WP-CLI command line interface could not be found. It is required for this script to run."
3234
echo
@@ -52,13 +54,13 @@ echo
5254
read -r -p "Continue? [y/N] " user_consent
5355
echo
5456

55-
# Pause before continuing to be sure the user understands this is desructive.
57+
# Pause before continuing to be sure the user understands this is destructive.
5658
case $user_consent in
5759
[yY])
5860
echo "You have agreed to destroy the entire site. Continuing."
5961
;;
6062
*)
61-
echo "You have chosen not to contiune. Exiting."
63+
echo "You have chosen not to continue. Exiting."
6264
exit;
6365
;;
6466
esac
@@ -71,7 +73,7 @@ esac
7173

7274
echo $SECONDS > /dev/null # start the timer
7375
echo
74-
wp maintenance-mode activate
76+
wp_skip_all maintenance-mode activate
7577

7678
# Site options to retain when wiping a site.
7779
site_options_to_retain=(
@@ -82,7 +84,7 @@ site_options_to_retain=(
8284
"admin_email"
8385
);
8486

85-
# Build the string of --exclude args for `wp option list`
87+
# Build the string of --exclude args for `wp_skip_all option list`
8688
site_options_exclude_regex=$(printf "^%s$|" "${site_options_to_retain[@]}")
8789
site_options_exclude_regex=${site_options_exclude_regex::-1} # remove the last pipe
8890

@@ -91,7 +93,7 @@ site_options_exclude_regex=${site_options_exclude_regex::-1} # remove the last p
9193
# Destroy on a network level.
9294
###################################################################################################
9395
echo
94-
echo "Starting sitewide destruction..."
96+
echo "Starting multisite destruction..."
9597
echo
9698

9799
###################################################################################################
@@ -102,37 +104,31 @@ echo "Destroying and removing archived and deleted sites."
102104

103105
delete_site() {
104106

105-
site_id=$(wp site list | awk -v site_url=$site_url '{ if( $2 == site_url ) print $1; }')
107+
site_id=$(wp_skip_all site list | awk -v site_url=$site_url '{ if( $2 == site_url ) print $1; }')
106108

107109
echo "Wiping site ID: ${site_id}"
108110

109-
wp site delete --yes --skip-themes $site_id
111+
wp_skip_all site delete --yes --skip-themes $site_id
110112
}
111113

112-
for site_url in $(wp site list --deleted=1 --field=url); do
114+
for site_url in $(wp_skip_all site list --deleted=1 --field=url); do
113115
echo "DELETED site found: ${site_url}"
114116
delete_site;
115117
done;
116118

117-
for site_url in $(wp site list --archived=1 --field=url); do
119+
for site_url in $(wp_skip_all site list --archived=1 --field=url); do
118120
echo "ARCHIVED site found: ${site_url}"
119121
delete_site;
120122
done;
121123

122-
for site_url in $(wp site list --spam=1 --field=url); do
124+
for site_url in $(wp_skip_all site list --spam=1 --field=url); do
123125
echo "SPAM site found: ${site_url}"
124126
delete_site;
125127
done;
126128

127-
128-
# wp-cli with extra flags and targeting one site.
129-
wp_on_site () {
130-
wp --skip-themes --url="${site_url}" "$@"
131-
}
132-
133129
echo
134130
echo 'Emptying active sites.'
135-
for site_url in $(wp site list --deleted=0 --archived=0 --spam=0 --field=url); do
131+
for site_url in $(wp_skip_all site list --deleted=0 --archived=0 --spam=0 --field=url); do
136132

137133
# Get the site nice name.
138134
blogname=$(wp_on_site option get blogname)
@@ -208,40 +204,40 @@ rm -rf "${current_dir}/wp-content/uploads/*" 2>/dev/null
208204
wp_on_site transient delete --all
209205

210206
# Disable caching
211-
wp update option wp_cache_enabled "0"
207+
wp_skip_all update option wp_cache_enabled "0"
212208

213209
# For some reason network meta also gets changed. ms_files_rewrite
214210
# should be set to 0 otherwise WP reverts back to uploads file
215211
# structure from WP versions pre-3.5.
216-
wp network meta set 1 ms_files_rewriting 0
212+
wp_skip_all network meta set 1 ms_files_rewriting 0
217213

218-
wp network meta set 1 subdomain_install 0 # We are using sub dirs
219-
wp network meta set 1 global_terms_enabled 0
214+
wp_skip_all network meta set 1 subdomain_install 0 # We are using sub dirs
215+
wp_skip_all network meta set 1 global_terms_enabled 0
220216

221217
# wp_rg_* are deprecated tables https://docs.gravityforms.com/deprecated-database-tables/
222-
for table in $(wp db tables wp_gf_* wp_*_gf_* wp_rg_* wp_*_rg_* --all-tables); do
218+
for table in $(wp_skip_all db tables wp_gf_* wp_*_gf_* wp_rg_* wp_*_rg_* --all-tables); do
223219
# Create a comma separated list of tables to be dropped for a MySQL qeury
224220
tables_to_remove_sql="${tables_to_remove_sql}, ${table}"
225221
done;
226222

227223
tables_to_remove_sql=${tables_to_remove_sql:2} # remove the first two chars (, )
228224

229225
# Install and activate Gravity Forms.
230-
wp plugin deactivate gravityforms --network
231-
wp plugin uninstall gravityforms
226+
wp_skip_all plugin deactivate gravityforms --network
227+
wp_skip_all plugin uninstall gravityforms
232228
echo "Deleting tables created by Gravity Forms."
233-
wp db query "DROP TABLE IF EXISTS ${tables_to_remove_sql}"
234-
wp maintenance-mode deactivate
229+
wp_skip_all db query "DROP TABLE IF EXISTS ${tables_to_remove_sql}"
230+
wp_skip_all maintenance-mode deactivate
235231

236232
# Check if a Gravity Forms license key was provided.
237233
if [[ -n "$gf_license_key" ]]; then
238-
wp plugin install gravityforms
239-
wp plugin install gravityformscli --activate-network || echo -e "\e[1;31mFailed to install Gravity Forms CLI.\e[0m"
240-
wp gf install --skip-themes --key="${gf_license_key}" --activate-network
241-
wp gf setup --skip-themes --force
234+
wp_skip_all plugin install gravityforms
235+
wp_skip_all plugin install gravityformscli --activate-network || echo -e "\e[1;31mFailed to install Gravity Forms CLI.\e[0m"
236+
wp_skip_all gf install --skip-themes --key="${gf_license_key}" --activate-network
237+
wp_skip_all gf setup --skip-themes --force
242238
fi
243239

244-
home_page_url=$(wp site list --field="url" | head -n 1)
240+
home_page_url=$(wp_skip_all site list --field="url" | head -n 1)
245241
echo "Running wp-cron.php for the first time. (${home_page_url}wp-cron.php)"
246242
curl -s "${home_page_url}wp-cron.php" -o /dev/null
247243

empty-site-single.sh

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
#!/bin/bash
1+
#!/bin/env bash
22
#
33
# A script for emptying an individual network site and leaving an empty site.
44
#
55
# NOTE: Some PHP scripts may not like to be loaded via the command line. In some cases you can
66
# check if 'cli' === php_sapi_name() and return out of a function. If you see any PHP errors while
77
# running this script, you may want to start there.
88

9+
source 'source/includes.sh';
10+
911
# Script version
1012
version='1.0.3';
1113

@@ -30,21 +32,11 @@ if [[ $current_dir =~ "$production_dir" ]]; then
3032
echo
3133
fi
3234

33-
# Make sure that WP-CLI is installed.
34-
if [[ "$(which wp 2>/dev/null)" == "" ]]; then
35-
echo
36-
echo "The WP-CLI command line interface could not be found. It is required for this script to run."
37-
echo
38-
echo "Visit https://wp-cli.org/ to get WP-CLI."
39-
echo
40-
exit
41-
fi
42-
4335
while [[ -z $site_id ]]; do
4436
read -p "Please provide the Site ID: " site_id;
4537
done;
4638

47-
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)";
39+
site_url="$(wp_skip_all db query "SELECT option_value FROM wp_${site_id}_options WHERE option_name = 'siteurl'" --skip-column-names)";
4840
site_url="${site_url}/";
4941

5042
if [[ -z $site_url ]]; then
@@ -110,13 +102,13 @@ site_options_exclude_regex=${site_options_exclude_regex::-1} # remove the last p
110102
# Destroy the individual site.
111103
###################################################################################################
112104

113-
wp --allow-root maintenance-mode activate
105+
wp_skip_all maintenance-mode activate
114106

115107
echo "Destroying site ID: ${site_id}"
116108

117-
# wp-cli with extra flags and targetting one site.
109+
# wp-cli with extra flags and targeting one site.
118110
wp_on_site () {
119-
wp --allow-root --url="${site_url}" "$@"
111+
wp_skip_all --url="${site_url}" "$@"
120112
}
121113

122114
echo
@@ -193,10 +185,10 @@ wp_on_site rewrite structure '/%year%/%monthnum%/%day%/%postname%/'
193185
# Final Cleanup
194186
###################################################################################################
195187

196-
wp --allow-root maintenance-mode deactivate
188+
wp_skip_all maintenance-mode deactivate
197189

198190
# Run wp-cron now that all of these changes were made.
199-
home_page_url=$(wp site list --allow-root --field="url" | head -n 1)
191+
home_page_url=$(wp_skip_all site list --field="url" | head -n 1)
200192
echo "Running wp-cron.php for the first time. (${home_page_url}wp-cron.php)"
201193
curl -s "${home_page_url}wp-cron.php" -o /dev/null
202194

plugin-activate-based-on-theme.sh

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

3-
# Activates or deactivates a plugin based on the currently installed theme.
3+
# Activates or deactivates a plugin based on the currently activated theme.
44

5+
source 'source/includes.sh';
6+
7+
# The plugin slug to activate.
8+
plugin='classic-editor';
9+
10+
# The theme to check for when activating the plugin.
511
theme='some-theme';
6-
plugin='a-plugin';
712

8-
wp plugin deactivate --network $plugin;
13+
wp_skip_all plugin deactivate --network $plugin;
914

1015
# Get every network site.
11-
for s in $(wp site list --field=url); do
16+
for s in $(wp_skip_all site list --field=url); do
1217

1318
echo;
1419

1520
# Run the command but we really want the return status: $?
16-
wp theme is-active $theme --url="$s";
21+
wp_skip_all theme is-active $theme --url="$s";
1722
theme_is_not_active=$?;
1823
echo "Site: $s";
1924

2025
if [[ 1 -eq $theme_is_not_active ]]; then
2126
echo "$theme is NOT active.";
2227
echo "Activating $plugin plugin...";
23-
wp plugin activate $plugin --url="$s";
28+
wp_skip_all plugin activate $plugin --url="$s";
2429
else
2530
echo "$theme is active. Skipping plugin activation.";
2631
echo "Deactivating $plugin plugin...";
27-
wp plugin deactivate $plugin --url="$s";
32+
wp_skip_all plugin deactivate $plugin --url="$s";
2833
fi;
2934

3035
done;

0 commit comments

Comments
 (0)