From f671993002b1954b0483d0bc3d0d15247e983efa Mon Sep 17 00:00:00 2001 From: Anthony Date: Wed, 6 Aug 2025 22:46:20 +0800 Subject: [PATCH] Added wp-config.php update --- scripts/wp-search-replace.sh | 100 +++++++++++++++++++++++++++-------- 1 file changed, 79 insertions(+), 21 deletions(-) diff --git a/scripts/wp-search-replace.sh b/scripts/wp-search-replace.sh index 041b040..ba7e8ef 100644 --- a/scripts/wp-search-replace.sh +++ b/scripts/wp-search-replace.sh @@ -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,36 +186,75 @@ 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." - - # Delete all transients – recommended after domain/URL migration - info "Deleting all transients (wp transient delete --all)..." - TRANS_CMD=() + info "Running wp search-replace '$OLD_PART' '$NEW_PART' …" + # Build command anew for each iteration + CMD=() if [[ -n "$SUDO_CMD" ]]; then # shellcheck disable=SC2206 - TRANS_CMD=($SUDO_CMD) + 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." + CMD+=("$WP_EXECUTABLE" "search-replace" "$OLD_PART" "$NEW_PART" "${WP_RUN_ARGS[@]}") - # 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) + 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 - 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 - 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 +# 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