feat: Add Zoho CRM diagnostic and setup tools

- Added setup-zoho-credentials.php script to help generate OAuth credentials
- Added check-permissions.php tool to diagnose file access issues
- Updated diagnostics system to better identify common problems
- Added detailed steps for setting up Zoho CRM integration

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
bengizmo 2025-05-21 11:22:25 -03:00
parent 005bbf537e
commit 0d55020273
2 changed files with 269 additions and 0 deletions

View file

@ -0,0 +1,125 @@
<?php
/**
* Zoho CRM Credentials Setup Script
*
* This script helps you generate the Zoho CRM credentials needed for the integration.
* Run this script on your staging server to complete the OAuth flow and obtain a refresh token.
*
* Usage: php setup-zoho-credentials.php
*/
echo "\n=== Zoho CRM Credentials Setup ===\n\n";
// Get user input
echo "Step 1: Enter your Zoho CRM OAuth Application details\n";
echo "------------------------------------------------\n";
echo "These can be created at https://api-console.zoho.com/\n\n";
echo "Enter your Client ID: ";
$client_id = trim(fgets(STDIN));
echo "Enter your Client Secret: ";
$client_secret = trim(fgets(STDIN));
// Validate input
if (empty($client_id) || empty($client_secret)) {
die("Error: Client ID and Client Secret are required.\n");
}
// Set staging server redirect URI
$staging_url = "https://wordpress-974670-5399585.cloudwaysapps.com";
$redirect_uri = "$staging_url/oauth/callback";
echo "\nUsing redirect URI: $redirect_uri\n";
echo "Make sure this URL is added to your Zoho OAuth application's authorized redirect URIs.\n";
// Generate authorization URL
$auth_url = "https://accounts.zoho.com/oauth/v2/auth?" . http_build_query([
'scope' => 'ZohoCRM.settings.all,ZohoCRM.modules.all,ZohoCRM.users.all,ZohoCRM.org.all',
'client_id' => $client_id,
'response_type' => 'code',
'access_type' => 'offline',
'redirect_uri' => $redirect_uri,
'prompt' => 'consent'
]);
echo "\nStep 2: Authorization\n";
echo "-------------------\n";
echo "Open this URL in your browser:\n\n";
echo "$auth_url\n\n";
echo "After authorization, you'll be redirected to:\n";
echo "$redirect_uri?code=YOUR_AUTH_CODE\n\n";
echo "Enter the authorization code from the URL: ";
$auth_code = trim(fgets(STDIN));
if (empty($auth_code)) {
die("Error: Authorization code is required.\n");
}
// Exchange code for tokens
echo "\nStep 3: Exchanging code for tokens...\n";
echo "-----------------------------------\n";
$token_url = 'https://accounts.zoho.com/oauth/v2/token';
$token_params = [
'grant_type' => 'authorization_code',
'client_id' => $client_id,
'client_secret' => $client_secret,
'redirect_uri' => $redirect_uri,
'code' => $auth_code
];
// Use cURL to make the request
$ch = curl_init($token_url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($token_params));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
$response = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($http_code !== 200) {
echo "Error: Failed to get tokens (HTTP $http_code)\n";
echo "Response: " . $response . "\n";
exit(1);
}
$token_data = json_decode($response, true);
if (!isset($token_data['access_token']) || !isset($token_data['refresh_token'])) {
echo "Error: Invalid token response\n";
echo "Response: " . $response . "\n";
exit(1);
}
// Success! Display the values
echo "✓ Tokens received successfully!\n\n";
echo "Here are your Zoho CRM credentials:\n";
echo "--------------------------------\n";
echo "Client ID: $client_id\n";
echo "Client Secret: $client_secret\n";
echo "Refresh Token: " . $token_data['refresh_token'] . "\n\n";
// Generate wp-config.php code
echo "Add these lines to your wp-config.php file:\n";
echo "----------------------------------------\n";
echo "define('ZOHO_CLIENT_ID', '$client_id');\n";
echo "define('ZOHO_CLIENT_SECRET', '$client_secret');\n";
echo "define('ZOHO_REFRESH_TOKEN', '" . $token_data['refresh_token'] . "');\n";
echo "define('ZOHO_DIAGNOSTICS_ENABLED', true);\n\n";
// Generate environment variables setup
echo "Or set these environment variables in your .env file:\n";
echo "-----------------------------------------------\n";
echo "ZOHO_CLIENT_ID=$client_id\n";
echo "ZOHO_CLIENT_SECRET=$client_secret\n";
echo "ZOHO_REFRESH_TOKEN=" . $token_data['refresh_token'] . "\n\n";
echo "To verify your setup, run the diagnostics script:\n";
echo "--------------------------------------------\n";
echo "$staging_url/wp-content/plugins/hvac-community-events/includes/zoho/diagnostics.php?run_diagnostics=true\n\n";
echo "=== Setup Complete! ===\n";

