diff --git a/modules/website/wordpress/wordpress-cron-manager.sh b/modules/website/wordpress/wordpress-cron-manager.sh index d511e37..37f45f8 100755 --- a/modules/website/wordpress/wordpress-cron-manager.sh +++ b/modules/website/wordpress/wordpress-cron-manager.sh @@ -37,6 +37,14 @@ DRY_RUN=false # PHP binary path detection PHP_BIN=$(command -v php 2>/dev/null || echo "/usr/bin/php") +# OPTIMIZATION: Define constants for frequently used strings +# Reduces hardcoded strings scattered throughout script (29+ occurrences) +declare -r WP_CRON_DISABLED_VAR="DISABLE_WP_CRON" +declare -r WP_CONFIG_FILENAME="wp-config.php" +declare -r WP_CRON_FILENAME="$WP_CRON_FILENAME" +declare -r WP_CONFIG_MARKER="stop editing" +declare -r WP_EDIT_START="/dev/null | grep -q "true" && return 0 || return 1 + grep -E "^\s*define\s*\(\s*['\"]$WP_CRON_DISABLED_VAR['\"]" "$wp_config" 2>/dev/null | grep -q "true" && return 0 || return 1 +} + +# OPTIMIZATION: Cleaner alias for disable_wp_cron_exists (more intuitive name) +# Returns 0 if wp-cron is disabled, 1 if enabled +is_wpcron_disabled() { + disable_wp_cron_exists "$1" } # Function to verify user owns the WordPress installation @@ -297,6 +338,17 @@ is_valid_username_format() { return 1 } +# OPTIMIZATION: Build cron command consistently +# Centralizes cron command format (appears 4 times throughout script) +# Returns: cron command string for wp-cron.php execution +build_cron_command() { + local site_path="$1" + + # Standard format: cd to site, run wp-cron.php with PHP + # Redirects both stdout and stderr to /dev/null to keep cron logs clean + echo "cd \"$site_path\" && $PHP_BIN -q $WP_CRON_FILENAME >/dev/null 2>&1" +} + # Function to pre-check all WordPress installations before any modifications # Returns count of valid installations preflight_check() { @@ -494,6 +546,41 @@ extract_user_from_path() { echo "$user" } +# OPTIMIZATION: Helper function to remove DISABLE_WP_CRON from wp-config +# Encapsulates sed pattern for consistency +# Returns 0 on success, 1 on failure +remove_disable_wpcron_from_config() { + local wp_config="$1" + + # Remove any existing DISABLE_WP_CRON lines using extended regex + # Pattern matches: define('DISABLE_WP_CRON', true); + # With flexibility for spacing and quotes + sed -i.wpbak -E '#define[[:space:]]*\([[:space:]]*['\''"]'"$WP_CRON_DISABLED_VAR"'['\''"][[:space:]]*,[[:space:]]*true[[:space:]]*\)#d' "$wp_config" + return $? +} + +# OPTIMIZATION: Helper function to add DISABLE_WP_CRON to wp-config +# Encapsulates sed pattern for consistency +# Returns 0 on success, 1 on failure +add_disable_wpcron_to_config() { + local wp_config="$1" + + # Try to insert before WordPress stop editing comment (proper convention) + if grep -q "$WP_CONFIG_MARKER" "$wp_config" 2>/dev/null; then + sed -i '#'"$WP_CONFIG_MARKER"'#i\ +define('"'"''"$WP_CRON_DISABLED_VAR"''"'"', true);' "$wp_config" + return 0 + elif grep -q "$WP_EDIT_START" "$wp_config"; then + # Fallback: if no stop editing found, add after opening PHP tag + sed -i '#'"$WP_EDIT_START"'#a\ +define('"'"''"$WP_CRON_DISABLED_VAR"''"'"', true);' "$wp_config" + return 0 + else + # File format is unexpected + return 1 + fi +} + # Function to safely modify wp-config.php to disable wp-cron # Returns 0 on success, 1 on failure disable_wpcron_in_config() { @@ -506,24 +593,15 @@ disable_wpcron_in_config() { # First, remove any existing DISABLE_WP_CRON lines (anywhere in file) # This ensures clean placement even if previously added in wrong location - if grep -q "DISABLE_WP_CRON" "$wp_config" 2>/dev/null; then - # CRITICAL FIX: Use -E flag for extended regex patterns with \s - sed -i.wpbak -E '#define[[:space:]]*\([[:space:]]*['\''"]DISABLE_WP_CRON['\''"][[:space:]]*,[[:space:]]*true[[:space:]]*\)#d' "$wp_config" + if grep -q "$WP_CRON_DISABLED_VAR" "$wp_config" 2>/dev/null; then + remove_disable_wpcron_from_config "$wp_config" || true else # Create backup even if no existing line cp "$wp_config" "${wp_config}.wpbak" fi # Now add it in the proper location - before "stop editing" comment - if grep -q "stop editing" "$wp_config" 2>/dev/null; then - # Add before "stop editing" line (proper WordPress convention) - sed -i '#stop editing#i\ -define('"'"'DISABLE_WP_CRON'"'"', true);' "$wp_config" - elif grep -q "/dev/null 2>&1; then + if grep -E "^[^/]*define\s*\(\s*['\"]$WP_CRON_DISABLED_VAR['\"]\s*,\s*true\s*\)" "$wp_config" >/dev/null 2>&1; then # Remove backup if successful rm -f "${wp_config}.wpbak" return 0 @@ -556,13 +634,12 @@ enable_wpcron_in_config() { fi # Check if DISABLE_WP_CRON exists and is set to true - if grep -E "^[^/]*define[[:space:]]*\([[:space:]]*['\"]DISABLE_WP_CRON['\"][[:space:]]*,[[:space:]]*true[[:space:]]*\)" "$wp_config" >/dev/null 2>&1; then - # Remove or comment out the line - # CRITICAL FIX: Use -E flag for extended regex patterns and proper character classes - sed -i.wpbak -E '#^[^/]*define[[:space:]]*\([[:space:]]*['\''"]DISABLE_WP_CRON['\''"][[:space:]]*,[[:space:]]*true[[:space:]]*\)#d' "$wp_config" + if grep -E "^[^/]*define[[:space:]]*\([[:space:]]*['\"]$WP_CRON_DISABLED_VAR['\"][[:space:]]*,[[:space:]]*true[[:space:]]*\)" "$wp_config" >/dev/null 2>&1; then + # Remove the line using helper function + remove_disable_wpcron_from_config "$wp_config" || true # Verify removal was successful - if ! grep -E "^[^/]*define\s*\(\s*['\"]DISABLE_WP_CRON['\"]\s*,\s*true\s*\)" "$wp_config" >/dev/null 2>&1; then + if ! grep -E "^[^/]*define\s*\(\s*['\"]$WP_CRON_DISABLED_VAR['\"]\s*,\s*true\s*\)" "$wp_config" >/dev/null 2>&1; then rm -f "${wp_config}.wpbak" return 0 else @@ -931,7 +1008,7 @@ case "$choice" in echo -e "${RED}✗${NC} Could not determine site path" exit 1 fi - cron_cmd="cd \"$site_path\" && $PHP_BIN -q wp-cron.php >/dev/null 2>&1" + cron_cmd=$(build_cron_command "$site_path") # Check if cron job already exists (for duplicate prevention) if cron_job_exists "$user" "$site_path"; then @@ -1053,7 +1130,7 @@ case "$choice" in fi # Add cron job with staggered timing - cron_cmd="cd \"$site_path\" && $PHP_BIN -q wp-cron.php >/dev/null 2>&1" + cron_cmd=$(build_cron_command "$site_path") # Check if PHP binary is available if [ ! -x "$PHP_BIN" ]; then @@ -1182,7 +1259,7 @@ case "$choice" in fi # Add cron job with staggered timing - cron_cmd="cd \"$site_path\" && $PHP_BIN -q wp-cron.php >/dev/null 2>&1" + cron_cmd=$(build_cron_command "$site_path") if ! cron_job_exists "$user" "$site_path"; then cron_time=$(generate_staggered_cron) @@ -1321,11 +1398,11 @@ case "$choice" in site_path=$(dirname "$wp_config") user=$(extract_user_from_path "$site_path") - if crontab -u "$user" -l 2>/dev/null | grep -q "wp-cron.php"; then + if crontab -u "$user" -l 2>/dev/null | grep -q "$WP_CRON_FILENAME"; then echo -e "System cron: ${GREEN}CONFIGURED${NC}" echo "" echo "Cron jobs:" - crontab -u "$user" -l 2>/dev/null | grep "wp-cron.php" + crontab -u "$user" -l 2>/dev/null | grep "$WP_CRON_FILENAME" else echo -e "System cron: ${RED}NOT CONFIGURED${NC}" fi @@ -1385,8 +1462,8 @@ case "$choice" in # Show cron jobs echo -e "${BOLD}Cron Jobs:${NC}" - if crontab -u "$check_user" -l 2>/dev/null | grep -q "wp-cron.php"; then - crontab -u "$check_user" -l 2>/dev/null | grep "wp-cron.php" + if crontab -u "$check_user" -l 2>/dev/null | grep -q "$WP_CRON_FILENAME"; then + crontab -u "$check_user" -l 2>/dev/null | grep "$WP_CRON_FILENAME" else echo " No wp-cron jobs found" fi @@ -1591,7 +1668,7 @@ case "$choice" in if [ "$DRY_RUN" = "true" ]; then echo "[DRY-RUN] Would remove all wp-cron jobs for user $target_user" else - if safe_remove_cron_jobs "$target_user" "wp-cron.php"; then + if safe_remove_cron_jobs "$target_user" "$WP_CRON_FILENAME"; then echo -e "${GREEN}✓${NC} Removed all wp-cron jobs from user crontab" fi fi @@ -1694,8 +1771,8 @@ case "$choice" in else for user_home in /home/*; do user=$(basename "$user_home") - if crontab -u "$user" -l 2>/dev/null | grep -q "wp-cron.php"; then - if safe_remove_cron_jobs "$user" "wp-cron.php"; then + if crontab -u "$user" -l 2>/dev/null | grep -q "$WP_CRON_FILENAME"; then + if safe_remove_cron_jobs "$user" "$WP_CRON_FILENAME"; then echo " • Removed cron jobs for user: $user" fi fi