fix: Resolve OAuth callback 404 error and add debugging features
- 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 <noreply@anthropic.com>
This commit is contained in:
parent
0c661a1a56
commit
b53c53b6a7
2 changed files with 81 additions and 1 deletions
|
|
@ -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__));
|
||||
|
||||
|
|
|
|||
|
|
@ -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 {
|
|||
<p class="description">
|
||||
Use this exact URL in your Zoho OAuth app configuration.
|
||||
<button type="button" id="copy-redirect-uri" class="button button-small">Copy</button>
|
||||
<button type="button" id="flush-rewrite-rules" class="button button-small">Flush Rules</button>
|
||||
</p>
|
||||
<?php
|
||||
// Debug: Check if rewrite rule exists
|
||||
$rewrite_rules = get_option('rewrite_rules');
|
||||
$oauth_rule_exists = isset($rewrite_rules['^oauth/callback/?$']);
|
||||
?>
|
||||
<p class="description" style="color: <?php echo $oauth_rule_exists ? '#46b450' : '#dc3232'; ?>">
|
||||
OAuth rewrite rule: <?php echo $oauth_rule_exists ? '✓ Active' : '✗ Missing (click Flush Rules)'; ?>
|
||||
</p>
|
||||
</td>
|
||||
</tr>
|
||||
|
|
@ -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;
|
||||
});
|
||||
});
|
||||
</script>
|
||||
|
|
@ -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;
|
||||
|
|
|
|||
Loading…
Reference in a new issue