diff --git a/modules/website/wordpress/wordpress-cron-manager.sh b/modules/website/wordpress/wordpress-cron-manager.sh index e5edda5..f22f866 100755 --- a/modules/website/wordpress/wordpress-cron-manager.sh +++ b/modules/website/wordpress/wordpress-cron-manager.sh @@ -440,23 +440,26 @@ safe_add_cron_job() { local cron_time="$2" local cron_cmd="$3" - # Check if user has valid crontab access - if ! crontab -u "$user" -l >/dev/null 2>&1; then - # User might not have a crontab yet - try creating one + # OPTIMIZATION: Single crontab -l call instead of 3 separate calls + # Check existence, read content, and validate in one operation + local current_crontab + current_crontab=$(crontab -u "$user" -l 2>/dev/null) + local crontab_exit=$? + + if [ $crontab_exit -ne 0 ]; then + # User doesn't have a crontab yet - try creating one echo "# WordPress cron jobs" | crontab -u "$user" - 2>/dev/null || return 1 + current_crontab="# WordPress cron jobs" fi # CRITICAL FIX: Check if exact job already exists to prevent duplicates - if crontab -u "$user" -l 2>/dev/null | grep -qF "$cron_cmd"; then + if echo "$current_crontab" | grep -qF "$cron_cmd"; then # Job already exists, don't add duplicate return 0 fi # Add the job to crontab - # CRITICAL: Must verify crontab -l succeeded before piping back - # If crontab -l fails, we would pipe only the new job, losing existing jobs! - local current_crontab - current_crontab=$(crontab -u "$user" -l 2>/dev/null) || return 1 + # CRITICAL: crontab -l already verified to have succeeded above (echo "$current_crontab"; echo "$cron_time $cron_cmd") | crontab -u "$user" - 2>/dev/null return $? } @@ -467,16 +470,18 @@ safe_remove_cron_jobs() { local user="$1" local pattern="$2" # Pattern to match jobs to remove - # Check if crontab exists and contains the pattern - if ! crontab -u "$user" -l 2>/dev/null | grep -q "$pattern"; then + # OPTIMIZATION: Single crontab -l call instead of 2 separate calls + local current_crontab + current_crontab=$(crontab -u "$user" -l 2>/dev/null) || return 0 + + # Check if pattern exists in crontab + if ! echo "$current_crontab" | grep -q "$pattern"; then # Pattern not found - nothing to remove return 0 fi # Remove jobs matching pattern - # CRITICAL: Must verify crontab -l succeeded before piping back - local current_crontab - current_crontab=$(crontab -u "$user" -l 2>/dev/null) || return 1 + # CRITICAL: crontab -l already verified to have succeeded above echo "$current_crontab" | grep -v "$pattern" | crontab -u "$user" - 2>/dev/null return $? } @@ -1586,13 +1591,21 @@ disable_wpcron_in_config() { return 1 fi - # First, remove any existing DISABLE_WP_CRON lines (anywhere in file) + # First, always create a backup before any modifications + # BUG FIX: Create backup regardless of whether DISABLE_WP_CRON exists + if ! cp "$wp_config" "${wp_config}.wpbak" 2>/dev/null; then + print_warning "Could not create backup of $wp_config" + return 1 + fi + + # Remove any existing DISABLE_WP_CRON lines (anywhere in file) # This ensures clean placement even if previously added in wrong location 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" + if ! remove_disable_wpcron_from_config "$wp_config"; then + # Restore backup if removal failed + mv "${wp_config}.wpbak" "$wp_config" + return 1 + fi fi # Now add it in the proper location - before "stop editing" comment