#\!/bin/bash # Zoho CRM Integration Fix Deployment Script # This script runs the cleanup and then deploys the Zoho fix # Colors for output GREEN='\033[0;32m' RED='\033[0;31m' YELLOW='\033[1;33m' NC='\033[0m' # No Color # Load environment variables source "$(dirname "$0")/../.env" # Check if environment variables are loaded if [ -z "$UPSKILL_STAGING_IP" ] || [ -z "$UPSKILL_STAGING_SSH_USER" ]; then echo -e "${RED}Error: Missing required environment variables${NC}" echo "Please ensure .env file exists and contains UPSKILL_STAGING_IP and UPSKILL_STAGING_SSH_USER" exit 1 fi # Set variables REMOTE_HOST="${UPSKILL_STAGING_IP}" REMOTE_USER="${UPSKILL_STAGING_SSH_USER}" REMOTE_PASS="${UPSKILL_STAGING_PASS}" REMOTE_PATH="/home/974670.cloudwaysapps.com/uberrxmprk/public_html" PLUGINS_PATH="${REMOTE_PATH}/wp-content/plugins" MAIN_PLUGIN="hvac-community-events" echo -e "${YELLOW}=== Zoho CRM Integration Fix Deployment ===${NC}" echo -e "${YELLOW}Target: ${REMOTE_USER}@${REMOTE_HOST}:${PLUGINS_PATH}/${MAIN_PLUGIN}${NC}" # Step 1: Run the cleanup script first echo -e "${YELLOW}Step 1: Running plugin cleanup...${NC}" "$(dirname "$0")/cleanup-hvac-plugins.sh" if [ $? -ne 0 ]; then echo -e "${RED}Error: Plugin cleanup failed. Aborting deployment.${NC}" exit 1 fi echo -e "${GREEN}Plugin cleanup completed successfully.${NC}" # Step 2: Create directories for Zoho files echo -e "${YELLOW}Step 2: Creating required directories...${NC}" sshpass -p "$REMOTE_PASS" ssh -o StrictHostKeyChecking=no "$REMOTE_USER@$REMOTE_HOST" < "$ZOHO_CONFIG" << 'EOPHP' "$DIAGNOSTICS_FILE" << 'EOPHP' "$CSS_FILE" << 'EOCSS' /* Debug Information Styling */ .hvac-zoho-debug-info { margin-top: 15px; padding: 15px; background: #f9f9f9; border: 1px solid #ddd; border-left: 4px solid #dc3232; } .hvac-zoho-debug-info details summary { cursor: pointer; font-weight: bold; color: #0073aa; padding: 5px; background: #f0f0f0; } .hvac-zoho-debug-info pre { margin: 10px 0; padding: 10px; background: #f0f0f0; border: 1px solid #ddd; overflow: auto; max-height: 300px; } EOCSS sshpass -p "$REMOTE_PASS" scp -o StrictHostKeyChecking=no "$CSS_FILE" "$REMOTE_USER@$REMOTE_HOST:$PLUGINS_PATH/$MAIN_PLUGIN/assets/css/zoho-admin.css" if [ $? -ne 0 ]; then echo -e "${RED}Error: Failed to upload CSS file. Aborting deployment.${NC}" rm "$CSS_FILE" exit 1 fi rm "$CSS_FILE" echo -e "${GREEN}CSS file uploaded successfully.${NC}" # Step 6: Update class-zoho-admin.php if it exists echo -e "${YELLOW}Step 6: Updating admin class if it exists...${NC}" # Create a PHP script to update the admin class UPDATE_ADMIN_SCRIPT=$(mktemp) cat > "$UPDATE_ADMIN_SCRIPT" << 'EOPHP' get_modules(); // Enhanced error handling and detailed response if (\$response && \!isset(\$response['error'])) { wp_send_json_success(array( 'message' => 'Connection successful\!', 'modules' => isset(\$response['modules']) ? count(\$response['modules']) . ' modules available' : 'No modules found' )); } else { \$error_message = isset(\$response['error']) ? \$response['error'] : 'Unknown error'; \$error_code = isset(\$response['code']) ? \$response['code'] : ''; \$error_details = isset(\$response['details']) ? \$response['details'] : ''; // Log the error if (defined('ZOHO_DEBUG_MODE') && ZOHO_DEBUG_MODE) { \$log_message = \"[\" . date('Y-m-d H:i:s') . \"] Connection test failed: \$error_message\\n\"; \$log_message .= \"[\" . date('Y-m-d H:i:s') . \"] Error code: \$error_code\\n\"; \$log_message .= \"[\" . date('Y-m-d H:i:s') . \"] Details: \$error_details\\n\"; \$log_message .= \"[\" . date('Y-m-d H:i:s') . \"] Raw response: \" . json_encode(\$response) . \"\\n\"; if (defined('ZOHO_LOG_FILE')) { error_log(\$log_message, 3, ZOHO_LOG_FILE); } } // Send detailed error data back to frontend wp_send_json_error(array( 'message' => 'Connection failed', 'error' => \$error_message, 'code' => \$error_code, 'details' => \$error_details, 'raw' => json_encode(\$response) )); } }"; // Replace the method $content = preg_replace($pattern, $replacement, $content); // Save the updated file if (file_put_contents($admin_path, $content)) { echo "Admin class updated successfully with enhanced error reporting.\n"; } else { echo "Failed to update admin class.\n"; exit(1); } } else { echo "Could not find test_connection method in admin class. Manual update may be required.\n"; } EOPHP sshpass -p "$REMOTE_PASS" scp -o StrictHostKeyChecking=no "$UPDATE_ADMIN_SCRIPT" "$REMOTE_USER@$REMOTE_HOST:$REMOTE_PATH/update-admin-class.php" ADMIN_UPDATE_RESULT=$(sshpass -p "$REMOTE_PASS" ssh -o StrictHostKeyChecking=no "$REMOTE_USER@$REMOTE_HOST" "cd $REMOTE_PATH && php update-admin-class.php") echo "$ADMIN_UPDATE_RESULT" sshpass -p "$REMOTE_PASS" ssh -o StrictHostKeyChecking=no "$REMOTE_USER@$REMOTE_HOST" "rm -f $REMOTE_PATH/update-admin-class.php" rm "$UPDATE_ADMIN_SCRIPT" # Step 7: Update class-zoho-crm-auth.php if it exists echo -e "${YELLOW}Step 7: Updating auth class if it exists...${NC}" # Create a PHP script to update the auth class UPDATE_AUTH_SCRIPT=$(mktemp) cat > "$UPDATE_AUTH_SCRIPT" << 'EOPHP' client_id; } /** * Get client secret * * @return string */ public function get_client_secret() { return \$this->client_secret; } /** * Get refresh token * * @return string */ public function get_refresh_token() { return \$this->refresh_token; } }\$1"; $content = preg_replace($pattern, $replacement, $content); } // Find constructor to add environment loading $pattern = '/public function __construct\(\) {/'; $replacement = "public function __construct() { // Load environment variables from .env if available if (function_exists('load_env_from_dotenv')) { load_env_from_dotenv(); }"; $content = preg_replace($pattern, $replacement, $content); // Save the updated file if (file_put_contents($auth_path, $content)) { echo "Auth class updated successfully to load environment variables.\n"; } else { echo "Failed to update auth class.\n"; exit(1); } EOPHP sshpass -p "$REMOTE_PASS" scp -o StrictHostKeyChecking=no "$UPDATE_AUTH_SCRIPT" "$REMOTE_USER@$REMOTE_HOST:$REMOTE_PATH/update-auth-class.php" AUTH_UPDATE_RESULT=$(sshpass -p "$REMOTE_PASS" ssh -o StrictHostKeyChecking=no "$REMOTE_USER@$REMOTE_HOST" "cd $REMOTE_PATH && php update-auth-class.php") echo "$AUTH_UPDATE_RESULT" sshpass -p "$REMOTE_PASS" ssh -o StrictHostKeyChecking=no "$REMOTE_USER@$REMOTE_HOST" "rm -f $REMOTE_PATH/update-auth-class.php" rm "$UPDATE_AUTH_SCRIPT" # Step 8: Create and upload JS file echo -e "${YELLOW}Step 8: Creating and uploading JS file...${NC}" JS_FILE=$(mktemp) cat > "$JS_FILE" << 'EOJS' jQuery(document).ready(function($) { $('#hvac-zoho-test-connection').on('click', function(e) { e.preventDefault(); var $button = $(this); var $result = $('#hvac-zoho-test-connection-result'); $button.prop('disabled', true); $result.html('

