diff --git a/wordpress-dev/wordpress/wp-content/plugins/hvac-community-events/includes/google-sheets/class-google-sheets-auth.php b/wordpress-dev/wordpress/wp-content/plugins/hvac-community-events/includes/google-sheets/class-google-sheets-auth.php index 669aa5bc..87590dff 100644 --- a/wordpress-dev/wordpress/wp-content/plugins/hvac-community-events/includes/google-sheets/class-google-sheets-auth.php +++ b/wordpress-dev/wordpress/wp-content/plugins/hvac-community-events/includes/google-sheets/class-google-sheets-auth.php @@ -45,8 +45,8 @@ class HVAC_Google_Sheets_Auth { // Load stored access token from WordPress options $this->load_access_token(); - // Register callback handler - add_action('init', array($this, 'handle_oauth_callback')); + // Register callback handler - use template_redirect to catch it before page rendering + add_action('template_redirect', array($this, 'handle_oauth_callback')); } /** @@ -78,11 +78,21 @@ class HVAC_Google_Sheets_Auth { 'code' => $auth_code ); + if (class_exists('HVAC_Logger')) { + HVAC_Logger::info("Token exchange request params: " . json_encode(array( + 'client_id' => substr($this->client_id, 0, 20) . '...', + 'redirect_uri' => $this->redirect_uri, + 'grant_type' => 'authorization_code', + 'code' => substr($auth_code, 0, 20) . '...' + )), 'GoogleSheets'); + } + $response = wp_remote_post($this->token_url, array( 'body' => $params, 'headers' => array( 'Content-Type' => 'application/x-www-form-urlencoded' - ) + ), + 'timeout' => 30 )); if (is_wp_error($response)) { @@ -90,7 +100,14 @@ class HVAC_Google_Sheets_Auth { return false; } + $response_code = wp_remote_retrieve_response_code($response); $body = wp_remote_retrieve_body($response); + + if (class_exists('HVAC_Logger')) { + HVAC_Logger::info("Token exchange response code: " . $response_code, 'GoogleSheets'); + HVAC_Logger::info("Token exchange response body: " . $body, 'GoogleSheets'); + } + $data = json_decode($body, true); if (isset($data['access_token'])) { @@ -98,7 +115,13 @@ class HVAC_Google_Sheets_Auth { if (isset($data['refresh_token'])) { $this->refresh_token = $data['refresh_token']; } - $this->token_expiry = time() + $data['expires_in']; + $this->token_expiry = time() + (int)$data['expires_in']; + + if (class_exists('HVAC_Logger')) { + HVAC_Logger::info("Successfully received tokens. Access token: " . substr($this->access_token, 0, 20) . "...", 'GoogleSheets'); + HVAC_Logger::info("Refresh token: " . ($this->refresh_token ? 'RECEIVED' : 'NOT RECEIVED'), 'GoogleSheets'); + HVAC_Logger::info("Token expires at: " . date('Y-m-d H:i:s', $this->token_expiry), 'GoogleSheets'); + } // Save tokens $this->save_tokens(); @@ -106,7 +129,7 @@ class HVAC_Google_Sheets_Auth { return true; } - $this->log_error('Invalid token response: ' . $body); + $this->log_error('Invalid token response (status ' . $response_code . '): ' . $body); return false; } @@ -282,7 +305,17 @@ class HVAC_Google_Sheets_Auth { 'created_at' => time() ); - update_option('hvac_google_sheets_tokens', $token_data); + $result = update_option('hvac_google_sheets_tokens', $token_data); + + if (class_exists('HVAC_Logger')) { + HVAC_Logger::info("Saving tokens to database: " . ($result ? 'SUCCESS' : 'FAILED'), 'GoogleSheets'); + HVAC_Logger::info("Token data: " . json_encode(array( + 'access_token' => substr($this->access_token, 0, 20) . '...', + 'refresh_token' => $this->refresh_token ? 'SET' : 'NOT SET', + 'expires_at' => date('Y-m-d H:i:s', $this->token_expiry), + 'created_at' => date('Y-m-d H:i:s', time()) + )), 'GoogleSheets'); + } } /** @@ -345,6 +378,16 @@ class HVAC_Google_Sheets_Auth { * Handle OAuth callback from Google */ public function handle_oauth_callback() { + // Debug: Log all OAuth callback attempts + if (isset($_GET['code']) && isset($_GET['scope'])) { + if (class_exists('HVAC_Logger')) { + HVAC_Logger::info("OAuth callback detected - URI: " . $_SERVER['REQUEST_URI'], 'GoogleSheets'); + HVAC_Logger::info("OAuth callback - code param: " . substr($_GET['code'], 0, 20) . "...", 'GoogleSheets'); + } + error_log("HVAC Google OAuth callback detected - URI: " . $_SERVER['REQUEST_URI']); + error_log("HVAC Google OAuth callback - code: " . substr($_GET['code'], 0, 20) . "..."); + } + // Check if this is an OAuth callback request to the Google Sheets page if (isset($_GET['code']) && isset($_GET['scope']) && (strpos($_SERVER['REQUEST_URI'], '/google-sheets/') !== false || @@ -354,11 +397,19 @@ class HVAC_Google_Sheets_Auth { if (class_exists('HVAC_Logger')) { HVAC_Logger::info("Processing OAuth callback with code: " . substr($auth_code, 0, 20) . "...", 'GoogleSheets'); + HVAC_Logger::info("Current redirect URI: " . $this->redirect_uri, 'GoogleSheets'); } // Exchange the authorization code for tokens $success = $this->exchange_code_for_tokens($auth_code); + if (class_exists('HVAC_Logger')) { + HVAC_Logger::info("Token exchange result: " . ($success ? 'SUCCESS' : 'FAILED'), 'GoogleSheets'); + if (!$success) { + HVAC_Logger::error("Token exchange error: " . $this->get_last_error(), 'GoogleSheets'); + } + } + if ($success) { // Redirect to Google Sheets admin page with success message (clean URL) wp_redirect(add_query_arg(array( diff --git a/wordpress-dev/wordpress/wp-content/plugins/hvac-community-events/includes/google-sheets/class-google-sheets-manager.php b/wordpress-dev/wordpress/wp-content/plugins/hvac-community-events/includes/google-sheets/class-google-sheets-manager.php index be093709..8ab1e526 100644 --- a/wordpress-dev/wordpress/wp-content/plugins/hvac-community-events/includes/google-sheets/class-google-sheets-manager.php +++ b/wordpress-dev/wordpress/wp-content/plugins/hvac-community-events/includes/google-sheets/class-google-sheets-manager.php @@ -193,6 +193,7 @@ class HVAC_Google_Sheets_Manager { $data = array( 'range' => 'System Overview!A1:B10', 'majorDimension' => 'ROWS', + 'valueInputOption' => 'USER_ENTERED', 'values' => array( array('HVAC Community Events - System Overview', ''), array('Generated', date('Y-m-d H:i:s')), @@ -242,6 +243,7 @@ class HVAC_Google_Sheets_Manager { $data = array( 'range' => 'Trainer Performance!A1:E' . (count($values)), 'majorDimension' => 'ROWS', + 'valueInputOption' => 'USER_ENTERED', 'values' => $values ); @@ -280,6 +282,7 @@ class HVAC_Google_Sheets_Manager { $data = array( 'range' => 'All Events!A1:F' . (count($values)), 'majorDimension' => 'ROWS', + 'valueInputOption' => 'USER_ENTERED', 'values' => $values ); @@ -315,6 +318,7 @@ class HVAC_Google_Sheets_Manager { $data = array( 'range' => 'Revenue Analytics!A1:C' . (count($values)), 'majorDimension' => 'ROWS', + 'valueInputOption' => 'USER_ENTERED', 'values' => $values ); @@ -348,6 +352,7 @@ class HVAC_Google_Sheets_Manager { $data = array( 'range' => 'Event Details!A1:B' . (count($values)), 'majorDimension' => 'ROWS', + 'valueInputOption' => 'USER_ENTERED', 'values' => $values ); @@ -389,6 +394,7 @@ class HVAC_Google_Sheets_Manager { $data = array( 'range' => 'Attendees!A1:C' . (count($values)), 'majorDimension' => 'ROWS', + 'valueInputOption' => 'USER_ENTERED', 'values' => $values ); @@ -421,6 +427,7 @@ class HVAC_Google_Sheets_Manager { $data = array( 'range' => 'Financial Summary!A1:B' . (count($values)), 'majorDimension' => 'ROWS', + 'valueInputOption' => 'USER_ENTERED', 'values' => $values );