Skip to content

Commit 68e4f36

Browse files
CopilotPDowney
andauthored
feat: add shared fetch_wp_salts() with retry/validation; use in both vhost-install and vhost-import
Agent-Logs-Url: https://github.com/EngineScript/EngineScript/sessions/29220c6a-26cb-4309-8b44-e054afd721f5 Co-authored-by: PDowney <[email protected]>
1 parent f052bb1 commit 68e4f36

4 files changed

Lines changed: 36 additions & 17 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@ Changes are organized by date, with the most recent changes listed first.
1111
- Updated the single-zip database file detection in `scripts/functions/vhost/vhost-import.sh` to search for both `*.sql` and `*.sql.gz` patterns, so compressed database dumps are correctly found and imported instead of failing silently.
1212
- Removed the duplicate `URL_VALIDATION_REGEX` argument from the `prompt_input` call for the Site URL field; validation is now handled exclusively by the subsequent `validate_url` function call, eliminating redundant logic.
1313
- Added `DB_CHARSET="${DB_CHARSET_VALIDATED}"` after the charset `case` statement so `DB_CHARSET` is always the validated, lowercase value when used in the `wp-config.php` `sed` replacement, preventing a potential mismatch if the user supplies a mixed-case charset.
14-
- Improved the WordPress salt fetch error message to also suggest retrying the import later if the WordPress.org API is temporarily unavailable, rather than only mentioning local network/firewall issues.
1514
- Simplified `run_url_search_replace_if_present` to run `wp search-replace` directly with `--report-changed-only`, removing the preliminary `wp db search` pre-check that unnecessarily doubled full-table scans on large databases.
15+
- Extracted WordPress salt generation into a new shared `fetch_wp_salts()` function in `scripts/functions/shared/enginescript-shared-vhost.sh`, replacing the divergent inline implementations in both `vhost-install.sh` and `vhost-import.sh`.
16+
- `fetch_wp_salts()` retries up to 5 times with a 15-second delay between attempts, validates each response contains the expected `define(` content, and only hard-fails with a clear error message after all attempts are exhausted—preventing a transient WordPress.org API outage from permanently breaking a new install or import.
1617

1718
## 2026-04-10
1819

scripts/functions/shared/enginescript-shared-vhost.sh

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -717,6 +717,38 @@ configure_wpconfig_settings() {
717717
}
718718

719719

720+
#----------------------------------------------------------------------------------
721+
# Fetch WordPress salts from api.wordpress.org and apply them to wp-config.php.
722+
# Retries up to 5 times with a 15-second delay between attempts so that a
723+
# transient WordPress.org API outage does not permanently break the install.
724+
fetch_wp_salts() {
725+
local wp_config_path="$1"
726+
local max_attempts=5
727+
local retry_delay=15
728+
local salt=""
729+
local attempt
730+
731+
echo "Generating new WordPress salts..."
732+
for (( attempt=1; attempt<=max_attempts; attempt++ )); do
733+
salt=$(curl --fail --silent --show-error --location --connect-timeout 10 --max-time 30 \
734+
"https://api.wordpress.org/secret-key/1.1/salt/" 2>/dev/null)
735+
if [[ -n "${salt}" ]] && printf '%s' "${salt}" | grep -q "define("; then
736+
local string='put your unique phrase here'
737+
printf '%s\n' "g/${string}/d" a "${salt}" . w | ed -s "${wp_config_path}"
738+
echo "WordPress salts applied successfully."
739+
return 0
740+
fi
741+
if [[ ${attempt} -lt ${max_attempts} ]]; then
742+
echo "Warning: Failed to fetch valid WordPress salts (attempt ${attempt}/${max_attempts}). Retrying in ${retry_delay} seconds..." >&2
743+
sleep "${retry_delay}"
744+
fi
745+
done
746+
747+
echo "Error: Failed to fetch valid WordPress salts from api.wordpress.org after ${max_attempts} attempts. Please check your internet connection and DNS/firewall/proxy settings. If the issue persists, the WordPress.org API may be temporarily unavailable—please retry the import later." >&2
748+
exit 1
749+
}
750+
751+
720752
#----------------------------------------------------------------------------------
721753
# Create robots.txt file
722754
create_robots_txt() {

scripts/functions/vhost/vhost-import.sh

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -556,19 +556,7 @@ sed -i \
556556
configure_redis "${SITE_URL}" "${TARGET_WP_PATH}/wp-config.php"
557557

558558
# WP Salt Creation (Generate new salts)
559-
echo "Generating new WordPress salts..."
560-
SALT=$(curl --fail --silent --show-error --location --retry 3 --connect-timeout 10 --max-time 30 "https://api.wordpress.org/secret-key/1.1/salt/") || {
561-
echo "Error: Failed to fetch WordPress salts from api.wordpress.org. Please check your internet connection and DNS/firewall/proxy settings. If the issue persists, the WordPress.org API may be temporarily unavailable—please retry the import later." >&2
562-
exit 1
563-
}
564-
565-
if [ -z "${SALT}" ] || ! printf '%s' "${SALT}" | grep -q "define("; then
566-
echo "Error: Retrieved invalid WordPress salts content" >&2
567-
exit 1
568-
fi
569-
570-
STRING='put your unique phrase here'
571-
printf '%s\n' "g/$STRING/d" a "$SALT" . w | ed -s "${TARGET_WP_PATH}/wp-config.php"
559+
fetch_wp_salts "${TARGET_WP_PATH}/wp-config.php"
572560

573561
# Configure wp-config.php settings
574562
configure_wpconfig_settings "${SITE_URL}" "${TARGET_WP_PATH}/wp-config.php"

scripts/functions/vhost/vhost-install.sh

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -195,9 +195,7 @@ if [[ "${INSTALL_WORDPRESS}" == "1" ]]; then
195195
configure_redis "${SITE_URL}" "/var/www/sites/${SITE_URL}/html/wp-config.php"
196196

197197
# WP Salt Creation
198-
SALT=$(curl -L https://api.wordpress.org/secret-key/1.1/salt/)
199-
STRING='put your unique phrase here'
200-
printf '%s\n' "g/$STRING/d" a "$SALT" . w | ed -s "/var/www/sites/${SITE_URL}/html/wp-config.php"
198+
fetch_wp_salts "/var/www/sites/${SITE_URL}/html/wp-config.php"
201199

202200
# Configure wp-config.php settings
203201
configure_wpconfig_settings "${SITE_URL}" "/var/www/sites/${SITE_URL}/html/wp-config.php"

0 commit comments

Comments
 (0)