diff --git a/scripts/wp-search-replace.sh b/scripts/wp-search-replace.sh index 1510a6d..ff3adf6 100644 --- a/scripts/wp-search-replace.sh +++ b/scripts/wp-search-replace.sh @@ -224,26 +224,34 @@ if [[ -f "$CONFIG_FILE" && -w "$CONFIG_FILE" ]]; then TARGET_URL="https://$TARGET_URL" info "Added https:// prefix to URL: $TARGET_URL" fi - # shellcheck disable=SC2016 # we want literal single quotes inside sed replacement - # Replace existing definitions if they exist -sed -i -E "s|define\(\s*'WP_HOME'\s*,\s*'[^']*'\s*\);|define( 'WP_HOME', '${TARGET_URL}' );|; s|define\(\s*'WP_SITEURL'\s*,\s*'[^']*'\s*\);|define( 'WP_SITEURL', '${TARGET_URL}' );|; s|\$_SERVER\['HTTP_HOST'\]\s*=\s*'[^']*';|\$_SERVER['HTTP_HOST'] = '${HOST_ONLY}';|" "$CONFIG_FILE" || warning "Failed to update wp-config.php" + # Derive host-only from TARGET_URL for HTTP_HOST usage + HOST_ONLY="${TARGET_URL#*://}" + HOST_ONLY="${HOST_ONLY%%/*}" + [[ -z "$HOST_ONLY" ]] && HOST_ONLY="$TARGET_URL" + # shellcheck disable=SC2016 # we want literal quotes inside sed replacement + # Replace existing definitions (handle both single and double quotes) +sed -i -E "s|define\(\s*['\"]WP_HOME['\"]\s*,\s*['\"][^'\"]*['\"]\s*\);|define( 'WP_HOME', '${TARGET_URL}' );|; s|define\(\s*['\"]WP_SITEURL['\"]\s*,\s*['\"][^'\"]*['\"]\s*\);|define( 'WP_SITEURL', '${TARGET_URL}' );|; s|\$_SERVER\[(\"|')HTTP_HOST\1\]\s*=\s*(\"|')[^"']*\2;|\$_SERVER['HTTP_HOST'] = '${HOST_ONLY}';|" "$CONFIG_FILE" || warning "Failed to update wp-config.php" -# If WP_HOME wasn't present, insert block above /* That's all */ -if ! grep -q "WP_HOME" "$CONFIG_FILE"; then - info "Inserting WP_HOME and WP_SITEURL constants into wp-config.php …" - awk -v url="$TARGET_URL" -v host="$HOST_ONLY" ' - BEGIN{inserted=0} - /\/\* That\x27s all/{ - print "\n// Added by wp-search-replace.sh"; - print "define( \"WP_HOME\", \"" url "\" );"; - print "define( \"WP_SITEURL\", \"" url "\" );"; - print "if ( defined( \"WP_CLI\" ) && WP_CLI && ! isset( $_SERVER[\"HTTP_HOST\"] ) ) { $_SERVER[\"HTTP_HOST\"] = \"" host "\"; }"; - inserted=1; - } - {print} - END{if(!inserted) exit 1} - ' "$CONFIG_FILE" > "$CONFIG_FILE.tmp" && mv "$CONFIG_FILE.tmp" "$CONFIG_FILE" -fi + # Determine which entries are missing to avoid duplicates + needs_home=1; grep -Eq "define\(\s*['\"]WP_HOME['\"]" "$CONFIG_FILE" && needs_home=0 || true + needs_siteurl=1; grep -Eq "define\(\s*['\"]WP_SITEURL['\"]" "$CONFIG_FILE" && needs_siteurl=0 || true + needs_http_host=1; grep -Eq "\$_SERVER\[(\"|')HTTP_HOST\1\]\s*=" "$CONFIG_FILE" && needs_http_host=0 || true + + if [[ $needs_home -eq 1 || $needs_siteurl -eq 1 || $needs_http_host -eq 1 ]]; then + info "Inserting missing wp-config.php directives (WP_HOME/WP_SITEURL/HTTP_HOST) …" + awk -v url="$TARGET_URL" -v host="$HOST_ONLY" -v insert_home="$needs_home" -v insert_siteurl="$needs_siteurl" -v insert_hostfix="$needs_http_host" ' + BEGIN{inserted=0} + /\/\* That\x27s all/{ + print "\n// Added by wp-search-replace.sh"; + if (insert_home == 1) print "define( \"WP_HOME\", \"" url "\" );"; + if (insert_siteurl == 1) print "define( \"WP_SITEURL\", \"" url "\" );"; + if (insert_hostfix == 1) print "if ( defined( \"WP_CLI\" ) && WP_CLI && ! isset( $_SERVER[\"HTTP_HOST\"] ) ) { $_SERVER[\"HTTP_HOST\"] = \"" host "\"; }"; + inserted=1; + } + {print} + END{if(!inserted) exit 1} + ' "$CONFIG_FILE" > "$CONFIG_FILE.tmp" && mv "$CONFIG_FILE.tmp" "$CONFIG_FILE" || warning "Could not insert directives into wp-config.php" + fi else warning "wp-config.php not found or not writable; skipped WP_HOME / WP_SITEURL update." fi