Files
news-platform/deploy/scripts/wordpress-ensure-public-url.sh
T
Artur Moreira 67f655820a fix(wordpress): broaden proxy detection for WP_HOME inference (BIT-470)
- Treat X-Forwarded-Proto or X-Real-IP as sufficient proxy hints alongside X-Forwarded-For
- Normalize WORDPRESS_PUBLIC_URL in ensure-public-url script (trailing slash, any localhost:8080 substring)
- Update deployment.md and wordpress README

Made-with: Cursor
2026-03-24 19:47:35 +00:00

57 lines
1.8 KiB
Bash
Executable File

#!/usr/bin/env bash
# On the BitReport LXC (/opt/news-platform), ensure wordpress/.env has a public
# WORDPRESS_PUBLIC_URL so Docker and wp_options sync are aligned with NPM HTTPS.
#
# Exits: 0 = no change, 2 = .env was updated (caller should recreate wordpress).
# No-op on developer machines (APP_DIR != /opt/news-platform).
set -euo pipefail
APP_DIR="${APP_DIR:-/opt/news-platform}"
WP_DIR="$APP_DIR/wordpress"
DOMAIN_MARKER="$APP_DIR/config/domains/newsproject.bitreport.pt.json"
TARGET_URL="https://newsproject.bitreport.pt"
log() { echo "[wordpress-ensure-public-url] $*"; }
if [ "$APP_DIR" != "/opt/news-platform" ]; then
exit 0
fi
if [ ! -f "$DOMAIN_MARKER" ] || [ ! -f "$WP_DIR/.env" ]; then
exit 0
fi
current=""
if grep -qE '^[[:space:]]*WORDPRESS_PUBLIC_URL=' "$WP_DIR/.env"; then
current=$(grep -E '^[[:space:]]*WORDPRESS_PUBLIC_URL=' "$WP_DIR/.env" | tail -n1 | cut -d= -f2- | tr -d '"' | tr -d "'" | tr -d '\r' | sed 's/^[[:space:]]*//;s/[[:space:]]*$//')
fi
current="${current%/}"
need_update=0
if [ -z "$current" ]; then
need_update=1
elif [ "$current" = "http://localhost:8080" ] || [ "$current" = "http://127.0.0.1:8080" ]; then
need_update=1
elif [[ "$current" == *"localhost:8080"* ]] || [[ "$current" == *"127.0.0.1:8080"* ]]; then
need_update=1
fi
if [ "$need_update" -eq 0 ]; then
exit 0
fi
log "Setting WORDPRESS_PUBLIC_URL=$TARGET_URL in wordpress/.env"
ts="$(date +%Y%m%d%H%M%S)"
cp "$WP_DIR/.env" "$WP_DIR/.env.bak.${ts}"
tmp="${WP_DIR}/.env.new.$$"
if grep -qE '^[[:space:]]*WORDPRESS_PUBLIC_URL=' "$WP_DIR/.env"; then
sed -E "s|^[[:space:]]*WORDPRESS_PUBLIC_URL=.*|WORDPRESS_PUBLIC_URL=${TARGET_URL}|" "$WP_DIR/.env" > "$tmp"
mv "$tmp" "$WP_DIR/.env"
else
printf '\nWORDPRESS_PUBLIC_URL=%s\n' "$TARGET_URL" >> "$WP_DIR/.env"
fi
exit 2