From 5b5f37de365ff84fc231e414a719d1b2ff4ceff6 Mon Sep 17 00:00:00 2001
From: Jake Vanderwerf <get@jakevanderwerf.ca>
Date: Thu, 01 Jan 2026 22:38:58 +0000
Subject: [PATCH] =further feed block testing

---
 inc/integrations/Integrations.php |   35 ++++++++++++++++++++---------------
 1 files changed, 20 insertions(+), 15 deletions(-)

diff --git a/inc/integrations/Integrations.php b/inc/integrations/Integrations.php
index 71392bb..b1958bc 100644
--- a/inc/integrations/Integrations.php
+++ b/inc/integrations/Integrations.php
@@ -761,6 +761,12 @@
 		array  $options = []
 	): array|WP_Error
 	{
+		if (!$this->is_healthy) {
+			$this->logDebug('Skipping request - integration is unhealthy', [
+				'consecutive_errors' => $this->error_stats['consecutive_errors'],
+				'last_success' => $this->error_stats['last_success']
+			]);
+		}
 		$this->ensureInitialized();
 		if (!$this->isSetUp()){
 			$this->logError('Connection not setup for '.$this->service_name, [
@@ -774,9 +780,6 @@
 			return new WP_Error('rate_limit', 'Rate limit exceeded. Please try again later.');
 		}
 
-		// Debug: Check if credentials are loaded
-		error_log('['.$this->service_name.'] Make Request - Credentials loaded: ' . (!empty($this->credentials) ? 'Yes' : 'No'));
-		error_log('With Credentials: '.print_r($this->credentials, true));
 
 		$attempt = 0;
 		$lastError = null;
@@ -1215,14 +1218,10 @@
 
 	public function handleAjaxResponse()
 	{
-		error_log('Ajax Response: '.print_r($_GET, true));
 
 		$code = $_GET['code'];
 		$state =  $_GET['state'];
 
-		error_log('OAuth Callback - Code: ' . $code);
-		error_log('OAuth Callback - State: ' . $state);
-
 
 		$state_parts = explode('|', $state);
 		$state_key = $state_parts[0] ?? '';
@@ -1230,16 +1229,13 @@
 		$user_id = ($user_id === 0) ? null : $user_id;
 		$return_url = isset($state_parts[2]) ? base64_decode($state_parts[2]) : admin_url('admin.php?page=jvb-integrations');
 
-		error_log('Service: '.print_r($this->service_name, true));
 		$state_data = get_transient('oauth_state_' . $state_key);
-		error_log('State Data: '.print_r($state_data, true));
 		if (!$state_data || $state_data['service'] !== $this->service_name) {
 			wp_die('Invalid state parameter', 'OAuth Error');
 		}
 
 		// Delete the transient to prevent reuse
 		delete_transient('oauth_state_' . $state_key);
-		error_log('Return URL: '.print_r($return_url, true));
 		// Handle error from OAuth provider
 		if (array_key_exists('error', $_GET)) {
 			$error_description = $_GET['error_description'] ?? 'Authorization denied';
@@ -1637,8 +1633,6 @@
 
 		$auth_url = $this->oauth['authorize'] . '?' . http_build_query($params);
 
-		// Debug log for troubleshooting
-		error_log("Generated OAuth URL for {$this->service_name}: " . $auth_url);
 
 		return $auth_url;
 	}
@@ -1932,7 +1926,6 @@
 			return false;
 		}
 
-		// Build refresh request data
 		$request_data = [
 			'client_id' => $this->credentials['client_id'],
 			'client_secret' => $this->credentials['client_secret'],
@@ -1940,12 +1933,24 @@
 			'grant_type' => 'refresh_token'
 		];
 
-		// Use centralized OAuth request method
 		$response = $this->makeOAuthRequest('POST', $this->oauth['token'], $request_data);
 
 		if (is_wp_error($response)) {
+			$error_message = $response->get_error_message();
+
+			if (str_contains($error_message, 'invalid_grant')) {
+				$this->logError('OAuth refresh token is invalid - user must re-authorize', [
+					'error' => $error_message
+				], 'critical');
+
+				// Mark unhealthy immediately
+				$this->error_stats['consecutive_errors'] = $this->error_threshold;
+				$this->is_healthy = false;
+				$this->saveErrorStats();
+			}
+
 			$this->logError('Failed to refresh OAuth token for '.$this->service_name, [
-				'error' => $response->get_error_message()
+				'error' => $error_message
 			]);
 			return false;
 		}

--
Gitblit v1.10.0