Added wp-config.php update

main
Anthony 2025-08-06 22:46:20 +08:00
parent f82d941dcc
commit f671993002
1 changed files with 79 additions and 21 deletions

View File

@ -154,6 +154,25 @@ fi
info "WP-CLI final execution command prefix: [${SUDO_CMD:-direct}]"
# --- Domain normalization helper ---
trim_trailing_slash() {
local str="$1"
[[ "$str" == */ ]] && str="${str%/}"
printf '%s' "$str"
}
OLD_URL="${WP_CLI_ARGS[0]}"
NEW_URL="${WP_CLI_ARGS[1]}"
OLD_NORMAL=$(trim_trailing_slash "$OLD_URL")
NEW_NORMAL=$(trim_trailing_slash "$NEW_URL")
# Array of replacement tasks (each element "old|||new")
TASKS=("$OLD_URL|||$NEW_URL")
if [[ "$OLD_URL" != "$OLD_NORMAL" ]]; then
# also replace variant without trailing slash
TASKS+=("$OLD_NORMAL|||$NEW_NORMAL")
fi
# --- Execute Command ---
info "Executing 'wp search-replace'..."
@ -167,12 +186,54 @@ CMD+=("$WP_EXECUTABLE" "search-replace")
CMD+=("${WP_CLI_ARGS[@]}")
CMD+=("${WP_RUN_ARGS[@]}")
# Run the command and capture exit status
"${CMD[@]}"
STATUS=$?
for pair in "${TASKS[@]}"; do
OLD_PART=${pair%%|||*}
NEW_PART=${pair##*|||}
if [[ $STATUS -eq 0 ]]; then
success "'wp search-replace' command completed successfully."
info "Running wp search-replace '$OLD_PART' '$NEW_PART' …"
# Build command anew for each iteration
CMD=()
if [[ -n "$SUDO_CMD" ]]; then
# shellcheck disable=SC2206
CMD=($SUDO_CMD)
fi
CMD+=("$WP_EXECUTABLE" "search-replace" "$OLD_PART" "$NEW_PART" "${WP_RUN_ARGS[@]}")
SEARCH_OUTPUT=$( "${CMD[@]}" 2>&1 )
STATUS=$?
printf "%s\n" "$SEARCH_OUTPUT"
if [[ $STATUS -ne 0 ]]; then
error_exit "wp search-replace failed for pattern '$OLD_PART' -> '$NEW_PART' (exit $STATUS)."
fi
done
success "All search-replace tasks completed successfully."
# -----------------------------------------------------------------
# Update wp-config.php WP_HOME and WP_SITEURL if they exist
# -----------------------------------------------------------------
CONFIG_FILE="$WP_ROOT/wp-config.php"
if [[ -f "$CONFIG_FILE" && -w "$CONFIG_FILE" ]]; then
info "Updating WP_HOME and WP_SITEURL in wp-config.php …"
# Use the new URL without trailing slash
TARGET_URL="$NEW_NORMAL"
# shellcheck disable=SC2016 # we want literal single quotes inside sed replacement
sed -i -E "s|define\(\s*'WP_HOME',\s*'[^']*'\s*\);|define( 'WP_HOME', '${TARGET_URL}' );|; s|define\(\s*'WP_SITEURL',\s*'[^']*'\s*\);|define( 'WP_SITEURL', '${TARGET_URL}' );|" "$CONFIG_FILE" || warning "Failed to update wp-config.php"
else
warning "wp-config.php not found or not writable; skipped WP_HOME / WP_SITEURL update."
fi
# Purge LiteSpeed Cache (if plugin installed)
info "Attempting LiteSpeed Cache purge (wp litespeed-purge all)…"
LS_CMD=()
if [[ -n "$SUDO_CMD" ]]; then
# shellcheck disable=SC2206
LS_CMD=($SUDO_CMD)
fi
LS_CMD+=("$WP_EXECUTABLE" "litespeed-purge" "all" "${WP_RUN_ARGS[@]}")
"${LS_CMD[@]}" || warning "LiteSpeed purge returned non-zero exit status (plugin may be inactive)."
# Delete all transients recommended after domain/URL migration
info "Deleting all transients (wp transient delete --all)..."
@ -193,9 +254,6 @@ if [[ $STATUS -eq 0 ]]; then
fi
FLUSH_CMD+=("$WP_EXECUTABLE" "cache" "flush" "${WP_RUN_ARGS[@]}")
"${FLUSH_CMD[@]}" || warning "Cache flush command returned non-zero exit status."
else
error_exit "The 'wp search-replace' command failed with exit code $STATUS."
fi
exit $STATUS