-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsetup-wordpress.sh
executable file
·307 lines (257 loc) · 7.42 KB
/
setup-wordpress.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
#!/bin/bash
#
# Notes:
# - docker compose run redirects the stderr of any invoked executables to
# stdout. The only messages that will appear on stderr are issued by docker
# compose run itself. This appears to be an undocumented "feature":
# https://docs.docker.com/engine/reference/commandline/compose_run/
#
# The "2>/dev/null" below silences the messages from docker compose run.
# For example, the output like the following will not be visible:
# [+] Creating 2/0
# ✔ Container cc-wordpress-db Running 0.0s
# ✔ Container cc-web Running 0.0s
#
set -o errexit
set -o errtrace
set -o nounset
# shellcheck disable=SC2154
trap '_es=${?};
printf "${0}: line ${LINENO}: \"${BASH_COMMAND}\"";
printf " exited with a status of ${_es}\n";
exit ${_es}' ERR
# shellcheck disable=SC1091
source .env
OPT_DATE_FORMAT=Y-m-d
OPT_TIME_FORMAT='H:i'
OPT_DEFAULT_COMMENT_STATUS=closed
PLUGINS_ACTIVATE='
acf-menu-chooser
advanced-custom-fields
akismet
classic-editor
redirection
tablepress
wordpress-importer
'
THEMES_ACTIVATE='
vocabulary-theme
'
THEMES_REMOVE='
twentytwentyone
twentytwentytwo
'
WEB_WP_DIR=/var/www/html
WEB_WP_URL=http://localhost:8080
#### FUNCTIONS ################################################################
activate_plugins() {
local _plugin
header 'Activate WordPress plugins'
for _plugin in ${PLUGINS_ACTIVATE}
do
if wpcli --no-color --quiet plugin is-active "${_plugin}" &> /dev/null
then
no_op "${_plugin} is already active"
else
wpcli plugin activate "${_plugin}"
fi
done
echo
}
activate_themes() {
local _theme
header 'Activate WordPress themes'
for _theme in ${THEMES_ACTIVATE}
do
if wpcli --no-color --quiet theme is-active "${_theme}" &> /dev/null
then
no_op "${_theme} is already active"
else
wpcli theme activate "${_theme}"
fi
done
echo
}
composer_install() {
header 'Composer install'
docker compose run --rm composer install 2>/dev/null
echo
}
container_info() {
local _key _val IFS
header 'Container info'
# composer
printf "\033[1m%s\033[0m container - %s\n" \
'composer' 'A Dependency Manager for PHP'
container_print 'Composer version' "$(docker compose run --rm composer \
--no-ansi --version 2>/dev/null | sed -e's/^Composer version //')"
echo
# web
printf "\033[1m%s\033[0m container - %s\n" 'web' 'WordPress'
print_var WEB_WP_URL
print_var WEB_WP_DIR
container_print 'WordPress version:' "$(wpcli core version)"
echo
# wordpress-cli
printf "\033[1m%s\033[0m container - %s\n" \
'wordpress-cli' 'The command line interface for WordPress'
IFS=$'\n'
for _line in $(wpcli --info | sort)
do
_key="${_line%%:*}"
# '| xargs' is used to trim whitespace
_val="$( echo "${_line#*:}" | xargs)"
[[ -n "${_val}" ]] || continue
[[ "${_key}" =~ ^WP-CLI ]] || continue
container_print "${_key}:" "${_val}"
done
echo
}
container_print() {
printf "\033[36m%19s\033[0m %s\n" "${1}" "${2}"
}
enable_permalinks() {
header 'Enable post name permalinks'
if wpcli --no-color --quiet rewrite list 2> /dev/null \
| grep -qF 'page/?([0-9]{1,})/?$'
then
no_op 'rewrite rules exist'
else
wpcli rewrite structure --hard '/%postname%'
fi
echo
}
error_exit() {
# Echo error message and exit with error
echo -e "\033[31mERROR:\033[0m ${*}" 1>&2
exit 1
}
header() {
# Priant 80 character wide black on white heading with time
printf "\033[1m\033[7m %-71s$(date '+%T') \033[0m\n" "${@}"
}
install_wordpress() {
local _err
header 'Install WordPress'
if [[ -n "${WP_ADMIN_EMAIL}" ]] && [[ -n "${WP_ADMIN_EMAIL}" ]] \
&& [[ -n "${WP_ADMIN_EMAIL}" ]]
then
print_var WP_ADMIN_EMAIL
print_var WP_ADMIN_USER
print_var WP_ADMIN_PASS
else
_err='The following variables must be set in .env (see .env.example):'
_err="${_err} WP_ADMIN_EMAIL, WP_ADMIN_USER, WP_ADMIN_PASS"
error_exit "${_err}"
fi
echo
if wpcli --no-color --quiet core is-installed &> /dev/null
then
no_op 'already installed'
else
wpcli core install \
--title='CreativeCommons.org Local Dev' \
--admin_email="${WP_ADMIN_EMAIL}" \
--admin_user="${WP_ADMIN_USER}" \
--admin_password="${WP_ADMIN_PASS}" \
--skip-email
fi
echo
}
no_op() {
# Print no-op message"
printf "\033[1m\033[30mno-op: %s\033[0m\n" "${@}"
}
optimize_tables() {
header 'Optimize WordPress database tables'
wpcli db optimize --color \
| sed -e'/Table does not support optimize/d' -e'/^status : OK/d'
echo
}
print_var() {
printf "\033[36m%19s\033[0m %s\n" "${1}:" "${!1}"
}
remove_themes() {
local _theme
header 'Remove extraneous WordPress themes'
for _theme in ${THEMES_REMOVE}
do
if ! wpcli --no-color --quiet theme is-installed "${_theme}" \
> /dev/null
then
no_op "${_theme} is not installed"
else
wpcli theme delete "${_theme}"
fi
done
echo
}
update_wordpress_options() {
local _date_format _default_comment_status _noop _time_format
header 'Update WordPress options'
_date_format=$(wpcli option get date_format)
if [[ "${OPT_DATE_FORMAT}" != "${_date_format}" ]]
then
echo "Update date_format: ${OPT_DATE_FORMAT}"
wpcli option update date_format "${OPT_DATE_FORMAT}"
else
no_op "date_format: ${OPT_DATE_FORMAT}"
fi
_default_comment_status=$(wpcli option get default_comment_status)
if [[ "${OPT_DEFAULT_COMMENT_STATUS}" != "${_default_comment_status}" ]]
then
echo "Update default_comment_status: ${OPT_DEFAULT_COMMENT_STATUS}"
wpcli option update default_comment_status \
"${OPT_DEFAULT_COMMENT_STATUS}"
else
_noop="default_comment_status: ${OPT_DEFAULT_COMMENT_STATUS}"
no_op "${_noop}"
fi
_time_format=$(wpcli option get time_format)
if [[ "${OPT_TIME_FORMAT}" != "${_time_format}" ]]
then
echo "Update time_format: ${OPT_TIME_FORMAT}"
wpcli option update time_format "${OPT_TIME_FORMAT}"
else
no_op "time_format: ${OPT_TIME_FORMAT}"
fi
echo
}
wordpress_update_db() {
header 'Update WordPress database'
wpcli core update-db
echo
}
wordpress_db_check() {
header 'Check WordPress database'
wpcli db check
echo
}
wordpress_status() {
header 'Show maintenance mode status to expose any PHP Warnings'
wpcli maintenance-mode status
echo
}
wpcli() {
# Call WP-CLI with appropriate site arguments via Docker
docker compose run --rm \
--env WP_ADMIN_USER="${WP_ADMIN_USER}" \
--env WP_ADMIN_PASS="${WP_ADMIN_PASS}" \
--env WP_ADMIN_EMAIL="${WP_ADMIN_EMAIL}" \
wordpress-cli \
/usr/local/bin/wp --path="${WEB_WP_DIR}" --url="${WEB_WP_URL}" \
"${@}" 2>/dev/null
}
#### MAIN #####################################################################
container_info
composer_install
install_wordpress
update_wordpress_options
remove_themes
activate_plugins
activate_themes
enable_permalinks
wordpress_update_db
optimize_tables
wordpress_db_check
wordpress_status