View file

@ -0,0 +1,144 @@
<?php
/**
* Zoho CRM File & Permissions Diagnostic Tool
*
* This script checks file permissions and directory access
* that might affect the Zoho CRM integration.
*
* Access with: ?run_check=true
*/
// Security check
if (!isset($_GET['run_check']) || $_GET['run_check'] !== 'true') {
die('Access denied. Use ?run_check=true parameter.');
}
// Set headers
header('Content-Type: text/plain');
echo "=== Zoho CRM File & Permissions Check ===\n\n";
echo "Date: " . date('Y-m-d H:i:s') . "\n";
echo "Server: " . $_SERVER['SERVER_NAME'] . "\n";
echo "PHP Version: " . phpversion() . "\n\n";
// Check plugin directory
$plugin_dir = dirname(dirname(dirname(__FILE__)));
echo "Plugin Directory: $plugin_dir\n";
echo "Exists: " . (file_exists($plugin_dir) ? 'Yes' : 'No') . "\n";
echo "Readable: " . (is_readable($plugin_dir) ? 'Yes' : 'No') . "\n";
echo "Writable: " . (is_writable($plugin_dir) ? 'Yes' : 'No') . "\n";
echo "Permissions: " . substr(sprintf('%o', fileperms($plugin_dir)), -4) . "\n\n";
// Check Zoho directory
$zoho_dir = dirname(__FILE__);
echo "Zoho Directory: $zoho_dir\n";
echo "Exists: " . (file_exists($zoho_dir) ? 'Yes' : 'No') . "\n";
echo "Readable: " . (is_readable($zoho_dir) ? 'Yes' : 'No') . "\n";
echo "Writable: " . (is_writable($zoho_dir) ? 'Yes' : 'No') . "\n";
echo "Permissions: " . substr(sprintf('%o', fileperms($zoho_dir)), -4) . "\n\n";
// Check logs directory
$logs_dir = dirname(dirname(__FILE__)) . '/logs';
echo "Logs Directory: $logs_dir\n";
echo "Exists: " . (file_exists($logs_dir) ? 'Yes' : 'No') . "\n";
// Create logs directory if it doesn't exist
if (!file_exists($logs_dir)) {
echo "Trying to create logs directory...\n";
$result = @mkdir($logs_dir, 0755, true);
echo "Creation result: " . ($result ? 'Success' : 'Failed') . "\n";
if ($result) {
echo "Readable: " . (is_readable($logs_dir) ? 'Yes' : 'No') . "\n";
echo "Writable: " . (is_writable($logs_dir) ? 'Yes' : 'No') . "\n";
echo "Permissions: " . substr(sprintf('%o', fileperms($logs_dir)), -4) . "\n";
}
} else {
echo "Readable: " . (is_readable($logs_dir) ? 'Yes' : 'No') . "\n";
echo "Writable: " . (is_writable($logs_dir) ? 'Yes' : 'No') . "\n";
echo "Permissions: " . substr(sprintf('%o', fileperms($logs_dir)), -4) . "\n";
}
echo "\n";
// Check zoho-config.php
$config_file = $zoho_dir . '/zoho-config.php';
echo "Config File: $config_file\n";
echo "Exists: " . (file_exists($config_file) ? 'Yes' : 'No') . "\n";
echo "Readable: " . (is_readable($config_file) ? 'Yes' : 'No') . "\n";
echo "Writable: " . (is_writable($config_file) ? 'Yes' : 'No') . "\n";
echo "Size: " . (file_exists($config_file) ? filesize($config_file) . ' bytes' : 'N/A') . "\n";
echo "Permissions: " . (file_exists($config_file) ? substr(sprintf('%o', fileperms($config_file)), -4) : 'N/A') . "\n\n";
// Check class-zoho-crm-auth.php
$auth_file = $zoho_dir . '/class-zoho-crm-auth.php';
echo "Auth Class: $auth_file\n";
echo "Exists: " . (file_exists($auth_file) ? 'Yes' : 'No') . "\n";
echo "Readable: " . (is_readable($auth_file) ? 'Yes' : 'No') . "\n";
echo "Size: " . (file_exists($auth_file) ? filesize($auth_file) . ' bytes' : 'N/A') . "\n";
echo "Permissions: " . (file_exists($auth_file) ? substr(sprintf('%o', fileperms($auth_file)), -4) : 'N/A') . "\n\n";
// Test log file writing
$test_log_file = $logs_dir . '/test-permissions.log';
echo "Testing log file writing: $test_log_file\n";
$write_result = @file_put_contents($test_log_file, date('Y-m-d H:i:s') . " Test log entry\n", FILE_APPEND);
echo "Write result: " . ($write_result !== false ? 'Success (' . $write_result . ' bytes)' : 'Failed') . "\n";
if ($write_result !== false) {
echo "File exists after write: " . (file_exists($test_log_file) ? 'Yes' : 'No') . "\n";
echo "File permissions: " . substr(sprintf('%o', fileperms($test_log_file)), -4) . "\n";
}
echo "\n";
// Check if we can load WordPress
echo "Checking WordPress integration...\n";
$loaded_wp = false;
// Try to load WordPress
if (!function_exists('get_option')) {
// Try to find and load WordPress
$wp_load_path = dirname(dirname(dirname(dirname(dirname(__FILE__))))) . '/wp-load.php';
if (file_exists($wp_load_path)) {
echo "Found wp-load.php at: $wp_load_path\n";
require_once $wp_load_path;
$loaded_wp = function_exists('get_option');
echo "WordPress loaded: " . ($loaded_wp ? 'Yes' : 'No') . "\n";
} else {
echo "Could not find wp-load.php\n";
}
} else {
$loaded_wp = true;
echo "WordPress already loaded\n";
}
if ($loaded_wp) {
// Check if plugin is active
if (function_exists('is_plugin_active')) {
$plugin_active = is_plugin_active('hvac-community-events/hvac-community-events.php');
echo "Plugin active: " . ($plugin_active ? 'Yes' : 'No') . "\n";
} else {
echo "Could not check if plugin is active (is_plugin_active function not available)\n";
}
// Check WordPress options
echo "\nChecking WordPress options...\n";
echo "Site URL: " . get_option('siteurl') . "\n";
echo "Home URL: " . get_option('home') . "\n";
// Check if Zoho credentials are stored in options
echo "\nChecking Zoho credentials in WordPress options...\n";
echo "Access token option exists: " . (get_option('hvac_zoho_access_token') !== false ? 'Yes' : 'No') . "\n";
echo "Refresh token option exists: " . (get_option('hvac_zoho_refresh_token') !== false ? 'Yes' : 'No') . "\n";
echo "Token expiry option exists: " . (get_option('hvac_zoho_token_expiry') !== false ? 'Yes' : 'No') . "\n";
// Check user capabilities
echo "\nChecking current user capabilities...\n";
if (function_exists('current_user_can') && function_exists('wp_get_current_user')) {
echo "Current user: " . wp_get_current_user()->user_login . "\n";
echo "Can manage options: " . (current_user_can('manage_options') ? 'Yes' : 'No') . "\n";
} else {
echo "Could not check user capabilities\n";
}
}
echo "\n=== Check Completed ===\n";
echo "If you see any 'Failed' or 'No' responses, they may indicate issues with your Zoho CRM integration.\n";
echo "See the diagnostic log for more details about the connection test failures.";