From b53c53b6a7ffe0d42d0310a4eac9a39973e6fad8 Mon Sep 17 00:00:00 2001 From: bengizmo Date: Sun, 15 Jun 2025 07:09:17 -0300 Subject: [PATCH] fix: Resolve OAuth callback 404 error and add debugging features MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Add missing HVAC_CE_PLUGIN_FILE constant for plugin activation hooks - Implement automatic rewrite rule flushing to prevent 404 errors - Add admin interface button to manually flush rewrite rules - Include real-time rewrite rule status indicator in admin - Add comprehensive OAuth callback debugging and error logging - Change OAuth flow to use same window instead of popup for better UX - Add activation hook to ensure rewrite rules are set up properly The OAuth callback URL /oauth/callback should now work correctly after WordPress rewrite rules have been flushed on the staging server. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude --- .../hvac-community-events.php | 1 + .../includes/admin/class-zoho-admin.php | 81 ++++++++++++++++++- 2 files changed, 81 insertions(+), 1 deletion(-) diff --git a/wordpress-dev/wordpress/wp-content/plugins/hvac-community-events/hvac-community-events.php b/wordpress-dev/wordpress/wp-content/plugins/hvac-community-events/hvac-community-events.php index 4944098d..25891199 100644 --- a/wordpress-dev/wordpress/wp-content/plugins/hvac-community-events/hvac-community-events.php +++ b/wordpress-dev/wordpress/wp-content/plugins/hvac-community-events/hvac-community-events.php @@ -18,6 +18,7 @@ if (!defined('ABSPATH')) { // Define plugin constants define('HVAC_CE_VERSION', '1.0.0'); +define('HVAC_CE_PLUGIN_FILE', __FILE__); define('HVAC_CE_PLUGIN_DIR', plugin_dir_path(__FILE__)); define('HVAC_CE_PLUGIN_URL', plugin_dir_url(__FILE__)); diff --git a/wordpress-dev/wordpress/wp-content/plugins/hvac-community-events/includes/admin/class-zoho-admin.php b/wordpress-dev/wordpress/wp-content/plugins/hvac-community-events/includes/admin/class-zoho-admin.php index 1cbb5a1a..d6949a83 100644 --- a/wordpress-dev/wordpress/wp-content/plugins/hvac-community-events/includes/admin/class-zoho-admin.php +++ b/wordpress-dev/wordpress/wp-content/plugins/hvac-community-events/includes/admin/class-zoho-admin.php @@ -23,6 +23,7 @@ class HVAC_Zoho_Admin { add_action('wp_ajax_hvac_zoho_test_connection', array($this, 'test_connection')); add_action('wp_ajax_hvac_zoho_sync_data', array($this, 'sync_data')); add_action('wp_ajax_hvac_zoho_save_credentials', array($this, 'save_credentials')); + add_action('wp_ajax_hvac_zoho_flush_rewrite_rules', array($this, 'flush_rewrite_rules_ajax')); // Add simple test handler add_action('wp_ajax_hvac_zoho_simple_test', array($this, 'simple_test')); // Add OAuth callback handler @@ -30,6 +31,9 @@ class HVAC_Zoho_Admin { add_action('init', array($this, 'add_oauth_rewrite_rule')); add_action('template_redirect', array($this, 'handle_oauth_template_redirect')); add_filter('query_vars', array($this, 'add_oauth_query_vars')); + + // Ensure rewrite rules are flushed when plugin is activated + register_activation_hook(HVAC_CE_PLUGIN_FILE, array($this, 'flush_rewrite_rules_on_activation')); } /** @@ -191,6 +195,15 @@ class HVAC_Zoho_Admin {

Use this exact URL in your Zoho OAuth app configuration. + +

+ +

+ OAuth rewrite rule:

@@ -316,6 +329,28 @@ class HVAC_Zoho_Admin { }); }); + // Flush rewrite rules + $('#flush-rewrite-rules').on('click', function() { + var button = $(this); + button.prop('disabled', true).text('Flushing...'); + + $.post(ajaxurl, { + action: 'hvac_zoho_flush_rewrite_rules' + }, function(response) { + if (response.success) { + button.text('Flushed!').css('color', '#46b450'); + setTimeout(function() { + location.reload(); // Reload to update the status + }, 1000); + } else { + button.text('Error').css('color', '#dc3232'); + setTimeout(function() { + button.prop('disabled', false).text('Flush Rules').css('color', ''); + }, 2000); + } + }); + }); + // Handle credentials form submission $('#zoho-credentials-form').on('submit', function(e) { e.preventDefault(); @@ -360,7 +395,8 @@ class HVAC_Zoho_Admin { '&redirect_uri=' + encodeURIComponent(redirectUri) + '&prompt=consent'; - window.open(oauthUrl, '_blank'); + // Open OAuth URL in the same window to handle callback properly + window.location.href = oauthUrl; }); }); @@ -416,6 +452,30 @@ class HVAC_Zoho_Admin { )); } + /** + * Flush rewrite rules via AJAX + */ + public function flush_rewrite_rules_ajax() { + if (!current_user_can('manage_options')) { + wp_send_json_error(array('message' => 'Unauthorized access')); + return; + } + + flush_rewrite_rules(); + + wp_send_json_success(array( + 'message' => 'Rewrite rules flushed successfully' + )); + } + + /** + * Flush rewrite rules on plugin activation + */ + public function flush_rewrite_rules_on_activation() { + $this->add_oauth_rewrite_rule(); + flush_rewrite_rules(); + } + /** * Add OAuth query vars */ @@ -429,13 +489,26 @@ class HVAC_Zoho_Admin { */ public function add_oauth_rewrite_rule() { add_rewrite_rule('^oauth/callback/?$', 'index.php?hvac_oauth_callback=1', 'top'); + + // Check if we need to flush rewrite rules + $rewrite_rules = get_option('rewrite_rules'); + if (!isset($rewrite_rules['^oauth/callback/?$'])) { + // Rules need to be flushed + add_action('wp_loaded', 'flush_rewrite_rules'); + } } /** * Handle OAuth template redirect */ public function handle_oauth_template_redirect() { + // Debug: Log when this method is called + error_log('handle_oauth_template_redirect called'); + error_log('Query var hvac_oauth_callback: ' . get_query_var('hvac_oauth_callback')); + error_log('Request URI: ' . $_SERVER['REQUEST_URI']); + if (get_query_var('hvac_oauth_callback')) { + error_log('Processing OAuth callback'); $this->process_oauth_callback(); } } @@ -444,7 +517,11 @@ class HVAC_Zoho_Admin { * Process OAuth callback from Zoho */ public function process_oauth_callback() { + error_log('process_oauth_callback called'); + error_log('GET parameters: ' . print_r($_GET, true)); + if (!isset($_GET['code'])) { + error_log('OAuth callback missing authorization code'); wp_die('OAuth callback missing authorization code'); } @@ -497,6 +574,8 @@ class HVAC_Zoho_Admin { error_log('OAuth tokens saved successfully'); + error_log('OAuth tokens saved successfully, redirecting to admin page'); + // Success - redirect to admin page with success message wp_redirect(admin_url('admin.php?page=hvac-zoho-sync&oauth_success=1')); exit;