-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwordpress-update
More file actions
executable file
·399 lines (337 loc) · 10.5 KB
/
wordpress-update
File metadata and controls
executable file
·399 lines (337 loc) · 10.5 KB
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
#!/usr/bin/env bash
# WordPress installation updater
# Usage: ./wordpress-update [OPTIONS]
set -euo pipefail
# Set terminal type for proper color support
export TERM=xterm-256color
# Main function wrapper
main() {
# Check if common-functions exists
if [[ ! -f "$(dirname "$0")/common-functions" ]]; then
echo "Downloading common-functions from GitHub..."
if ! curl -fsSL https://raw.githubusercontent.com/Flower7C3/bash-tools/master/common-functions -o "$(dirname "$0")/common-functions"; then
echo "Failed to download common-functions"
exit 1
fi
fi
# Source common functions
source "$(dirname "$0")/common-functions"
# Call the actual main function
wordpress_main "$@"
}
wordpress_main() {
# Function to display usage
function show_usage() {
log_usage_title '[OPTIONS]'
echo
echo 'Update WordPress installations safely'
echo
log_header 'Options'
log_usage_options_line '-v;--version <VERSION>' \
'WordPress version to install (default: %s)' "$DEFAULT_WP_VERSION"
log_usage_options_line '-s;--source <PATH>' \
'WordPress source directory path (default: current directory)'
log_usage_options_line '-i;--issue <ISSUE>' \
'Issue/ticket number for git commit'
log_usage_options_line '-b;--no-backup' \
'Skip creating backup before update'
log_usage_options_line '-g;--no-git' \
'Skip git operations'
log_usage_options_line '-n;--dry-run' \
'Show what would be done without making changes'
log_usage_options_line '-f;--force' \
'Force update even if version is the same'
log_usage_options_line '-u;--update' \
'Update app'
log_usage_options_line '-h;--help' \
'Show this help message'
echo
log_header 'Examples'
log_usage_example_line '--version 6.4.2 --source /var/www/html'
log_usage_example_line '--version latest --issue "WP-123"'
log_usage_example_line '--dry-run --version 6.4.1'
log_usage_example_line '--no-backup --no-git --version 6.4.2'
echo
log_header 'Notes'
echo ' - Always backup your WordPress installation before updating'
echo ' - The script will update core WordPress files only'
echo ' - Plugins and themes are not affected'
echo ' - wp-config.php is preserved'
echo ' - Custom uploads and content are preserved'
exit 0
}
# Default values
DEFAULT_WP_VERSION="latest"
DEFAULT_WP_SOURCE="$(pwd)"
DEFAULT_ISSUE_NO=""
DEFAULT_BACKUP=1
DEFAULT_GIT_COMMIT=1
# WordPress core files and directories
readonly WP_DIRECTORIES=("wp-admin/" "wp-includes/")
readonly WP_FILES=("index.php" "license.txt" "readme.html" "wp-activate.php" "wp-blog-header.php" "wp-comments-post.php" "wp-config-sample.php" "wp-links-opml.php" "wp-load.php" "wp-login.php" "wp-mail.php" "wp-settings.php" "wp-signup.php" "wp-trackback.php" "xmlrpc.php")
# Global variables
wp_version="$DEFAULT_WP_VERSION"
wp_source="$DEFAULT_WP_SOURCE"
issue_no="$DEFAULT_ISSUE_NO"
backup="$DEFAULT_BACKUP"
git_commit="$DEFAULT_GIT_COMMIT"
dry_run=0
force=0
# Function to get current WordPress version
get_current_wp_version() {
local _docroot="$1"
local _version_file="$_docroot/wp-includes/version.php"
if [[ -f "$_version_file" ]]; then
grep "wp_version =" "$_version_file" | cut -d"'" -f2
else
echo "unknown"
fi
}
# Function to get latest WordPress version
get_latest_wp_version() {
local _latest_version
if ! _latest_version=$(curl -s "https://api.wordpress.org/core/version-check/1.7/" | grep -o '"version":"[^"]*"' | head -1 | cut -d'"' -f4); then
log_error 'Failed to get latest WordPress version'
return 1
fi
echo "$_latest_version"
}
# Function to create backup
create_backup() {
local _docroot="$1"
local _backup_dir
_backup_dir="${_docroot}/../wp-backup-$(date +%Y%m%d-%H%M%S)"
log_info 'Creating backup to: <u>%s</u>' "$_backup_dir"
if [[ "$dry_run" == "1" ]]; then
log_info 'DRY RUN: Would create backup at <u>%s</u>' "$_backup_dir"
return 0
fi
mkdir -p "$_backup_dir"
# Copy WordPress core files
for dir in "${WP_DIRECTORIES[@]}"; do
if [[ -d "$_docroot/$dir" ]]; then
cp -r "$_docroot/$dir" "$_backup_dir/"
fi
done
for file in "${WP_FILES[@]}"; do
if [[ -f "$_docroot/$file" ]]; then
cp "$_docroot/$file" "$_backup_dir/"
fi
done
log_success ---status "$_backup_dir" "Backup created"
}
# Function to download WordPress
download_wordpress() {
local _version="$1"
local _zip_file="$2"
local _download_url="https://wordpress.org/wordpress-${_version}.zip"
log_info 'Downloading WordPress %s from <u>%s</u>' "$_version" "$_download_url"
if [[ "$dry_run" == "1" ]]; then
log_info 'DRY RUN: Would download from <u>%s</u>' "$_download_url"
return 0
fi
if ! curl -L -o "$_zip_file" "$_download_url"; then
log_error 'Failed to download WordPress %s' "$_version"
return 1
fi
log_success ---status "$_zip_file" "WordPress %s downloaded successfully" "$_version"
}
# Function to extract WordPress
extract_wordpress() {
local _zip_file="$1"
local _extract_dir
_extract_dir="$(dirname "$_zip_file")/wordpress"
log_info 'Extracting WordPress archive'
if [[ "$dry_run" == "1" ]]; then
log_info 'DRY RUN: Would extract to <u>%s</u>' "$_extract_dir"
return 0
fi
if ! unzip -q "$_zip_file" -d "$(dirname "$_zip_file")"; then
log_error 'Failed to extract WordPress archive'
return 1
fi
log_success 'WordPress extracted successfully' ---status "$_extract_dir"
}
# Function to update WordPress files
update_wordpress_files() {
local _docroot="$1"
local _extract_dir="$2"
log_info 'Updating WordPress core files'
# Remove old core files
for dir in "${WP_DIRECTORIES[@]}"; do
if [[ -d "$_docroot/$dir" ]]; then
if [[ "$dry_run" != "1" ]]; then
# shellcheck disable=SC2115
rm -rf "$_docroot/$dir"
fi
fi
done
for file in "${WP_FILES[@]}"; do
if [[ -f "$_docroot/$file" ]]; then
if [[ "$dry_run" != "1" ]]; then
rm -f "$_docroot/$file"
fi
fi
done
# Copy new core files
for dir in "${WP_DIRECTORIES[@]}"; do
if [[ -d "$_extract_dir/$dir" ]]; then
if [[ "$dry_run" != "1" ]]; then
cp -r "$_extract_dir/$dir" "$_docroot/"
fi
fi
done
for file in "${WP_FILES[@]}"; do
if [[ -f "$_extract_dir/$file" ]]; then
if [[ "$dry_run" != "1" ]]; then
cp "$_extract_dir/$file" "$_docroot/"
fi
fi
done
log_success 'WordPress core files updated successfully'
}
# Function to commit to git
commit_to_git() {
local _docroot="$1"
local _version="$2"
local _issue="$3"
if [[ "$git_commit" != "1" ]]; then
return 0
fi
log_info 'Committing changes to git'
if [[ "$dry_run" == "1" ]]; then
log_info 'DRY RUN: Would commit to git'
return 0
fi
# Check if we're in a git repository
if ! git rev-parse --git-dir >/dev/null 2>&1; then
log_warning 'Not in a git repository, skipping git operations'
return 0
fi
# Add changes
git add "$_docroot"
# Create commit message
local _commit_message="Upgrade to WordPress $_version"
if [[ -n "$_issue" ]]; then
_commit_message="$_commit_message
Issue: $_issue"
fi
# Commit changes
if ! git commit -m "$_commit_message"; then
log_warning 'Git commit failed or no changes to commit'
else
log_success 'Changes committed to git'
fi
}
# Function to cleanup temporary files
cleanup() {
local _temp_dir="$1"
if [[ -n "$_temp_dir" && -d "$_temp_dir" ]]; then
rm -rf "$_temp_dir"
fi
}
# Function to parse command line arguments
parse_arguments() {
while [[ $# -gt 0 ]]; do
case "$1" in
-v | --version)
wp_version="$2"
shift 2
;;
-s | --source)
wp_source="$2"
shift 2
;;
-i | --issue)
issue_no="$2"
shift 2
;;
-b | --no-backup)
backup=0
shift
;;
-g | --no-git)
git_commit=0
shift
;;
-n | --dry-run)
dry_run=1
shift
;;
-f | --force)
force=1
shift
;;
-h | --help)
show_usage
;;
-u | --update)
update_application
;;
-*)
log_error 'Unknown option: <b>%s</b>' "$1"
echo
show_usage
;;
*)
log_error 'Unexpected argument: <b>%s</b>' "$1"
echo
show_usage
;;
esac
done
}
log_title 'WordPress Update'
parse_arguments "$@"
check_dependencies curl git
if ! validate_wp_directory "$wp_source"; then
exit
fi
# Get current version
local _current_version
_current_version=$(get_current_wp_version "$wp_source")
log_info 'Current WordPress version: %s' "$_current_version"
# Handle "latest" version
if [[ "$wp_version" == "latest" ]]; then
wp_version=$(get_latest_wp_version)
log_info 'Latest WordPress version: %s' "$wp_version"
fi
# Check if update is needed
if [[ "$_current_version" == "$wp_version" && "$force" != "1" ]]; then
log_info 'WordPress is already at version %s' "$wp_version"
if [[ "$force" != "1" ]]; then
log_info 'Use <code>--force</code> to update anyway'
exit 0
fi
fi
# Confirm update
if [[ "$dry_run" != "1" ]]; then
log_warning 'This will update WordPress from <b>%s</b> to <b>%s</b. Make sure you have a backup before proceeding!' "$_current_version" "$wp_version"
confirm_or_exit "Do you want to continue?"
fi
# Create backup
if [[ "$backup" == "1" ]]; then
create_backup "$wp_source"
fi
# Download WordPress
local _temp_dir="/tmp/wp-update-$$"
mkdir -p "$_temp_dir"
local _zip_file="$_temp_dir/wordpress.zip"
download_wordpress "$wp_version" "$_zip_file"
# Extract WordPress
local _extract_dir
_extract_dir=$(extract_wordpress "$_zip_file")
# Update WordPress files
update_wordpress_files "$wp_source" "$_extract_dir"
# Commit to git
commit_to_git "$wp_source" "$wp_version" "$issue_no"
# Cleanup
cleanup "$(dirname "$_zip_file")"
# Final success message
log_success 'WordPress updated successfully from %s to %s' "$_current_version" "$wp_version"
if [[ "$dry_run" == "1" ]]; then
log_info 'This was a dry run - no actual changes were made'
fi
}
# Run main function
main "$@"