Testing connection...

'); $.ajax({ url: hvac_zoho_admin.ajax_url, type: 'POST', data: { action: 'hvac_zoho_test_connection', nonce: hvac_zoho_admin.nonce }, success: function(response) { $button.prop('disabled', false); $result.empty(); if (response.success) { var successHtml = '
'; successHtml += '

' + response.data.message + '

'; if (response.data.modules) { successHtml += '

' + response.data.modules + '

'; } successHtml += '
'; $result.html(successHtml); } else { $result.html('

Connection test failed. Please check the logs.

'); } }, error: function(response) { $button.prop('disabled', false); $result.empty(); // Create detailed error display var errorHtml = '
'; errorHtml += '

' + (response.data ? response.data.message : 'Connection failed') + ': ' + (response.data ? response.data.error : 'Unknown error') + '

'; // Add error code if available if (response.data && response.data.code) { errorHtml += '

Error Code: ' + response.data.code + '

'; } // Add debugging info errorHtml += '
'; // Add details if available if (response.data && response.data.details) { errorHtml += '

Details: ' + response.data.details + '

'; } // Add raw response data if available if (response.data && response.data.raw) { try { errorHtml += '
'; errorHtml += 'Raw Response Data (click to expand)'; errorHtml += '
' + JSON.stringify(JSON.parse(response.data.raw), null, 2) + '
'; errorHtml += '
'; } catch (e) { errorHtml += '

