#!/bin/bash ################################################################################ # WordPress Cron Manager ################################################################################ # Purpose: Disable wp-cron and convert to real system cron jobs # Features: # - Detect all WordPress installations # - Disable DISABLE_WP_CRON in wp-config.php # - Add proper cron jobs for scheduled tasks # - Server-wide, per-user, or per-domain operations ################################################################################ SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/../../.." && pwd)" [ -f "$SCRIPT_DIR/lib/common-functions.sh" ] && source "$SCRIPT_DIR/lib/common-functions.sh" || { echo "ERROR: common-functions.sh not found" >&2; exit 1; } [ -f "$SCRIPT_DIR/lib/system-detect.sh" ] && source "$SCRIPT_DIR/lib/system-detect.sh" || { echo "ERROR: system-detect.sh not found" >&2; exit 1; } if [ "$EUID" -ne 0 ]; then print_error "This script must be run as root" exit 1 fi # Lock file to prevent concurrent execution (ephemeral, removed on exit) LOCK_FILE="/tmp/wordpress-cron-manager.lock" exec 9>"$LOCK_FILE" if ! flock -n 9; then print_error "Another instance of this script is already running" exit 1 fi trap 'flock -u 9; rm -f "$LOCK_FILE"' EXIT INT TERM # Dry-run mode support DRY_RUN=false [[ "$1" == "--dry-run" ]] && DRY_RUN=true # PHP binary path detection PHP_BIN=$(command -v php 2>/dev/null || echo "/usr/bin/php") # Global counter for staggering cron times CRON_OFFSET=0 # Function to safely add cron job to user's crontab # Returns 0 on success, 1 on failure safe_add_cron_job() { local user="$1" 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 echo "# WordPress cron jobs" | crontab -u "$user" - 2>/dev/null || return 1 fi # Add the job to crontab (crontab -u "$user" -l 2>/dev/null; echo "$cron_time $cron_cmd") | crontab -u "$user" - 2>/dev/null return $? } # Function to safely remove cron jobs from user's crontab # Returns 0 on success, 1 on failure 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 # Pattern not found - nothing to remove return 0 fi # Remove jobs matching pattern crontab -u "$user" -l 2>/dev/null | grep -v "$pattern" | crontab -u "$user" - 2>/dev/null return $? } # Function to validate wp-config.php syntax before and after modification # Returns 0 if valid, 1 if syntax error detected validate_wp_config_syntax() { local wp_config="$1" # Check if file exists and is readable [ ! -f "$wp_config" ] && return 1 [ ! -r "$wp_config" ] && return 1 # Validate PHP syntax using php -l if available if command -v php >/dev/null 2>&1; then if ! php -l "$wp_config" >/dev/null 2>&1; then return 1 fi fi # Additional checks: ensure file has opening /dev/null; then return 1 fi return 0 } # Function to check if cron job already exists for a specific site # Returns 0 if exists, 1 if not found cron_job_exists() { local user="$1" local site_path="$2" crontab -u "$user" -l 2>/dev/null | grep -qF "$site_path" && return 0 || return 1 } # Function to check if DISABLE_WP_CRON already exists in wp-config # Returns 0 if exists, 1 if not found # Excludes commented-out lines to avoid false positives disable_wp_cron_exists() { local wp_config="$1" # Look for uncommented define() call with DISABLE_WP_CRON and true grep -E "^\s*define\s*\(\s*['\"]DISABLE_WP_CRON['\"]" "$wp_config" 2>/dev/null | grep -q "true" && return 0 || return 1 } # Function to verify user owns the WordPress installation # Returns 0 if user matches, 1 if mismatch verify_user_ownership() { local user="$1" local wp_config="$2" # Get actual owner of file local actual_owner=$(stat -c '%U' "$wp_config" 2>/dev/null || echo "") if [ -z "$actual_owner" ]; then # stat failed, try alternative method actual_owner=$(ls -l "$wp_config" 2>/dev/null | awk '{print $3}') fi # Verify user matches if [ "$user" = "$actual_owner" ]; then return 0 else return 1 fi } # Function to check if user exists and can receive crontab entries # Returns 0 if valid, 1 if not user_is_valid() { local user="$1" # Check if user exists in system if ! id "$user" >/dev/null 2>&1; then return 1 fi # Check if user can be used with crontab (not system user like root) # Should have a real home directory local home_dir=$(getent passwd "$user" | cut -d: -f6) if [ -z "$home_dir" ] || [ "$home_dir" = "/dev/null" ]; then return 1 fi return 0 } # Function to pre-check all WordPress installations before any modifications # Returns count of valid installations preflight_check() { local panel="$1" local found_count=0 local issues_count=0 echo "" echo "Running pre-flight checks..." echo "" # Find all wp-config.php files based on panel type local wp_configs="" case "$panel" in cpanel) wp_configs=$(find /home/*/public_html -name "wp-config.php" -type f 2>/dev/null) ;; interworx) wp_configs=$(find /home/*/*/html -name "wp-config.php" -type f 2>/dev/null) ;; plesk) wp_configs=$(find /var/www/vhosts/*/httpdocs -name "wp-config.php" -type f 2>/dev/null) ;; *) wp_configs=$(find /var/www/html -name "wp-config.php" -type f 2>/dev/null) ;; esac if [ -z "$wp_configs" ]; then echo -e "${YELLOW}No WordPress installations found${NC}" return 0 fi while IFS= read -r wp_config; do found_count=$((found_count + 1)) site_path=$(dirname "$wp_config") user=$(extract_user_from_path "$site_path") # Verify user is valid if ! user_is_valid "$user"; then echo -e "${RED}✗${NC} Site $found_count: Invalid user '$user' ($site_path)" issues_count=$((issues_count + 1)) continue fi # Verify user owns the WordPress install if ! verify_user_ownership "$user" "$wp_config"; then echo -e "${YELLOW}⚠${NC} Site $found_count: User mismatch - extracted='$user', owner=$(stat -c '%U' "$wp_config" 2>/dev/null) ($site_path)" issues_count=$((issues_count + 1)) continue fi # Validate wp-config.php syntax if ! validate_wp_config_syntax "$wp_config"; then echo -e "${RED}✗${NC} Site $found_count: Invalid wp-config.php syntax ($site_path)" issues_count=$((issues_count + 1)) continue fi echo -e "${GREEN}✓${NC} Site $found_count: $site_path (user: $user)" done <<< "$wp_configs" echo "" echo "Pre-flight check complete:" echo " Total found: $found_count" echo " Issues: $issues_count" echo " Ready: $((found_count - issues_count))" return $((found_count - issues_count)) } # Function to display detailed status of all WordPress installations # Shows current state before any changes show_installation_status() { local panel="$1" echo "" echo "Current WordPress Installation Status:" echo "" local wp_configs="" case "$panel" in cpanel) wp_configs=$(find /home/*/public_html -name "wp-config.php" -type f 2>/dev/null) ;; interworx) wp_configs=$(find /home/*/*/html -name "wp-config.php" -type f 2>/dev/null) ;; plesk) wp_configs=$(find /var/www/vhosts/*/httpdocs -name "wp-config.php" -type f 2>/dev/null) ;; *) wp_configs=$(find /var/www/html -name "wp-config.php" -type f 2>/dev/null) ;; esac if [ -z "$wp_configs" ]; then echo "No WordPress installations found" return 1 fi local count=0 while IFS= read -r wp_config; do count=$((count + 1)) site_path=$(dirname "$wp_config") user=$(extract_user_from_path "$site_path") # Check wp-cron status if disable_wp_cron_exists "$wp_config"; then cron_status="${GREEN}Disabled${NC} (system cron)" else cron_status="${YELLOW}Enabled${NC} (default)" fi # Check if cron job exists if cron_job_exists "$user" "$site_path"; then cron_job="${GREEN}Yes${NC}" else cron_job="${YELLOW}No${NC}" fi echo "$count. ${BOLD}$site_path${NC}" echo " User: $user" echo " WP-Cron: $cron_status" echo " System Cron Job: $cron_job" echo "" done <<< "$wp_configs" echo "Total installations: $count" } # Function to create timestamped backup of wp-config.php # Returns 0 on success, 1 on failure # Also returns the backup filename create_timestamped_backup() { local wp_config="$1" local backup_timestamp=$(date +%Y%m%d-%H%M%S) local backup_file="${wp_config}.backup-${backup_timestamp}" # Verify source file exists if [ ! -f "$wp_config" ]; then return 1 fi # Create backup if cp "$wp_config" "$backup_file" 2>/dev/null; then echo "$backup_file" return 0 else return 1 fi } # Function to generate staggered cron time # Distributes jobs across 60 minutes to avoid load spikes generate_staggered_cron() { local minute=$((CRON_OFFSET % 60)) # Create pattern: every hour at staggered minutes # Site 1: minute 0, Site 2: minute 1, etc. # Increment offset for next site (wraps at 60) CRON_OFFSET=$((CRON_OFFSET + 1)) echo "$minute * * * *" } # Function to extract user from WordPress site path # Multi-panel aware extract_user_from_path() { local site_path="$1" local user="" case "$SYS_CONTROL_PANEL" in cpanel) # Extract user from /home/username/public_html pattern user=$(echo "$site_path" | awk -F'/' '{print $3}') ;; interworx) # Extract user from /home/username/domain/html pattern user=$(echo "$site_path" | awk -F'/' '{print $3}') ;; plesk) # Extract domain from path and lookup user local domain=$(echo "$site_path" | grep -oE '/vhosts/[^/]+' | sed 's|/vhosts/||') user=$(plesk bin subscription --info "$domain" 2>/dev/null | grep "Owner" | awk '{print $2}') ;; *) user="www-data" ;; esac # CRITICAL FIX: Validate user is not empty - prevents adding cron to wrong user if [ -z "$user" ]; then user="www-data" # Fallback only if extraction completely failed fi echo "$user" } # 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 # 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" 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 # 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 } # Function to safely re-enable wp-cron (revert changes) # Returns 0 on success, 1 on failure enable_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 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" # Verify removal was successful if ! grep -E "^[^/]*define\s*\(\s*['\"]DISABLE_WP_CRON['\"]\s*,\s*true\s*\)" "$wp_config" >/dev/null 2>&1; then rm -f "${wp_config}.wpbak" return 0 else # Restore backup if removal failed if [ -f "${wp_config}.wpbak" ]; then mv "${wp_config}.wpbak" "$wp_config" fi return 1 fi else # DISABLE_WP_CRON not found or already disabled return 0 fi } clear print_banner "WordPress Cron Manager" echo "" echo -e "${BOLD}What would you like to do?${NC}" echo "" echo -e "${GREEN}Enable System Cron:${NC}" echo -e " ${CYAN}1)${NC} Scan for WordPress installations" echo -e " ${CYAN}2)${NC} Disable wp-cron for specific domain" echo -e " ${CYAN}3)${NC} Disable wp-cron for specific user (all their WP sites)" echo -e " ${CYAN}4)${NC} Disable wp-cron server-wide (all WordPress sites)" echo "" echo -e "${YELLOW}Revert to WP-Cron:${NC}" echo -e " ${CYAN}6)${NC} Re-enable wp-cron for specific domain" echo -e " ${CYAN}7)${NC} Re-enable wp-cron for specific user (all their WP sites)" echo -e " ${CYAN}8)${NC} Re-enable wp-cron server-wide (all WordPress sites)" echo "" echo -e "${CYAN}Status & Information:${NC}" echo -e " ${CYAN}5)${NC} Check wp-cron status for domain/user" echo -e " ${CYAN}9)${NC} Run pre-flight checks (validate all installations)" echo -e " ${CYAN}10)${NC} Show detailed status of all WordPress sites" echo "" echo -e " ${RED}0)${NC} Return to menu" echo "" echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━" echo -n "Select option [0]: " # Validate choice input while true; do read -r choice choice="${choice:-0}" if ! [[ "$choice" =~ ^([0-9]|10)$ ]]; then echo "" print_error "Invalid choice. Please enter 0-10" echo "" continue fi break done case "$choice" in 1) # Scan for WordPress installations echo "" print_banner "WordPress Installation Scanner" echo "" echo "Scanning for WordPress installations..." echo "" # Find all wp-config.php files - Multi-panel support wp_sites="" case "$SYS_CONTROL_PANEL" in cpanel) wp_sites=$(find /home/*/public_html -name "wp-config.php" -type f 2>/dev/null) ;; interworx) wp_sites=$(find /home/*/*/html -name "wp-config.php" -type f 2>/dev/null) ;; plesk) wp_sites=$(find /var/www/vhosts/*/httpdocs -name "wp-config.php" -type f 2>/dev/null) ;; *) wp_sites=$(find /var/www/html -name "wp-config.php" -type f 2>/dev/null) ;; esac if [ -z "$wp_sites" ]; then echo -e "${YELLOW}No WordPress installations found${NC}" else count=0 echo -e "${BOLD}Found WordPress Installations:${NC}" echo "" while IFS= read -r config_file; do count=$((count + 1)) # Extract info - Multi-panel support site_path=$(dirname "$config_file") # Extract user and domain based on control panel user="$(extract_user_from_path "$site_path")" domain="" case "$SYS_CONTROL_PANEL" in cpanel) userdata_dir="${SYS_CPANEL_USERDATA_DIR:-/var/cpanel/userdata}" if [ -f "$userdata_dir/$user/main" ]; then domain=$(grep -m1 "^servername:" "$userdata_dir/$user/main" 2>/dev/null | awk '{print $2}') fi # CRITICAL FIX: Fallback if domain extraction failed if [ -z "$domain" ]; then domain="$user.local" # Use user@hostname fallback fi ;; interworx) domain=$(echo "$site_path" | cut -d'/' -f4) # CRITICAL FIX: Fallback if domain extraction failed if [ -z "$domain" ]; then domain="$user.local" # Use user@hostname fallback fi ;; plesk) domain=$(echo "$site_path" | grep -oE '/vhosts/[^/]+' | sed 's|/vhosts/||') user=$(plesk bin subscription --info "$domain" 2>/dev/null | grep "Owner" | awk '{print $2}') [ -z "$user" ] && user="plesk-user" ;; *) user="standalone" domain="localhost" ;; esac # Final validation: ensure neither is empty (prevents display issues) [ -z "$user" ] && user="unknown" [ -z "$domain" ] && domain="unknown-domain" # Check if wp-cron is disabled if grep -q "define.*DISABLE_WP_CRON.*true" "$config_file" 2>/dev/null; then status="${GREEN}✓ Disabled (using system cron)${NC}" else status="${YELLOW}⚠ Enabled (default wp-cron)${NC}" fi echo -e "${count}. ${BOLD}$domain${NC}" echo " Path: $site_path" echo " User: $user" echo " Status: $status" echo "" done <<< "$wp_sites" echo -e "${CYAN}Total WordPress installations: $count${NC}" fi ;; 2) # Disable wp-cron for specific domain echo "" echo -n "Enter domain name (or 0 to cancel): " read -r domain if [ -z "$domain" ] || [ "$domain" = "0" ]; then echo "Operation cancelled." press_enter exit 0 fi # Find WordPress installation for this domain - Multi-panel support echo "" echo "Searching for WordPress installation for $domain..." wp_config="" case "$SYS_CONTROL_PANEL" in cpanel) # Method 1: Check main_domain in /var/cpanel/userdata/*/main files userdata_base="${SYS_CPANEL_USERDATA_DIR:-/var/cpanel/userdata}" for userdata_file in "$userdata_base"/*/main; do if grep -q "^main_domain: $domain" "$userdata_file" 2>/dev/null; then user=$(basename "$(dirname "$userdata_file")") potential_config="/home/$user/public_html/wp-config.php" if [ -f "$potential_config" ]; then wp_config="$potential_config" break fi fi done # Method 2: If not found, search all domain-specific files for servername if [ -z "$wp_config" ]; then for userdata_file in "$userdata_base"/*/*; do # Skip cache files and main files [[ "$userdata_file" == *.cache ]] && continue [[ "$userdata_file" == */main ]] && continue [[ "$userdata_file" == */cache ]] && continue [[ "$userdata_file" == */cache.json ]] && continue if grep -q "^servername: $domain" "$userdata_file" 2>/dev/null; then user=$(basename "$(dirname "$userdata_file")") potential_config="/home/$user/public_html/wp-config.php" if [ -f "$potential_config" ]; then wp_config="$potential_config" break fi fi done fi ;; interworx) # Find user from vhost config user=$(grep -l "ServerName ${domain}" /etc/httpd/conf.d/vhost_*.conf 2>/dev/null | head -1 | \ xargs grep "SuexecUserGroup" 2>/dev/null | awk '{print $2}') if [ -n "$user" ]; then potential_config="/home/${user}/${domain}/html/wp-config.php" [ -f "$potential_config" ] && wp_config="$potential_config" fi ;; plesk) # Try standard Plesk path potential_config="/var/www/vhosts/${domain}/httpdocs/wp-config.php" [ -f "$potential_config" ] && wp_config="$potential_config" ;; *) # Standalone - try standard path potential_config="/var/www/html/wp-config.php" [ -f "$potential_config" ] && wp_config="$potential_config" ;; esac if [ -z "$wp_config" ]; then print_error "WordPress installation not found for $domain" press_enter exit 1 fi echo -e "${GREEN}Found WordPress:${NC} $wp_config" echo "" # Extract site path and user site_path=$(dirname "$wp_config") user=$(extract_user_from_path "$site_path") # PRE-FLIGHT VALIDATION CHECKS echo "Running pre-flight validation checks..." echo "" # Check 1: Validate user if ! user_is_valid "$user"; then print_error "User '$user' is not valid or not a cPanel user" press_enter exit 1 fi echo -e "${GREEN}✓${NC} User valid: $user" # Check 2: Verify user ownership if ! verify_user_ownership "$user" "$wp_config"; then actual_owner=$(stat -c '%U' "$wp_config" 2>/dev/null || ls -l "$wp_config" | awk '{print $3}') echo -e "${YELLOW}⚠${NC} User mismatch detected:" echo " Extracted user: $user" echo " Actual owner: $actual_owner" echo "" echo -n "Continue anyway? (y/n) [n]: " read -r confirm if [ "$confirm" != "y" ] && [ "$confirm" != "Y" ]; then press_enter exit 0 fi else echo -e "${GREEN}✓${NC} User ownership verified" fi # Check 3: Validate wp-config.php syntax BEFORE any changes if ! validate_wp_config_syntax "$wp_config"; then print_error "wp-config.php has syntax errors - cannot modify" echo " Please fix syntax issues first" press_enter exit 1 fi echo -e "${GREEN}✓${NC} wp-config.php syntax is valid" # Check 4: Check for existing DISABLE_WP_CRON if disable_wp_cron_exists "$wp_config"; then echo -e "${YELLOW}wp-cron is already disabled for this site${NC}" echo "" echo -n "Re-configure anyway? (y/n) [n]: " read -r confirm if [ "$confirm" != "y" ] && [ "$confirm" != "Y" ]; then press_enter exit 0 fi else echo -e "${GREEN}✓${NC} wp-cron currently enabled (will be disabled)" fi # Check 5: Check for existing cron job if cron_job_exists "$user" "$site_path"; then echo -e "${YELLOW}⚠${NC} System cron job already exists for this site" echo "" echo -n "Update existing cron job? (y/n) [n]: " read -r confirm if [ "$confirm" != "y" ] && [ "$confirm" != "Y" ]; then press_enter exit 0 fi else echo -e "${GREEN}✓${NC} No existing system cron job" fi echo "" echo "All validation checks passed. Ready to make changes..." echo "" # CREATE BACKUP WITH TIMESTAMP echo -e "${BOLD}BACKUP CREATION:${NC}" backup_file=$(create_timestamped_backup "$wp_config") if [ $? -ne 0 ] || [ -z "$backup_file" ]; then print_error "Failed to create backup of wp-config.php" echo " Cannot proceed without backup" press_enter exit 1 fi echo -e "${GREEN}✓${NC} Backup created successfully" echo -e "${CYAN}Location:${NC} $backup_file" echo -e "${CYAN}Timestamp:${NC} $(date '+%Y-%m-%d %H:%M:%S')" echo "" # User confirmation to proceed with modification echo -e "${YELLOW}IMPORTANT:${NC} This will modify your wp-config.php file" echo "" echo -n "Proceed with modification? (y/n) [y]: " read -r confirm if [ "$confirm" = "n" ] || [ "$confirm" = "N" ]; then echo "Operation cancelled. Backup preserved at: $backup_file" press_enter exit 0 fi echo "" echo "Modifying wp-config.php..." echo "" # 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" # CRITICAL: Verify syntax after modification if ! validate_wp_config_syntax "$wp_config"; then print_error "CRITICAL: wp-config.php syntax became invalid after modification!" echo " Restoring backup..." if cp "$backup_file" "$wp_config"; then echo -e "${GREEN}✓${NC} Restored from backup: $backup_file" echo "" echo "Your original wp-config.php has been restored." echo "Backup (with attempted modification) kept at: ${backup_file}.failed" cp "$backup_file" "${backup_file}.failed" else print_error "CRITICAL: Could not restore from backup!" echo "Original backup location: $backup_file" fi press_enter exit 1 fi echo -e "${GREEN}✓${NC} wp-config.php syntax verified after modification" else print_error "Failed to modify wp-config.php" echo " Please check file permissions and syntax" press_enter exit 1 fi # Add cron job with staggered timing if [ -z "$site_path" ]; then 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" # Check if cron job already exists (for duplicate prevention) if cron_job_exists "$user" "$site_path"; then # Remove old one first to avoid duplicates safe_remove_cron_jobs "$user" "$site_path.*wp-cron.php" echo -e "${YELLOW}⚠${NC} Removed existing cron job (updating)" fi # Generate staggered cron time and add to crontab cron_time=$(generate_staggered_cron) if safe_add_cron_job "$user" "$cron_time" "$cron_cmd"; then echo -e "${GREEN}✓${NC} Added cron job ($cron_time)" else echo -e "${YELLOW}⚠${NC} Failed to add cron job" fi echo "" print_success "WordPress cron converted to system cron for $domain" echo "" echo "Changes made:" echo " • DISABLE_WP_CRON set to true in wp-config.php" echo " • System cron job added (runs once per hour)" echo " • Backup saved: ${wp_config}.backup-*" ;; 3) # Disable wp-cron for specific user echo "" echo -n "Enter cPanel username (or 0 to cancel): " read -r target_user if [ -z "$target_user" ] || [ "$target_user" = "0" ]; then echo "Operation cancelled." press_enter exit 0 fi if [ ! -d "/home/$target_user" ]; then print_error "User $target_user does not exist" press_enter exit 1 fi echo "" echo "Searching for WordPress installations for user: $target_user" echo "" wp_configs=$(find "/home/$target_user" -name "wp-config.php" -type f 2>/dev/null) if [ -z "$wp_configs" ]; then print_error "No WordPress installations found for $target_user" press_enter exit 1 fi count=0 converted=0 failed=0 while IFS= read -r wp_config; do count=$((count + 1)) site_path=$(dirname "$wp_config") # Validate site path if [ -z "$site_path" ] || [ ! -d "$site_path" ]; then echo -e "${YELLOW}Warning: Invalid site path${NC}" failed=$((failed + 1)) continue fi # Extract user from site path (per-site, not using $target_user assumption) user=$(extract_user_from_path "$site_path") if [ -z "$user" ]; then echo -e "${YELLOW}Warning: Could not extract username from $site_path${NC}" failed=$((failed + 1)) continue fi echo -e "${BOLD}Site $count:${NC} $site_path" # Create timestamped backup backup_file=$(create_timestamped_backup "$wp_config") if [ -z "$backup_file" ]; then echo " • ${YELLOW}Warning: Backup failed, skipping site${NC}" failed=$((failed + 1)) echo "" continue fi echo " • Backed up wp-config.php" # Safely disable wp-cron if [ "$DRY_RUN" = "true" ]; then echo " [DRY-RUN] Would modify wp-config.php" else if ! disable_wpcron_in_config "$wp_config"; then echo " • ${RED}Error: Could not modify wp-config.php${NC}" [ -f "$backup_file" ] && cp "$backup_file" "$wp_config" 2>/dev/null failed=$((failed + 1)) echo "" continue fi fi echo " • Set DISABLE_WP_CRON to true" # Validate syntax after modification if [ "$DRY_RUN" != "true" ] && ! validate_wp_config_syntax "$wp_config"; then echo " • ${RED}Error: wp-config.php syntax invalid after modification${NC}" cp "$backup_file" "$wp_config" 2>/dev/null [ -f "$backup_file" ] && cp "$backup_file" "${backup_file}.failed" 2>/dev/null failed=$((failed + 1)) echo "" continue fi # Add cron job with staggered timing cron_cmd="cd \"$site_path\" && $PHP_BIN -q wp-cron.php >/dev/null 2>&1" # Check if PHP binary is available if [ ! -x "$PHP_BIN" ]; then echo " • ${RED}Error: PHP binary not found at $PHP_BIN${NC}" failed=$((failed + 1)) echo "" continue fi if ! cron_job_exists "$user" "$site_path"; then cron_time=$(generate_staggered_cron) if [ "$DRY_RUN" = "true" ]; then echo " [DRY-RUN] Would add cron job ($cron_time)" else if safe_add_cron_job "$user" "$cron_time" "$cron_cmd"; then echo " • Added cron job ($cron_time)" converted=$((converted + 1)) else echo " • ${RED}Error: Failed to add cron job${NC}" failed=$((failed + 1)) fi fi else echo " • Cron job already exists" fi echo "" done <<< "$wp_configs" if [ "$DRY_RUN" = "true" ]; then echo -e "${CYAN}[DRY-RUN] Would have converted $count site(s)${NC}" else print_success "$converted WordPress sites for $target_user converted to system cron" if [ $failed -gt 0 ]; then print_warning "$failed site(s) failed or were skipped" fi fi ;; 4) # Server-wide conversion echo "" echo -e "${RED}${BOLD}WARNING: Server-Wide wp-cron Conversion${NC}" echo "" echo "This will:" echo " • Find ALL WordPress installations on the server" echo " • Disable wp-cron in each wp-config.php" echo " • Add system cron jobs for each user" echo "" echo -n "Are you sure? Type 'yes' to confirm: " read -r confirm if [ "$confirm" != "yes" ]; then echo "Cancelled" press_enter exit 0 fi echo "" echo "Scanning entire server for WordPress installations..." echo "" total=0 converted=0 failed=0 # Find all wp-config.php files - Multi-panel support wp_configs="" case "$SYS_CONTROL_PANEL" in cpanel) wp_configs=$(find /home/*/public_html -name "wp-config.php" -type f 2>/dev/null) ;; interworx) wp_configs=$(find /home/*/*/html -name "wp-config.php" -type f 2>/dev/null) ;; plesk) wp_configs=$(find /var/www/vhosts/*/httpdocs -name "wp-config.php" -type f 2>/dev/null) ;; *) wp_configs=$(find /var/www/html -name "wp-config.php" -type f 2>/dev/null) ;; esac if [ -z "$wp_configs" ]; then echo -e "${YELLOW}No WordPress installations found${NC}" press_enter exit 0 fi while IFS= read -r wp_config; do total=$((total + 1)) site_path=$(dirname "$wp_config") if [ -z "$site_path" ]; then echo -e "${RED}✗ Could not determine site path${NC}" failed=$((failed + 1)) continue fi user=$(extract_user_from_path "$site_path") echo -e "${BOLD}Processing:${NC} $site_path (user: $user)" # Create timestamped backup backup_file=$(create_timestamped_backup "$wp_config") if [ -z "$backup_file" ]; then echo " ${RED}✗ Backup failed, skipping${NC}" failed=$((failed + 1)) echo "" continue fi # Safely disable wp-cron if [ "$DRY_RUN" = "true" ]; then echo " [DRY-RUN] Would modify wp-config.php" else if ! disable_wpcron_in_config "$wp_config"; then echo -e "${RED}✗ Failed to modify wp-config.php${NC}" [ -f "$backup_file" ] && cp "$backup_file" "$wp_config" 2>/dev/null failed=$((failed + 1)) echo "" continue fi fi # Validate syntax after modification if [ "$DRY_RUN" != "true" ] && ! validate_wp_config_syntax "$wp_config"; then echo " ${RED}✗ wp-config.php syntax invalid after modification${NC}" cp "$backup_file" "$wp_config" 2>/dev/null [ -f "$backup_file" ] && cp "$backup_file" "${backup_file}.failed" 2>/dev/null failed=$((failed + 1)) echo "" continue fi # Check PHP binary if [ ! -x "$PHP_BIN" ]; then echo " ${RED}✗ PHP binary not found at $PHP_BIN${NC}" failed=$((failed + 1)) echo "" continue fi # Add cron job with staggered timing cron_cmd="cd \"$site_path\" && $PHP_BIN -q wp-cron.php >/dev/null 2>&1" if ! cron_job_exists "$user" "$site_path"; then cron_time=$(generate_staggered_cron) if [ "$DRY_RUN" = "true" ]; then echo " [DRY-RUN] Would add cron job ($cron_time)" else if safe_add_cron_job "$user" "$cron_time" "$cron_cmd"; then echo " Cron: $cron_time" converted=$((converted + 1)) else echo " ${RED}✗ Failed to add cron job${NC}" failed=$((failed + 1)) fi fi fi if [ "$DRY_RUN" != "true" ]; then echo -e "${GREEN}✓${NC} Converted" fi echo "" done <<< "$wp_configs" echo "" if [ "$DRY_RUN" = "true" ]; then echo -e "${CYAN}[DRY-RUN] Would convert up to $total site(s)${NC}" else print_success "Server-wide conversion complete" echo "" echo "Summary:" echo " • Total WordPress sites found: $total" echo " • Successfully converted: $converted" if [ $failed -gt 0 ]; then echo -e " • ${RED}Failed or skipped: $failed${NC}" fi fi ;; 5) # Check status echo "" echo "Check wp-cron status for:" echo -e " ${CYAN}1)${NC} Specific domain" echo -e " ${CYAN}2)${NC} Specific user" echo -e " ${RED}0)${NC} Cancel" echo "" echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━" # Validate check_choice input while true; do echo -n "Select [1]: " read -r check_choice check_choice="${check_choice:-1}" if ! [[ "$check_choice" =~ ^[0-2]$ ]]; then echo "" print_error "Invalid choice. Please enter 0, 1, or 2" echo "" continue fi break done if [ "$check_choice" = "0" ]; then echo "Operation cancelled." press_enter exit 0 elif [ "$check_choice" = "1" ]; then echo "" echo -n "Enter domain name (or 0 to cancel): " read -r domain if [ -z "$domain" ] || [ "$domain" = "0" ]; then echo "Operation cancelled." press_enter exit 0 fi # Find WordPress for domain wp_config="" # Method 1: Check main_domain in main files userdata_base="${SYS_CPANEL_USERDATA_DIR:-/var/cpanel/userdata}" for userdata_file in "$userdata_base"/*/main; do if grep -q "^main_domain: $domain" "$userdata_file" 2>/dev/null; then user=$(basename "$(dirname "$userdata_file")") potential_config="/home/$user/public_html/wp-config.php" if [ -f "$potential_config" ]; then wp_config="$potential_config" break fi fi done # Method 2: Search domain-specific files for servername if [ -z "$wp_config" ]; then for userdata_file in "$userdata_base"/*/*; do [[ "$userdata_file" == *.cache ]] && continue [[ "$userdata_file" == */main ]] && continue [[ "$userdata_file" == */cache ]] && continue [[ "$userdata_file" == */cache.json ]] && continue if grep -q "^servername: $domain" "$userdata_file" 2>/dev/null; then user=$(basename "$(dirname "$userdata_file")") potential_config="/home/$user/public_html/wp-config.php" if [ -f "$potential_config" ]; then wp_config="$potential_config" break fi fi done fi if [ -z "$wp_config" ]; then print_error "WordPress not found for $domain" press_enter exit 1 fi echo "" echo -e "${BOLD}WordPress Cron Status for $domain${NC}" echo "" echo "Config file: $wp_config" echo "" if grep -q "define.*DISABLE_WP_CRON.*true" "$wp_config" 2>/dev/null; then echo -e "wp-cron: ${GREEN}DISABLED${NC} (using system cron)" # Check for cron job 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 echo -e "System cron: ${GREEN}CONFIGURED${NC}" echo "" echo "Cron jobs:" crontab -u "$user" -l 2>/dev/null | grep "wp-cron.php" else echo -e "System cron: ${RED}NOT CONFIGURED${NC}" fi else echo -e "wp-cron: ${YELLOW}ENABLED${NC} (default WordPress cron)" echo "" echo "Recommendation: Disable wp-cron and use system cron for better performance" fi else echo "" echo -n "Enter cPanel username (or 0 to cancel): " read -r check_user if [ -z "$check_user" ] || [ "$check_user" = "0" ]; then echo "Operation cancelled." press_enter exit 0 fi if [ ! -d "/home/$check_user" ]; then print_error "User $check_user does not exist" press_enter exit 1 fi echo "" echo -e "${BOLD}WordPress Cron Status for user: $check_user${NC}" echo "" wp_configs=$(find "/home/$check_user" -name "wp-config.php" -type f 2>/dev/null) if [ -z "$wp_configs" ]; then echo "No WordPress installations found" else count=0 while IFS= read -r wp_config; do count=$((count + 1)) site_path=$(dirname "$wp_config") echo -e "${count}. ${BOLD}$site_path${NC}" if grep -q "define.*DISABLE_WP_CRON.*true" "$wp_config" 2>/dev/null; then echo " wp-cron: ${GREEN}DISABLED${NC}" else echo " wp-cron: ${YELLOW}ENABLED${NC}" fi echo "" done <<< "$wp_configs" # 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" else echo " No wp-cron jobs found" fi fi fi ;; 6) # Re-enable wp-cron for specific domain echo "" echo -n "Enter domain name (or 0 to cancel): " read -r domain if [ -z "$domain" ] || [ "$domain" = "0" ]; then echo "Operation cancelled." press_enter exit 0 fi # Find WordPress installation wp_config="" # Method 1: Check main_domain in main files for userdata_file in /var/cpanel/userdata/*/main; do if grep -q "^main_domain: $domain" "$userdata_file" 2>/dev/null; then user=$(basename "$(dirname "$userdata_file")") potential_config="/home/$user/public_html/wp-config.php" if [ -f "$potential_config" ]; then wp_config="$potential_config" break fi fi done # Method 2: Search domain-specific files for servername if [ -z "$wp_config" ]; then for userdata_file in /var/cpanel/userdata/*/*; do [[ "$userdata_file" == *.cache ]] && continue [[ "$userdata_file" == */main ]] && continue [[ "$userdata_file" == */cache ]] && continue [[ "$userdata_file" == */cache.json ]] && continue if grep -q "^servername: $domain" "$userdata_file" 2>/dev/null; then user=$(basename "$(dirname "$userdata_file")") potential_config="/home/$user/public_html/wp-config.php" if [ -f "$potential_config" ]; then wp_config="$potential_config" break fi fi done fi if [ -z "$wp_config" ]; then print_error "WordPress installation not found for $domain" press_enter exit 1 fi echo -e "${GREEN}Found WordPress:${NC} $wp_config" echo "" # Create timestamped backup backup_file=$(create_timestamped_backup "$wp_config") if [ -z "$backup_file" ]; then print_error "Backup failed, aborting" press_enter exit 1 fi echo -e "${GREEN}✓${NC} Backed up wp-config.php" # Re-enable wp-cron if [ "$DRY_RUN" = "true" ]; then echo "[DRY-RUN] Would remove DISABLE_WP_CRON from wp-config.php" else if ! enable_wpcron_in_config "$wp_config"; then echo -e "${YELLOW}⚠${NC} DISABLE_WP_CRON not found or already enabled" fi # Validate syntax after modification if ! validate_wp_config_syntax "$wp_config"; then echo -e "${RED}✗${NC} wp-config.php syntax invalid after modification" cp "$backup_file" "$wp_config" 2>/dev/null [ -f "$backup_file" ] && cp "$backup_file" "${backup_file}.failed" 2>/dev/null print_error "Reverted from backup" press_enter exit 1 fi echo -e "${GREEN}✓${NC} Removed DISABLE_WP_CRON from wp-config.php" fi # Remove cron job - Multi-panel support site_path=$(dirname "$wp_config") user=$(extract_user_from_path "$site_path") if [ "$DRY_RUN" = "true" ]; then echo "[DRY-RUN] Would remove cron job from user crontab" else if safe_remove_cron_jobs "$user" "$site_path.*wp-cron.php"; then echo -e "${GREEN}✓${NC} Removed cron job from user crontab" else echo -e "${YELLOW}⚠${NC} Failed to remove cron job" fi fi echo "" if [ "$DRY_RUN" = "true" ]; then echo -e "${CYAN}[DRY-RUN] Would revert WordPress cron to default for $domain${NC}" else print_success "WordPress cron reverted to default for $domain" fi ;; 7) # Re-enable wp-cron for specific user echo "" echo -n "Enter cPanel username (or 0 to cancel): " read -r target_user if [ -z "$target_user" ] || [ "$target_user" = "0" ]; then echo "Operation cancelled." press_enter exit 0 fi if [ ! -d "/home/$target_user" ]; then print_error "User $target_user does not exist" press_enter exit 1 fi echo "" echo "Reverting WordPress installations for user: $target_user" echo "" wp_configs=$(find "/home/$target_user" -name "wp-config.php" -type f 2>/dev/null) if [ -z "$wp_configs" ]; then print_error "No WordPress installations found for $target_user" press_enter exit 1 fi count=0 converted=0 failed=0 while IFS= read -r wp_config; do count=$((count + 1)) site_path=$(dirname "$wp_config") echo -e "${BOLD}Site $count:${NC} $site_path" # Create timestamped backup backup_file=$(create_timestamped_backup "$wp_config") if [ -z "$backup_file" ]; then echo " • ${RED}Backup failed, skipping${NC}" failed=$((failed + 1)) echo "" continue fi echo " • Backed up wp-config.php" # Re-enable wp-cron if [ "$DRY_RUN" = "true" ]; then echo " [DRY-RUN] Would remove DISABLE_WP_CRON" else if ! enable_wpcron_in_config "$wp_config"; then echo " • Already using default wp-cron" fi # Validate syntax after modification if ! validate_wp_config_syntax "$wp_config"; then echo " • ${RED}Syntax error after modification${NC}" cp "$backup_file" "$wp_config" 2>/dev/null [ -f "$backup_file" ] && cp "$backup_file" "${backup_file}.failed" 2>/dev/null failed=$((failed + 1)) echo "" continue fi echo " • Removed DISABLE_WP_CRON" converted=$((converted + 1)) fi echo "" done <<< "$wp_configs" # Remove all wp-cron jobs for this user 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 echo -e "${GREEN}✓${NC} Removed all wp-cron jobs from user crontab" fi fi if [ "$DRY_RUN" = "true" ]; then echo -e "${CYAN}[DRY-RUN] Would revert $count site(s) for $target_user${NC}" else print_success "All WordPress sites for $target_user reverted to default wp-cron" if [ $failed -gt 0 ]; then print_warning "$failed site(s) failed or were skipped" fi fi ;; 8) # Server-wide revert echo "" echo -e "${RED}${BOLD}WARNING: Server-Wide Revert${NC}" echo "" echo "This will:" echo " • Find ALL WordPress installations on the server" echo " • Remove DISABLE_WP_CRON from each wp-config.php" echo " • Remove all wp-cron system cron jobs" echo "" echo -n "Are you sure? Type 'yes' to confirm: " read -r confirm if [ "$confirm" != "yes" ]; then echo "Cancelled" press_enter exit 0 fi echo "" echo "Scanning entire server for WordPress installations..." echo "" total=0 reverted=0 failed=0 # Find all wp-config.php files - Multi-panel support wp_configs="" case "$SYS_CONTROL_PANEL" in cpanel) wp_configs=$(find /home/*/public_html -name "wp-config.php" -type f 2>/dev/null) ;; interworx) wp_configs=$(find /home/*/*/html -name "wp-config.php" -type f 2>/dev/null) ;; plesk) wp_configs=$(find /var/www/vhosts/*/httpdocs -name "wp-config.php" -type f 2>/dev/null) ;; *) wp_configs=$(find /var/www/html -name "wp-config.php" -type f 2>/dev/null) ;; esac if [ -z "$wp_configs" ]; then echo -e "${YELLOW}No WordPress installations found${NC}" press_enter exit 0 fi while IFS= read -r wp_config; do total=$((total + 1)) site_path=$(dirname "$wp_config") if [ -z "$site_path" ]; then echo -e "${RED}✗ Could not determine site path${NC}" failed=$((failed + 1)) continue fi user=$(extract_user_from_path "$site_path") echo -e "${BOLD}Processing:${NC} $site_path (user: $user)" # Create timestamped backup backup_file=$(create_timestamped_backup "$wp_config") if [ -z "$backup_file" ]; then echo " ${RED}✗ Backup failed, skipping${NC}" failed=$((failed + 1)) echo "" continue fi # Re-enable wp-cron if [ "$DRY_RUN" = "true" ]; then echo " [DRY-RUN] Would remove DISABLE_WP_CRON" else if ! enable_wpcron_in_config "$wp_config"; then echo " Already using default wp-cron" fi # Validate syntax after modification if ! validate_wp_config_syntax "$wp_config"; then echo " ${RED}✗ Syntax error after modification${NC}" cp "$backup_file" "$wp_config" 2>/dev/null [ -f "$backup_file" ] && cp "$backup_file" "${backup_file}.failed" 2>/dev/null failed=$((failed + 1)) echo "" continue fi reverted=$((reverted + 1)) echo -e "${GREEN}✓${NC} Reverted" fi echo "" done <<< "$wp_configs" # Remove all wp-cron jobs from all users echo "" echo "Removing wp-cron jobs from user crontabs..." if [ "$DRY_RUN" = "true" ]; then echo "[DRY-RUN] Would remove wp-cron jobs from all users" 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 echo " • Removed cron jobs for user: $user" fi fi done fi echo "" if [ "$DRY_RUN" = "true" ]; then echo -e "${CYAN}[DRY-RUN] Would revert up to $total site(s)${NC}" else print_success "Server-wide revert complete" echo "" echo "Summary:" echo " • Total WordPress sites found: $total" echo " • Successfully reverted: $reverted" if [ $failed -gt 0 ]; then echo -e " • ${RED}Failed or skipped: $failed${NC}" fi fi ;; 9) # Run pre-flight checks echo "" print_banner "Pre-Flight Checks" preflight_check "$SYS_CONTROL_PANEL" ;; 10) # Show detailed status echo "" print_banner "WordPress Installation Status" show_installation_status "$SYS_CONTROL_PANEL" ;; 0) exit 0 ;; *) print_error "Invalid option" ;; esac echo "" press_enter