#!/bin/bash # Simplified Zoho CRM Integration Fix Deployment Script # This script deploys the core Zoho fix files directly # 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" # 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" PLUGIN_PATH="${PLUGINS_PATH}/${MAIN_PLUGIN}" echo -e "${YELLOW}=== Simplified Zoho CRM Integration Fix Deployment ===${NC}" echo -e "${YELLOW}Target: ${REMOTE_USER}@${REMOTE_HOST}:${PLUGIN_PATH}${NC}" # Step 1: Ensure the directories exist echo -e "${YELLOW}Step 1: 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:$PLUGIN_PATH/assets/css/zoho-admin.css" if [ $? -ne 0 ]; then echo -e "${RED}Error: Failed to upload CSS file.${NC}" rm "$CSS_FILE" exit 1 fi rm "$CSS_FILE" echo -e "${GREEN}CSS file uploaded successfully.${NC}" # Step 5: Create the JS file echo -e "${YELLOW}Step 5: Creating 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 { // 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); } }, error: function(response) { $button.prop('disabled', false); $result.empty(); // Create detailed error display for AJAX errors var errorHtml = '
'; errorHtml += '

AJAX Error: The server returned an error or did not respond.

'; // Add raw response if available if (response.responseText) { errorHtml += '
'; errorHtml += '
'; errorHtml += 'Error Response (click to expand)'; errorHtml += '
' + response.responseText + '
'; errorHtml += '
'; errorHtml += '
'; } errorHtml += '
'; $result.html(errorHtml); } }); }); }); EOJS sshpass -p "$REMOTE_PASS" scp -o StrictHostKeyChecking=no "$JS_FILE" "$REMOTE_USER@$REMOTE_HOST:$PLUGIN_PATH/assets/js/zoho-admin.js" if [ $? -ne 0 ]; then echo -e "${RED}Error: Failed to upload JS file.${NC}" rm "$JS_FILE" exit 1 fi rm "$JS_FILE" echo -e "${GREEN}JS file uploaded successfully.${NC}" # Step 6: Create the PHP patch script echo -e "${YELLOW}Step 6: Creating PHP patch script...${NC}" PATCH_SCRIPT=$(mktemp) cat > "$PATCH_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) )); } }'; $modified_admin_content = preg_replace($pattern, $replacement, $admin_content); if ($modified_admin_content !== $admin_content) { file_put_contents($admin_class_path, $modified_admin_content); echo "Enhanced the test_connection method in the admin class\n"; } else { echo "Failed to enhance the test_connection method. Pattern not found.\n"; } } else { echo "The test_connection method was not found in the admin class\n"; } } else { echo "Admin class not found at: $admin_class_path\n"; } // 3. Check for the Auth class $auth_class_path = $plugin_path . '/includes/zoho/class-zoho-crm-auth.php'; if (file_exists($auth_class_path)) { // Create backup copy($auth_class_path, $auth_class_path . '.bak.' . date('YmdHis')); echo "Created backup of auth class\n"; // Read the file $auth_content = file_get_contents($auth_class_path); // Enhance the constructor to load environment variables $constructor_pattern = '/public function __construct\(\) {/'; $constructor_replacement = 'public function __construct() { // Load environment variables from .env if available if (function_exists(\'load_env_from_dotenv\')) { load_env_from_dotenv(); }'; $modified_auth_content = preg_replace($constructor_pattern, $constructor_replacement, $auth_content); // Add getter methods if they don't exist if (strpos($modified_auth_content, 'get_client_id') === false) { $class_end_pattern = '/}(\s*)$/'; $getters = ' /** * Get client ID * * @return string */ public function get_client_id() { return $this->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'; $modified_auth_content = preg_replace($class_end_pattern, $getters, $modified_auth_content); echo "Added getter methods to the auth class\n"; } // Save the modified auth class if ($modified_auth_content !== $auth_content) { file_put_contents($auth_class_path, $modified_auth_content); echo "Enhanced the auth class to load environment variables\n"; } else { echo "Failed to enhance the auth class\n"; } } else { echo "Auth class not found at: $auth_class_path\n"; } echo "Patch completed\n"; EOPHP sshpass -p "$REMOTE_PASS" scp -o StrictHostKeyChecking=no "$PATCH_SCRIPT" "$REMOTE_USER@$REMOTE_HOST:$REMOTE_PATH/hvac-zoho-patch.php" if [ $? -ne 0 ]; then echo -e "${RED}Error: Failed to upload patch script.${NC}" rm "$PATCH_SCRIPT" exit 1 fi # Step 7: Run the patch script echo -e "${YELLOW}Step 7: Running the patch script...${NC}" PATCH_RESULT=$(sshpass -p "$REMOTE_PASS" ssh -o StrictHostKeyChecking=no "$REMOTE_USER@$REMOTE_HOST" "cd $REMOTE_PATH && php hvac-zoho-patch.php") echo "$PATCH_RESULT" # Clean up the patch script sshpass -p "$REMOTE_PASS" ssh -o StrictHostKeyChecking=no "$REMOTE_USER@$REMOTE_HOST" "rm -f $REMOTE_PATH/hvac-zoho-patch.php" rm "$PATCH_SCRIPT" # Step 8: Clear WordPress cache echo -e "${YELLOW}Step 8: 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. Created required directories" echo -e "2. Added zoho-config.php with environment variable loading" echo -e "3. Created diagnostics.php for troubleshooting" echo -e "4. Added CSS styling for debug information" echo -e "5. Enhanced the zoho-admin.js file for better error reporting" echo -e "6. Patched the PHP classes for better logging and error handling" echo -e "7. 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