diff --git a/modules/website/wordpress/wordpress-cron-manager.sh b/modules/website/wordpress/wordpress-cron-manager.sh index abb1ad9..97943b4 100755 --- a/modules/website/wordpress/wordpress-cron-manager.sh +++ b/modules/website/wordpress/wordpress-cron-manager.sh @@ -45,6 +45,33 @@ generate_staggered_cron() { echo "$minutes * * * *" } +# 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) + user=$(extract_user_from_path "$site_path") + ;; + interworx) + user=$(extract_user_from_path "$site_path") + ;; + 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}') + [ -z "$user" ] && user="www-data" # Plesk fallback + ;; + *) + user="www-data" # Standalone fallback + ;; + esac + + echo "$user" +} + # Function to safely modify wp-config.php to disable wp-cron # Returns 0 on success, 1 on failure disable_wpcron_in_config() { @@ -163,8 +190,22 @@ case "$choice" in echo "Scanning for WordPress installations..." echo "" - # Find all wp-config.php files in home directories - wp_sites=$(find /home/*/public_html -name "wp-config.php" -type f 2>/dev/null) + # 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}" @@ -176,15 +217,33 @@ case "$choice" in while IFS= read -r config_file; do count=$((count + 1)) - # Extract info + # Extract info - Multi-panel support site_path=$(dirname "$config_file") - user=$(echo "$site_path" | cut -d'/' -f3) - # Try to find domain + # Extract user and domain based on control panel + user="(unknown)" domain="(unknown domain)" - if [ -f "/var/cpanel/userdata/$user/main" ]; then - domain=$(grep -m1 "^servername:" "/var/cpanel/userdata/$user/main" 2>/dev/null | awk '{print $2}') - fi + case "$SYS_CONTROL_PANEL" in + cpanel) + user=$(extract_user_from_path "$site_path") + if [ -f "/var/cpanel/userdata/$user/main" ]; then + domain=$(grep -m1 "^servername:" "/var/cpanel/userdata/$user/main" 2>/dev/null | awk '{print $2}') + fi + ;; + interworx) + user=$(extract_user_from_path "$site_path") + domain=$(echo "$site_path" | cut -d'/' -f4) + ;; + 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="(unknown)" + ;; + *) + user="standalone" + domain="localhost" + ;; + esac # Check if wp-cron is disabled if grep -q "define.*DISABLE_WP_CRON.*true" "$config_file" 2>/dev/null; then @@ -216,45 +275,69 @@ case "$choice" in exit 0 fi - # Find WordPress installation for this domain + # Find WordPress installation for this domain - Multi-panel support echo "" echo "Searching for WordPress installation for $domain..." - # Try to find via cPanel user data - # Search both main_domain (in main files) and servername (in domain files) wp_config="" - # Method 1: Check main_domain in /var/cpanel/userdata/*/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: If not found, search all domain-specific files for servername - if [ -z "$wp_config" ]; then - for userdata_file in /var/cpanel/userdata/*/*; 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 + case "$SYS_CONTROL_PANEL" in + cpanel) + # Method 1: Check main_domain in /var/cpanel/userdata/*/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: If not found, search all domain-specific files for servername + if [ -z "$wp_config" ]; then + for userdata_file in /var/cpanel/userdata/*/*; 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 - 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" @@ -295,8 +378,8 @@ case "$choice" in site_path=$(dirname "$wp_config") cron_cmd="cd $site_path && /usr/bin/php -q wp-cron.php >/dev/null 2>&1" - # Add to user's crontab - user=$(echo "$site_path" | cut -d'/' -f3) + # Add to user's crontab - Multi-panel support + user=$(extract_user_from_path "$site_path") # Check if cron job already exists if crontab -u "$user" -l 2>/dev/null | grep -q "$site_path.*wp-cron.php"; then @@ -410,7 +493,22 @@ case "$choice" in total=0 converted=0 - wp_configs=$(find /home/*/public_html -name "wp-config.php" -type f 2>/dev/null) + # 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}" @@ -421,7 +519,7 @@ case "$choice" in while IFS= read -r wp_config; do total=$((total + 1)) site_path=$(dirname "$wp_config") - user=$(echo "$site_path" | cut -d'/' -f3) + user=$(extract_user_from_path "$site_path") echo -e "${BOLD}Processing:${NC} $site_path (user: $user)" @@ -535,7 +633,7 @@ case "$choice" in # Check for cron job site_path=$(dirname "$wp_config") - user=$(echo "$site_path" | cut -d'/' -f3) + 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}" @@ -669,9 +767,9 @@ case "$choice" in echo -e "${YELLOW}⚠${NC} DISABLE_WP_CRON not found or already enabled" fi - # Remove cron job + # Remove cron job - Multi-panel support site_path=$(dirname "$wp_config") - user=$(echo "$site_path" | cut -d'/' -f3) + user=$(extract_user_from_path "$site_path") if crontab -u "$user" -l 2>/dev/null | grep -q "$site_path.*wp-cron.php"; then crontab -u "$user" -l 2>/dev/null | grep -v "$site_path.*wp-cron.php" | crontab -u "$user" - @@ -770,7 +868,22 @@ case "$choice" in total=0 reverted=0 - wp_configs=$(find /home/*/public_html -name "wp-config.php" -type f 2>/dev/null) + # 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}" @@ -781,7 +894,7 @@ case "$choice" in while IFS= read -r wp_config; do total=$((total + 1)) site_path=$(dirname "$wp_config") - user=$(echo "$site_path" | cut -d'/' -f3) + user=$(extract_user_from_path "$site_path") echo -e "${BOLD}Processing:${NC} $site_path (user: $user)"