get_error_message() . \"\\n\"; } else { $response_body = wp_remote_retrieve_body($response); $log_message .= \"[\" . date('Y-m-d H:i:s') . \"] API Response Body: \" . substr($response_body, 0, 1000) . (strlen($response_body) > 1000 ? '...' : '') . \"\\n\"; } if (defined('ZOHO_LOG_FILE')) { error_log($log_message, 3, ZOHO_LOG_FILE); } }"; $auth_content = preg_replace($response_pattern, $response_replacement, $auth_content); // Save the updated file file_put_contents($zoho_auth_path, $auth_content); echo "Updated Zoho CRM Auth class with environment loading and enhanced logging.\n"; } else { echo "Zoho CRM Auth class already patched.\n"; } } else { echo "Warning: Zoho CRM Auth class file not found at $zoho_auth_path\n"; } // Update Zoho Admin class to add better error reporting if (file_exists($zoho_admin_path)) { $admin_content = file_get_contents($zoho_admin_path); // Check if already patched if (strpos($admin_content, 'ZOHO_DEBUG_MODE') === false) { // Make a backup file_put_contents($zoho_admin_path . '.bak.' . date('YmdHis'), $admin_content); // Find the test_connection method $pattern = '/public function test_connection\(\) {([^}]+)}/s'; // Add enhanced error reporting $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) )); } }"; // Replace in the file $admin_content = preg_replace($pattern, $replacement, $admin_content); // Save the updated file file_put_contents($zoho_admin_path, $admin_content); echo "Updated Zoho Admin class with enhanced error reporting.\n"; } else { echo "Zoho Admin class already patched.\n"; } } else { echo "Warning: Zoho Admin class file not found at $zoho_admin_path\n"; } // Update Zoho Admin JavaScript to display detailed error information if (file_exists($zoho_js_path)) { $js_content = file_get_contents($zoho_js_path); // Check if already patched if (strpos($js_content, 'hvac-zoho-debug-info') === false) { // Make a backup file_put_contents($zoho_js_path . '.bak.' . date('YmdHis'), $js_content); // Find the error handling section $pattern = '/error: function\(response\) {([^}]+)}/s'; // Add enhanced error display $replacement = "error: function(response) { $('#hvac-zoho-test-connection-result').empty(); // Create detailed error display var errorHtml = '
'; errorHtml += '

' + response.data.message + ': ' + response.data.error + '

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

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

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

Details: ' + response.data.details + '

