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}]" 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 --- # --- Execute Command ---
info "Executing 'wp search-replace'..." info "Executing 'wp search-replace'..."
@ -167,36 +186,75 @@ CMD+=("$WP_EXECUTABLE" "search-replace")
CMD+=("${WP_CLI_ARGS[@]}") CMD+=("${WP_CLI_ARGS[@]}")
CMD+=("${WP_RUN_ARGS[@]}") CMD+=("${WP_RUN_ARGS[@]}")
# Run the command and capture exit status for pair in "${TASKS[@]}"; do
"${CMD[@]}" OLD_PART=${pair%%|||*}
STATUS=$? NEW_PART=${pair##*|||}
if [[ $STATUS -eq 0 ]]; then info "Running wp search-replace '$OLD_PART' '$NEW_PART' …"
success "'wp search-replace' command completed successfully." # Build command anew for each iteration
CMD=()
# Delete all transients recommended after domain/URL migration
info "Deleting all transients (wp transient delete --all)..."
TRANS_CMD=()
if [[ -n "$SUDO_CMD" ]]; then if [[ -n "$SUDO_CMD" ]]; then
# shellcheck disable=SC2206 # shellcheck disable=SC2206
TRANS_CMD=($SUDO_CMD) CMD=($SUDO_CMD)
fi fi
TRANS_CMD+=("$WP_EXECUTABLE" "transient" "delete" "--all" "${WP_RUN_ARGS[@]}") CMD+=("$WP_EXECUTABLE" "search-replace" "$OLD_PART" "$NEW_PART" "${WP_RUN_ARGS[@]}")
"${TRANS_CMD[@]}" || warning "Transient delete command returned non-zero exit status."
# Flush object/cache to ensure changes propagate immediately SEARCH_OUTPUT=$( "${CMD[@]}" 2>&1 )
info "Flushing WordPress caches (wp cache flush)..." STATUS=$?
FLUSH_CMD=() printf "%s\n" "$SEARCH_OUTPUT"
if [[ -n "$SUDO_CMD" ]]; then
# shellcheck disable=SC2206 if [[ $STATUS -ne 0 ]]; then
FLUSH_CMD=($SUDO_CMD) error_exit "wp search-replace failed for pattern '$OLD_PART' -> '$NEW_PART' (exit $STATUS)."
fi fi
FLUSH_CMD+=("$WP_EXECUTABLE" "cache" "flush" "${WP_RUN_ARGS[@]}")
"${FLUSH_CMD[@]}" || warning "Cache flush command returned non-zero exit status." 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 else
error_exit "The 'wp search-replace' command failed with exit code $STATUS." warning "wp-config.php not found or not writable; skipped WP_HOME / WP_SITEURL update."
fi 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)..."
TRANS_CMD=()
if [[ -n "$SUDO_CMD" ]]; then
# shellcheck disable=SC2206
TRANS_CMD=($SUDO_CMD)
fi
TRANS_CMD+=("$WP_EXECUTABLE" "transient" "delete" "--all" "${WP_RUN_ARGS[@]}")
"${TRANS_CMD[@]}" || warning "Transient delete command returned non-zero exit status."
# Flush object/cache to ensure changes propagate immediately
info "Flushing WordPress caches (wp cache flush)..."
FLUSH_CMD=()
if [[ -n "$SUDO_CMD" ]]; then
# shellcheck disable=SC2206
FLUSH_CMD=($SUDO_CMD)
fi
FLUSH_CMD+=("$WP_EXECUTABLE" "cache" "flush" "${WP_RUN_ARGS[@]}")
"${FLUSH_CMD[@]}" || warning "Cache flush command returned non-zero exit status."
exit $STATUS exit $STATUS