diff --git a/source/config-sample.sh b/source/config-sample.sh index 8946ca0..4dc99a9 100644 --- a/source/config-sample.sh +++ b/source/config-sample.sh @@ -14,4 +14,10 @@ declare -A config=( # This is set so that these scripts can live outside of the WordPress installation. [wp_path]='/var/www/public_html/' + # Whether or not to allow root to run wpcli. + # + # By default wpcli will display an error and exit if you run it as root. Set this to 1 to enable + # running wpcli as root. + [allow_root]=0 + ); diff --git a/source/wp-cli-overrides.sh b/source/wp-cli-overrides.sh index 757eeca..87f5357 100644 --- a/source/wp-cli-overrides.sh +++ b/source/wp-cli-overrides.sh @@ -6,7 +6,30 @@ # 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]}" "$@" + + # Setup the variable for building a wpcli command. + local -a cmd=( wp ) + + # Add the wpcli command to run and any additional flags that were used. + cmd+=( "$@" ) + + # Check if a path to a WP installation was provided. If none was provided, wpcli will default to + # the current directory. + if [[ -n "${config[wp_path]:-}" ]]; then + cmd+=( --path="${config[wp_path]}" ) + fi + + # Check if allow root was set to 1 and add the flag. + if (( config[allow_root] )); then + cmd+=( --allow-root ) + fi + + # Add flags to use every time. + cmd+=( '--skip-themes --skip-plugins' ) + + # Run the command + "${cmd[@]}" + } @@ -14,5 +37,28 @@ 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 --skip-plugins --path="${config[wp_path]}" --url="${site_url}" "$@" + + # Setup the variable for building a wpcli command. + local -a cmd=( wp ) + + # Add the wpcli command to run and any additional flags that were used. + cmd+=( "$@" ) + + # Check if a path to a WP installation was provided. If none was provided, wpcli will default to + # the current directory. + if [[ -n "${config[wp_path]:-}" ]]; then + cmd+=( --path="${config[wp_path]}" ) + fi + + # Check if allow root was set to 1 and add the flag. + if (( config[allow_root] )); then + cmd+=( --allow-root ) + fi + + # Add flags to use every time. + cmd+=( --skip-themes --skip-plugins --url="${site_url}" ) + + # Run the command + "${cmd[@]}" + } diff --git a/user-list.sh b/user-list.sh index 5b3c62e..628db8b 100755 --- a/user-list.sh +++ b/user-list.sh @@ -6,16 +6,16 @@ source 'source/includes.sh'; fields_to_display='user_login,user_email,roles'; -log_file='user-list.txt'; +output_file='user-list.txt'; -echo '#########################################################' | tee "$log_file"; -echo 'Users by WordPress site' | tee -a "$log_file"; -echo '#########################################################' | tee -a "$log_file"; +echo '#########################################################' | tee "$output_file"; +echo 'Users by WordPress site' | tee -a "$output_file"; +echo '#########################################################' | tee -a "$output_file"; for site_url in $(wp_skip_all site list --field="url" --archived=0 --deleted=0 --spam=0); do - echo '-------------------------------------------------' | tee -a "$log_file"; - echo "Site ${site_url}" | tee -a "$log_file"; - wp_on_site user list --fields="${fields_to_display}" | tee -a "$log_file"; + echo '-------------------------------------------------' | tee -a "$output_file"; + echo "Site ${site_url}" | tee -a "$output_file"; + wp_on_site user list --format=csv --fields="${fields_to_display}" | tee -a "$output_file"; done;