fix: Complete OAuth callback 404 resolution with parse_request implementation

The OAuth callback URL was returning 404 errors due to WordPress not recognizing
the custom /oauth/callback route. This commit implements a comprehensive fix using
multiple approaches to ensure OAuth callbacks are properly handled.

Changes:
- Add parse_request hook to directly intercept OAuth callback URLs
- Implement regex pattern matching for /oauth/callback requests
- Add fallback query var registration methods
- Include comprehensive error logging for debugging
- Maintain backward compatibility with existing rewrite rules

The parse_request implementation successfully bypasses WordPress's standard
rewrite rule processing, ensuring OAuth callbacks are caught and processed
before a 404 can occur. Verified working with successful token exchange.

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

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
bengizmo 2025-06-16 11:27:30 -03:00
parent b53c53b6a7
commit cb45406a9e

View file

@ -26,11 +26,17 @@ class HVAC_Zoho_Admin {
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
add_action('init', array($this, 'handle_oauth_callback'));
add_action('init', array($this, 'add_oauth_rewrite_rule'));
// Add OAuth callback handler - use early init priority
add_action('init', array($this, 'handle_oauth_callback'), 5);
add_action('init', array($this, 'add_oauth_rewrite_rule'), 5);
add_action('template_redirect', array($this, 'handle_oauth_template_redirect'));
add_filter('query_vars', array($this, 'add_oauth_query_vars'));
add_filter('query_vars', array($this, 'add_oauth_query_vars'), 10, 1);
// Also add to public query vars directly
add_action('init', array($this, 'add_public_query_vars'), 5);
// Alternative: Use pre_get_posts to catch the request
add_action('parse_request', array($this, 'parse_oauth_request'));
// Ensure rewrite rules are flushed when plugin is activated
register_activation_hook(HVAC_CE_PLUGIN_FILE, array($this, 'flush_rewrite_rules_on_activation'));
@ -481,9 +487,41 @@ class HVAC_Zoho_Admin {
*/
public function add_oauth_query_vars($vars) {
$vars[] = 'hvac_oauth_callback';
error_log('Adding hvac_oauth_callback to query vars. Current vars count: ' . count($vars));
return $vars;
}
/**
* Add OAuth query vars to public query vars
*/
public function add_public_query_vars() {
global $wp;
$wp->add_query_var('hvac_oauth_callback');
error_log('Added hvac_oauth_callback to public query vars');
}
/**
* Parse OAuth request using parse_request hook
*/
public function parse_oauth_request($wp) {
error_log('parse_oauth_request called with URI: ' . $_SERVER['REQUEST_URI']);
// Check if this is an OAuth callback request
if (preg_match('#^/oauth/callback/?#', $_SERVER['REQUEST_URI'])) {
error_log('OAuth callback detected in parse_request');
// Check if we have the code parameter
if (isset($_GET['code'])) {
error_log('Processing OAuth callback directly from parse_request');
$this->process_oauth_callback();
exit;
} else {
error_log('OAuth callback missing code parameter');
wp_die('OAuth callback missing authorization code');
}
}
}
/**
* Add OAuth callback rewrite rule
*/