Raw response data is available but could not be parsed: ' + e.message + '

'; } } errorHtml += '
'; // Close debug info div errorHtml += '
'; // Close notice div $result.html(errorHtml); } }); }); }); EOJS sshpass -p "$REMOTE_PASS" scp -o StrictHostKeyChecking=no "$JS_FILE" "$REMOTE_USER@$REMOTE_HOST:$PLUGINS_PATH/$MAIN_PLUGIN/assets/js/zoho-admin.js" if [ $? -ne 0 ]; then echo -e "${RED}Error: Failed to upload JS file. Aborting deployment.${NC}" rm "$JS_FILE" exit 1 fi rm "$JS_FILE" echo -e "${GREEN}JS file uploaded successfully.${NC}" # Step 9: Clear WordPress cache echo -e "${YELLOW}Step 9: Clearing WordPress cache...${NC}" sshpass -p "$REMOTE_PASS" ssh -o StrictHostKeyChecking=no "$REMOTE_USER@$REMOTE_HOST" "cd $REMOTE_PATH && wp cache flush" # Summary echo -e "${GREEN}=== Zoho CRM Integration Fix Deployed Successfully\! ===${NC}" echo -e "${YELLOW}The following steps were completed:${NC}" echo -e "1. Cleaned up duplicate HVAC plugins" echo -e "2. Created required directories" echo -e "3. Added environment variable loading in zoho-config.php" echo -e "4. Enhanced error reporting in class-zoho-admin.php" echo -e "5. Added getter methods in class-zoho-crm-auth.php" echo -e "6. Created a diagnostics tool for troubleshooting" echo -e "7. Added debug styling to CSS" echo -e "8. Enhanced error display in JS" echo -e "9. Cleared WordPress cache" echo -e "${YELLOW}You can now test the Zoho CRM integration in the WordPress admin panel.${NC}" echo -e "${YELLOW}Go to Events > Zoho CRM Sync and click the 'Test Connection' button.${NC}" echo -e "${YELLOW}If issues persist, run the diagnostics tool at:${NC}" echo -e "${YELLOW}https://wordpress-974670-5399585.cloudwaysapps.com/wp-content/plugins/hvac-community-events/includes/zoho/diagnostics.php?run_diagnostics=true${NC}" exit 0