'; } // Add raw response data if available if (response.data.raw) { errorHtml += '
'; errorHtml += 'Raw Response Data (click to expand)'; errorHtml += '
' + JSON.stringify(JSON.parse(response.data.raw), null, 2) + '
'; errorHtml += '
'; } errorHtml += '
'; // Close debug info div errorHtml += '
'; // Close notice div $('#hvac-zoho-test-connection-result').html(errorHtml); }"; // Replace in the file $js_content = preg_replace($pattern, $replacement, $js_content); // Save the updated file file_put_contents($zoho_js_path, $js_content); echo "Updated Zoho Admin JavaScript with enhanced error display.\n"; } else { echo "Zoho Admin JavaScript already patched.\n"; } } else { echo "Warning: Zoho Admin JavaScript file not found at $zoho_js_path\n"; } // Add CSS styling for the debug information if (file_exists($zoho_css_path)) { $css_content = file_get_contents($zoho_css_path); // Check if already patched if (strpos($css_content, 'hvac-zoho-debug-info') === false) { // Make a backup file_put_contents($zoho_css_path . '.bak.' . date('YmdHis'), $css_content); // Add styling for debug information $css_content .= "\n\n/* 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; }"; // Save the updated file file_put_contents($zoho_css_path, $css_content); echo "Updated Zoho Admin CSS with debug information styling.\n"; } else { echo "Zoho Admin CSS already patched.\n"; } } else { echo "Warning: Zoho Admin CSS file not found at $zoho_css_path\n"; } // Update Zoho config file to add environment loading if (file_exists($zoho_config_path)) { $config_content = file_get_contents($zoho_config_path); // Check if already patched if (strpos($config_content, 'load_env_from_dotenv') === false) { // Make a backup file_put_contents($zoho_config_path . '.bak.' . date('YmdHis'), $config_content); // Extract credentials preg_match('/define\\s*\\(\\s*[\'"]ZOHO_CLIENT_ID[\'"]\\s*,\\s*(.*?)\\s*\\)\\s*;/s', $config_content, $client_id_match); preg_match('/define\\s*\\(\\s*[\'"]ZOHO_CLIENT_SECRET[\'"]\\s*,\\s*(.*?)\\s*\\)\\s*;/s', $config_content, $client_secret_match); preg_match('/define\\s*\\(\\s*[\'"]ZOHO_REFRESH_TOKEN[\'"]\\s*,\\s*(.*?)\\s*\\)\\s*;/s', $config_content, $refresh_token_match); $client_id = isset($client_id_match[1]) ? $client_id_match[1] : "getenv('ZOHO_CLIENT_ID') ?: ''"; $client_secret = isset($client_secret_match[1]) ? $client_secret_match[1] : "getenv('ZOHO_CLIENT_SECRET') ?: ''"; $refresh_token = isset($refresh_token_match[1]) ? $refresh_token_match[1] : "getenv('ZOHO_REFRESH_TOKEN') ?: ''"; // Create new config with env loading $new_config = <<get_access_token(); echo "Token generation result: " . ($token ? "Success (Token: " . substr($token, 0, 10) . "...)" : "Failed") . "\n"; // Test API connection echo "Testing API connection...\n"; $modules = $zoho_auth->get_modules(); if (is_array($modules) && !isset($modules['error'])) { echo "API connection successful!\n"; echo "Available modules: " . (isset($modules['modules']) ? count($modules['modules']) : '0') . "\n"; if (isset($modules['modules'])) { echo "Module names:\n"; foreach ($modules['modules'] as $module) { echo " - " . $module['api_name'] . "\n"; } } } else { echo "API connection failed!\n"; echo "Error details: " . print_r($modules, true) . "\n"; } } catch (Exception $e) { echo "Exception: " . $e->getMessage() . "\n"; echo "Stack trace: " . $e->getTraceAsString() . "\n"; } } else { echo "HVAC_Zoho_CRM_Auth class does not exist!\n"; } echo "\n"; echo "==========================================\n"; echo "Log File Contents (if available)\n"; echo "==========================================\n"; if (defined('ZOHO_LOG_FILE') && file_exists(ZOHO_LOG_FILE) && is_readable(ZOHO_LOG_FILE)) { echo "Log file found at: " . ZOHO_LOG_FILE . "\n"; echo "Log file size: " . filesize(ZOHO_LOG_FILE) . " bytes\n"; echo "Last 50 lines of log file:\n"; $log_lines = file(ZOHO_LOG_FILE); $last_lines = array_slice($log_lines, -50); echo "-------------------------\n"; foreach ($last_lines as $line) { echo $line; } echo "-------------------------\n"; } else { echo "Log file not found or not readable.\n"; } echo "\n"; echo "==========================================\n"; echo "Diagnostics completed at " . date('Y-m-d H:i:s') . "\n"; echo "==========================================\n"; // Exit to prevent any further output exit(); EOT; file_put_contents($diagnostics_path, $diagnostics_content); echo "Created Zoho CRM diagnostics script at $diagnostics_path\n"; } else { echo "Zoho CRM diagnostics script already exists at $diagnostics_path\n"; } // Create local diagnostics file $check_permissions_path = $plugin_path . '/includes/zoho/check-permissions.php'; if (!file_exists($check_permissions_path)) { $check_permissions_content = <<