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); } }); }); }); EOT; if (file_put_contents($js_path, $js_content)) { echo " Created: zoho-admin.js\n"; } else { echo " ERROR: Failed to create zoho-admin.js\n"; } // Step 6: Update or create the admin class echo "\nStep 6: Updating admin class...\n"; $admin_class_path = $plugin_path . '/includes/admin/class-zoho-admin.php'; if (file_exists($admin_class_path)) { // Backup the existing file copy($admin_class_path, $admin_class_path . '.bak.' . date('YmdHis')); echo " Created backup of admin class\n"; // Read the current content $admin_content = file_get_contents($admin_class_path); // Check for test_connection method if (strpos($admin_content, 'public function test_connection') !== false) { // Replace the method $pattern = '/public function test_connection\(\) {.*?}/s'; $replacement = 'public function test_connection() { // Enable debug logging if (!defined(\'ZOHO_DEBUG_MODE\')) { define(\'ZOHO_DEBUG_MODE\', true); } // Create a temporary log file if needed if (!defined(\'ZOHO_LOG_FILE\')) { $log_dir = HVAC_CE_PLUGIN_DIR . \'includes/logs\'; if (!file_exists($log_dir)) { mkdir($log_dir, 0755, true); } define(\'ZOHO_LOG_FILE\', $log_dir . \'/zoho-debug.log\'); } // Log the request if (defined(\'ZOHO_DEBUG_MODE\') && ZOHO_DEBUG_MODE) { $log_message = "[" . date(\'Y-m-d H:i:s\') . "] Testing Zoho CRM connection\\n"; if (defined(\'ZOHO_LOG_FILE\')) { error_log($log_message, 3, ZOHO_LOG_FILE); } } // Check nonce for security check_ajax_referer(\'hvac_zoho_admin_nonce\', \'nonce\'); // Get Zoho CRM Auth instance $zoho_auth = HVAC_Zoho_CRM_Auth::get_instance(); // Test the connection $response = $zoho_auth->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) )); } }'; $admin_content = preg_replace($pattern, $replacement, $admin_content); if (file_put_contents($admin_class_path, $admin_content)) { echo " Updated test_connection method in admin class\n"; } else { echo " ERROR: Failed to update admin class\n"; } } else { echo " WARNING: test_connection method not found in admin class\n"; } } else { echo " WARNING: Admin class file not found - create a bare minimum version\n"; // Create a minimal version of the admin class $admin_class_content = <<<'EOT' admin_url('admin-ajax.php'), 'nonce' => wp_create_nonce('hvac_zoho_admin_nonce') )); } } /** * Test Zoho CRM connection */ public function test_connection() { // Enable debug logging if (!defined('ZOHO_DEBUG_MODE')) { define('ZOHO_DEBUG_MODE', true); } // Create a temporary log file if needed if (!defined('ZOHO_LOG_FILE')) { $log_dir = HVAC_CE_PLUGIN_DIR . 'includes/logs'; if (!file_exists($log_dir)) { mkdir($log_dir, 0755, true); } define('ZOHO_LOG_FILE', $log_dir . '/zoho-debug.log'); } // Log the request if (defined('ZOHO_DEBUG_MODE') && ZOHO_DEBUG_MODE) { $log_message = "[" . date('Y-m-d H:i:s') . "] Testing Zoho CRM connection\n"; if (defined('ZOHO_LOG_FILE')) { error_log($log_message, 3, ZOHO_LOG_FILE); } } // Check nonce for security check_ajax_referer('hvac_zoho_admin_nonce', 'nonce'); // Get Zoho CRM Auth instance $zoho_auth = HVAC_Zoho_CRM_Auth::get_instance(); // Test the connection $response = $zoho_auth->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) )); } } } // Initialize the admin class HVAC_Zoho_Admin::get_instance(); EOT; if (file_put_contents($admin_class_path, $admin_class_content)) { echo " Created new admin class file\n"; } else { echo " ERROR: Failed to create admin class file\n"; } } // Step 7: Update or create the auth class echo "\nStep 7: Updating auth class...\n"; $auth_class_path = $plugin_path . '/includes/zoho/class-zoho-crm-auth.php'; if (file_exists($auth_class_path)) { // Backup the existing file copy($auth_class_path, $auth_class_path . '.bak.' . date('YmdHis')); echo " Created backup of auth class\n"; // Read the current content $auth_content = file_get_contents($auth_class_path); // Check if already has load_env_from_dotenv if (strpos($auth_content, 'load_env_from_dotenv') === false) { // Update the constructor to load environment variables $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(); }'; $auth_content = preg_replace($pattern, $replacement, $auth_content); // Add getter methods if they don't exist if (strpos($auth_content, 'get_client_id') === false) { $pattern = '/}(\s*)$/'; $replacement = ' /** * 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'; $auth_content = preg_replace($pattern, $replacement, $auth_content); } if (file_put_contents($auth_class_path, $auth_content)) { echo " Updated auth class\n"; } else { echo " ERROR: Failed to update auth class\n"; } } else { echo " Auth class already has environment variable loading - skipping update\n"; } } else { echo " WARNING: Auth class file not found - create a bare minimum version\n"; // Create a minimal version of the auth class $auth_class_content = <<<'EOT' client_id = defined('ZOHO_CLIENT_ID') ? ZOHO_CLIENT_ID : ''; $this->client_secret = defined('ZOHO_CLIENT_SECRET') ? ZOHO_CLIENT_SECRET : ''; $this->redirect_uri = defined('ZOHO_REDIRECT_URI') ? ZOHO_REDIRECT_URI : ''; $this->refresh_token = defined('ZOHO_REFRESH_TOKEN') ? ZOHO_REFRESH_TOKEN : ''; // Get access token if refresh token exists if (!empty($this->refresh_token)) { $this->get_access_token(); } } /** * Get access token * * @return string */ public function get_access_token() { // Check if we have a valid access token if (!empty($this->access_token) && $this->access_token_expiry > time()) { return $this->access_token; } // If we have a refresh token, try to get a new access token if (!empty($this->refresh_token)) { $response = $this->refresh_access_token(); if (isset($response['access_token'])) { $this->access_token = $response['access_token']; $this->access_token_expiry = time() + $response['expires_in']; return $this->access_token; } } // Log the error if (defined('ZOHO_DEBUG_MODE') && ZOHO_DEBUG_MODE) { $log_message = "[" . date('Y-m-d H:i:s') . "] Failed to get access token\n"; $log_message .= "[" . date('Y-m-d H:i:s') . "] Refresh token: " . (empty($this->refresh_token) ? 'EMPTY' : substr($this->refresh_token, 0, 5) . '...') . "\n"; if (defined('ZOHO_LOG_FILE')) { error_log($log_message, 3, ZOHO_LOG_FILE); } } return ''; } /** * Refresh access token * * @return array */ public function refresh_access_token() { $url = ZOHO_ACCOUNTS_URL . '/oauth/v2/token'; $args = array( 'method' => 'POST', 'headers' => array( 'Content-Type' => 'application/x-www-form-urlencoded' ), 'body' => array( 'refresh_token' => $this->refresh_token, 'client_id' => $this->client_id, 'client_secret' => $this->client_secret, 'grant_type' => 'refresh_token' ) ); // Log the request if (defined('ZOHO_DEBUG_MODE') && ZOHO_DEBUG_MODE) { $log_message = "[" . date('Y-m-d H:i:s') . "] Refreshing access token\n"; $log_message .= "[" . date('Y-m-d H:i:s') . "] URL: $url\n"; $log_message .= "[" . date('Y-m-d H:i:s') . "] Client ID: " . substr($this->client_id, 0, 5) . "...\n"; $log_message .= "[" . date('Y-m-d H:i:s') . "] Client Secret: " . substr($this->client_secret, 0, 5) . "...\n"; $log_message .= "[" . date('Y-m-d H:i:s') . "] Refresh Token: " . substr($this->refresh_token, 0, 5) . "...\n"; if (defined('ZOHO_LOG_FILE')) { error_log($log_message, 3, ZOHO_LOG_FILE); } } $response = wp_remote_post($url, $args); if (is_wp_error($response)) { // Log the error if (defined('ZOHO_DEBUG_MODE') && ZOHO_DEBUG_MODE) { $log_message = "[" . date('Y-m-d H:i:s') . "] WP Error: " . $response->get_error_message() . "\n"; if (defined('ZOHO_LOG_FILE')) { error_log($log_message, 3, ZOHO_LOG_FILE); } } return array( 'error' => $response->get_error_message() ); } $body = json_decode(wp_remote_retrieve_body($response), true); if (isset($body['error'])) { // Log the error if (defined('ZOHO_DEBUG_MODE') && ZOHO_DEBUG_MODE) { $log_message = "[" . date('Y-m-d H:i:s') . "] Zoho Error: " . $body['error'] . "\n"; if (isset($body['error_description'])) { $log_message .= "[" . date('Y-m-d H:i:s') . "] Error Description: " . $body['error_description'] . "\n"; } if (defined('ZOHO_LOG_FILE')) { error_log($log_message, 3, ZOHO_LOG_FILE); } } return array( 'error' => isset($body['error_description']) ? $body['error_description'] : $body['error'] ); } return $body; } /** * Make API request * * @param string $endpoint * @param array $args * @return array */ public function make_api_request($endpoint, $args = array()) { // Get access token $access_token = $this->get_access_token(); if (empty($access_token)) { return array( 'error' => 'No access token available' ); } // Set default args $defaults = array( 'method' => 'GET', 'headers' => array( 'Authorization' => 'Zoho-oauthtoken ' . $access_token, 'Content-Type' => 'application/json' ) ); $args = wp_parse_args($args, $defaults); // Convert body to JSON if it's an array if (isset($args['body']) && is_array($args['body'])) { $args['body'] = json_encode($args['body']); } $url = ZOHO_API_BASE_URL . '/' . ltrim($endpoint, '/'); // Log the request if (defined('ZOHO_DEBUG_MODE') && ZOHO_DEBUG_MODE) { $log_message = "[" . date('Y-m-d H:i:s') . "] Making API request\n"; $log_message .= "[" . date('Y-m-d H:i:s') . "] URL: $url\n"; $log_message .= "[" . date('Y-m-d H:i:s') . "] Method: " . $args['method'] . "\n"; if (isset($args['body'])) { $log_message .= "[" . date('Y-m-d H:i:s') . "] Body: " . $args['body'] . "\n"; } if (defined('ZOHO_LOG_FILE')) { error_log($log_message, 3, ZOHO_LOG_FILE); } } $response = wp_remote_request($url, $args); if (is_wp_error($response)) { // Log the error if (defined('ZOHO_DEBUG_MODE') && ZOHO_DEBUG_MODE) { $log_message = "[" . date('Y-m-d H:i:s') . "] WP Error: " . $response->get_error_message() . "\n"; if (defined('ZOHO_LOG_FILE')) { error_log($log_message, 3, ZOHO_LOG_FILE); } } return array( 'error' => $response->get_error_message() ); } $body = json_decode(wp_remote_retrieve_body($response), true); $status = wp_remote_retrieve_response_code($response); // Log the response if (defined('ZOHO_DEBUG_MODE') && ZOHO_DEBUG_MODE) { $log_message = "[" . date('Y-m-d H:i:s') . "] API Response\n"; $log_message .= "[" . date('Y-m-d H:i:s') . "] Status: $status\n"; $log_message .= "[" . date('Y-m-d H:i:s') . "] Body: " . wp_remote_retrieve_body($response) . "\n"; if (defined('ZOHO_LOG_FILE')) { error_log($log_message, 3, ZOHO_LOG_FILE); } } if ($status < 200 || $status >= 300) { return array( 'error' => isset($body['message']) ? $body['message'] : 'API request failed with status ' . $status, 'code' => $status, 'details' => $body ); } return $body; } /** * Get modules * * @return array */ public function get_modules() { return $this->make_api_request('settings/modules'); } /** * 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; } } EOT; if (file_put_contents($auth_class_path, $auth_class_content)) { echo " Created new auth class file\n"; } else { echo " ERROR: Failed to create auth class file\n"; } } // Step 8: Clear the WordPress cache echo "\nStep 8: Attempting to clear WordPress cache...\n"; if (function_exists('wp_cache_flush')) { wp_cache_flush(); echo " Cleared WordPress object cache\n"; } else { echo " WordPress functions not available - cache not cleared\n"; } // Summary echo "\n=============================================\n"; echo "Zoho CRM Integration Fix Complete\n"; echo "=============================================\n"; echo "The following files have been created or updated:\n"; echo "1. zoho-config.php - Added environment variable loading\n"; echo "2. diagnostics.php - Added diagnostic tools\n"; echo "3. zoho-admin.css - Added CSS for error display\n"; echo "4. zoho-admin.js - Enhanced error reporting in JS\n"; echo "5. class-zoho-admin.php - Enhanced test_connection method\n"; echo "6. class-zoho-crm-auth.php - Added environment variable loading\n"; echo "\nTo test the integration:\n"; echo "1. Go to Events > Zoho CRM Sync in the WordPress admin\n"; echo "2. Click the 'Test Connection' button\n"; echo "3. You should now see detailed error information\n"; echo "\nTo run diagnostics, visit:\n"; echo "https://wordpress-974670-5399585.cloudwaysapps.com/wp-content/plugins/hvac-community-events/includes/zoho/diagnostics.php?run_diagnostics=true\n"; echo "\nCompleted at: " . date('Y-m-d H:i:s') . "\n"; echo "=============================================\n";