Skip to content

Commit 866838d

Browse files
feat(functions): add allow-root flag (#9)
* feat(functions): add option to config allow-root Add a new config option to add `--allow-root` to the wpcli command. The wpcli override functions are now dynamically built. * feat(user list): output user list in csv format and update var name
1 parent b757937 commit 866838d

3 files changed

Lines changed: 61 additions & 9 deletions

File tree

source/config-sample.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,10 @@ declare -A config=(
1414
# This is set so that these scripts can live outside of the WordPress installation.
1515
[wp_path]='/var/www/public_html/'
1616

17+
# Whether or not to allow root to run wpcli.
18+
#
19+
# By default wpcli will display an error and exit if you run it as root. Set this to 1 to enable
20+
# running wpcli as root.
21+
[allow_root]=0
22+
1723
);

source/wp-cli-overrides.sh

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,59 @@
66

77
# Usage of the wp command on the main site. Skips themes and plugins.
88
wp_skip_all () {
9-
wp --skip-themes --skip-plugins --path="${config[wp_path]}" "$@"
9+
10+
# Setup the variable for building a wpcli command.
11+
local -a cmd=( wp )
12+
13+
# Add the wpcli command to run and any additional flags that were used.
14+
cmd+=( "$@" )
15+
16+
# Check if a path to a WP installation was provided. If none was provided, wpcli will default to
17+
# the current directory.
18+
if [[ -n "${config[wp_path]:-}" ]]; then
19+
cmd+=( --path="${config[wp_path]}" )
20+
fi
21+
22+
# Check if allow root was set to 1 and add the flag.
23+
if (( config[allow_root] )); then
24+
cmd+=( --allow-root )
25+
fi
26+
27+
# Add flags to use every time.
28+
cmd+=( '--skip-themes --skip-plugins' )
29+
30+
# Run the command
31+
"${cmd[@]}"
32+
1033
}
1134

1235

1336
# Usage of the wp command for a specific site.
1437
#
1538
# It expects $site_url to be set. This is useful in a for loop of every site.
1639
wp_on_site () {
17-
wp --skip-themes --skip-plugins --path="${config[wp_path]}" --url="${site_url}" "$@"
40+
41+
# Setup the variable for building a wpcli command.
42+
local -a cmd=( wp )
43+
44+
# Add the wpcli command to run and any additional flags that were used.
45+
cmd+=( "$@" )
46+
47+
# Check if a path to a WP installation was provided. If none was provided, wpcli will default to
48+
# the current directory.
49+
if [[ -n "${config[wp_path]:-}" ]]; then
50+
cmd+=( --path="${config[wp_path]}" )
51+
fi
52+
53+
# Check if allow root was set to 1 and add the flag.
54+
if (( config[allow_root] )); then
55+
cmd+=( --allow-root )
56+
fi
57+
58+
# Add flags to use every time.
59+
cmd+=( --skip-themes --skip-plugins --url="${site_url}" )
60+
61+
# Run the command
62+
"${cmd[@]}"
63+
1864
}

user-list.sh

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,16 @@ source 'source/includes.sh';
66

77
fields_to_display='user_login,user_email,roles';
88

9-
log_file='user-list.txt';
9+
output_file='user-list.txt';
1010

11-
echo '#########################################################' | tee "$log_file";
12-
echo 'Users by WordPress site' | tee -a "$log_file";
13-
echo '#########################################################' | tee -a "$log_file";
11+
echo '#########################################################' | tee "$output_file";
12+
echo 'Users by WordPress site' | tee -a "$output_file";
13+
echo '#########################################################' | tee -a "$output_file";
1414

1515
for site_url in $(wp_skip_all site list --field="url" --archived=0 --deleted=0 --spam=0); do
1616

17-
echo '-------------------------------------------------' | tee -a "$log_file";
18-
echo "Site ${site_url}" | tee -a "$log_file";
19-
wp_on_site user list --fields="${fields_to_display}" | tee -a "$log_file";
17+
echo '-------------------------------------------------' | tee -a "$output_file";
18+
echo "Site ${site_url}" | tee -a "$output_file";
19+
wp_on_site user list --format=csv --fields="${fields_to_display}" | tee -a "$output_file";
2020

2121
done;

0 commit comments

Comments
 (0)