From e89317141de0187eab5df75173bafba46f96cf02 Mon Sep 17 00:00:00 2001 From: cschantz Date: Fri, 7 Nov 2025 17:07:33 -0500 Subject: [PATCH] Add safe wp-config.php modification with validation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Critical Safety Improvements: - Prevent duplicate DISABLE_WP_CRON entries - Detect and modify existing definitions (commented or not) - Automatic rollback on failure - Verification of changes before committing Safety Function Features: ✅ Checks file exists and is writable before modification ✅ Detects existing DISABLE_WP_CRON (even if set to false) ✅ Modifies existing line instead of adding duplicate ✅ Ignores commented lines when detecting existing definitions ✅ Creates temporary backup (.wpbak) during modification ✅ Verifies change was successful after modification ✅ Automatically restores backup if verification fails ✅ Removes temporary backup only on success Prevents Issues: ❌ No duplicate define() statements ❌ No syntax errors from malformed sed commands ❌ No broken wp-config.php files ❌ No accumulation of multiple entries on repeated runs Error Handling: - Returns 0 on success, 1 on failure - Calling code can gracefully handle failures - User feedback when modification fails - Skips sites that fail instead of breaking entire batch 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude --- modules/website/wordpress-cron-manager.sh | 79 +++++++++++++++++------ 1 file changed, 61 insertions(+), 18 deletions(-) diff --git a/modules/website/wordpress-cron-manager.sh b/modules/website/wordpress-cron-manager.sh index 39500d8..cc5c535 100755 --- a/modules/website/wordpress-cron-manager.sh +++ b/modules/website/wordpress-cron-manager.sh @@ -45,6 +45,48 @@ generate_staggered_cron() { echo "$minutes * * * *" } +# Function to safely modify wp-config.php to disable wp-cron +# Returns 0 on success, 1 on failure +disable_wpcron_in_config() { + local wp_config="$1" + + # Check if file exists and is writable + if [ ! -f "$wp_config" ] || [ ! -w "$wp_config" ]; then + return 1 + fi + + # Check if DISABLE_WP_CRON already exists (not commented) + if grep -E "^[^/]*define\s*\(\s*['\"]DISABLE_WP_CRON['\"]" "$wp_config" >/dev/null 2>&1; then + # Line exists, modify it to set true + sed -i.wpbak "s/^\([^/]*\)define\s*(\s*['\"]DISABLE_WP_CRON['\"]\s*,\s*[^)]*)/\1define('DISABLE_WP_CRON', true)/" "$wp_config" + else + # Line doesn't exist, add it after /dev/null 2>&1; then + # Remove backup if successful + rm -f "${wp_config}.wpbak" + return 0 + else + # Restore backup if verification failed + if [ -f "${wp_config}.wpbak" ]; then + mv "${wp_config}.wpbak" "$wp_config" + fi + return 1 + fi +} + clear print_banner "WordPress Cron Manager" @@ -167,15 +209,15 @@ case "$choice" in cp "$wp_config" "${wp_config}.backup-$(date +%Y%m%d-%H%M%S)" echo -e "${GREEN}✓${NC} Backed up wp-config.php" - # Add DISABLE_WP_CRON to wp-config.php - if grep -q "DISABLE_WP_CRON" "$wp_config" 2>/dev/null; then - # Replace existing - sed -i "s/define.*DISABLE_WP_CRON.*/define('DISABLE_WP_CRON', true);/" "$wp_config" + # Safely disable wp-cron in wp-config.php + if disable_wpcron_in_config "$wp_config"; then + echo -e "${GREEN}✓${NC} Set DISABLE_WP_CRON to true in wp-config.php" else - # Add after opening PHP tag - sed -i "//dev/null echo " • Backed up wp-config.php" - # Disable wp-cron - if grep -q "DISABLE_WP_CRON" "$wp_config" 2>/dev/null; then - sed -i "s/define.*DISABLE_WP_CRON.*/define('DISABLE_WP_CRON', true);/" "$wp_config" + # Safely disable wp-cron + if disable_wpcron_in_config "$wp_config"; then + echo " • Set DISABLE_WP_CRON to true" else - sed -i "//dev/null 2>&1" @@ -313,11 +356,11 @@ case "$choice" in # Backup cp "$wp_config" "${wp_config}.backup-$(date +%Y%m%d-%H%M%S)" 2>/dev/null - # Disable wp-cron - if grep -q "DISABLE_WP_CRON" "$wp_config" 2>/dev/null; then - sed -i "s/define.*DISABLE_WP_CRON.*/define('DISABLE_WP_CRON', true);/" "$wp_config" 2>/dev/null - else - sed -i "//dev/null + # Safely disable wp-cron + if ! disable_wpcron_in_config "$wp_config"; then + echo -e "${YELLOW}⚠ Failed to modify wp-config.php${NC}" + echo "" + continue fi # Add cron job with staggered timing