From 0afb2c0046b55c123eafb4ab9ee77efa68d12463 Mon Sep 17 00:00:00 2001
From: Jake Vanderwerf <get@jakevanderwerf.ca>
Date: Sat, 06 Jun 2026 17:15:31 +0000
Subject: [PATCH] =Starting the Favourites.js setup, converting previous Northeh stuff to new Registrar, fixing up Square.php integration to match

---
 inc/integrations/Integrations.php |  222 ++++++++++++++++++++++++++----------------------------
 1 files changed, 107 insertions(+), 115 deletions(-)

diff --git a/inc/integrations/Integrations.php b/inc/integrations/Integrations.php
index 12b73ec..7b32588 100644
--- a/inc/integrations/Integrations.php
+++ b/inc/integrations/Integrations.php
@@ -2,10 +2,12 @@
 namespace JVBase\integrations;
 
 use Exception;
-use JVBase\managers\CacheManager;
-use JVBase\managers\UploadManager;
-use JVBase\meta\MetaManager;
+use JVBase\managers\Cache;
+use JVBase\meta\Form;
+use JVBase\meta\Meta;
 use JVBase\managers\ErrorHandler;
+use JVBase\registrar\helpers\AddIntegrationFields;
+use JVBase\registrar\Registrar;
 use WP_Error;
 use WP_Post;
 use WP_REST_Request;
@@ -91,12 +93,13 @@
 		'test_connection' => 'Test Connection',
 	];
 
+	protected array $allowedContent = [];
 
 	/**
 	 * Caching Configuration
 	 */
 	protected ?string $cacheName = null;
-	protected CacheManager $cache;
+	protected Cache $cache;
 	protected array $cacheStrategy = [
 		'aggressive' => 3600,  // 1 hour for stable data (e.g., profile info)
 		'moderate' => 300,     // 5 minutes for semi-dynamic data (e.g., posts)
@@ -131,8 +134,8 @@
 		'update' => false,     // Can update already-shared posts
 		'delete' => false,     // Can remove posts from the service
 	];
-	protected array $syncPostTypes = []; // Post types that can be synced (e.g., ['artwork', 'tattoo']): usually built by querying the global JVB_CONTENT if the integration name exists as a key in [ 'integrations' => []]
-	protected array $syncTaxonomies = []; // Post types that can be synced (e.g., ['artwork', 'tattoo']): usually built by querying the global JVB_CONTENT if the integration name exists as a key in [ 'integrations' => []]
+	protected array $syncPostTypes = []; // Post types that can be synced (e.g., ['artwork', 'tattoo']): usually built by Registrar.php if the integration name exists as a key in [ 'integrations' => []]
+	protected array $syncTaxonomies = []; // Post types that can be synced (e.g., ['artwork', 'tattoo']): usually built by Registrar.php if the integration name exists as a key in [ 'integrations' => []]
 	protected array $contentTypes = [];   // Integration's available content types. Set by child classes' getContentTypes
 	protected bool $has_content = false; // Whether integration has content that can sync
 	/**
@@ -167,7 +170,7 @@
 	{
 		$this->cacheName = $this->cacheName ?: $this->service_name;
 		$this->userID = $userID;
-		$this->cache = CacheManager::for('integrations_' . $this->cacheName, $this->ttl);
+		$this->cache = Cache::for('integrations_' . $this->cacheName, $this->ttl);
 
 		// Load error stats from cache
 		$this->loadErrorStats();
@@ -295,12 +298,10 @@
 		$postTypes = get_option($key, false);
 		if (!$postTypes) {
 			$postTypes = [];
-
-			// Get from JVB_CONTENT
-
-			foreach (JVB_CONTENT as $type => $config) {
-				if (array_key_exists('integrations', $config) && array_key_exists($this->service_name, $config['integrations'])) {
-					$postTypes[] = $type;
+			foreach (Registrar::getRegistered('post') as $registrar) {
+				$registrar = Registrar::getInstance($registrar);
+				if ($registrar->hasIntegration($this->service_name)) {
+					$postTypes[] = $registrar->getSlug();
 				}
 			}
 
@@ -318,11 +319,10 @@
 		if (!$taxonomies) {
 			// Combine both content and taxonomy filtering
 			$taxonomies = [];
-			// Get from JVB_TAXONOMY (content taxonomies)
-			foreach (JVB_TAXONOMY as $type => $config) {
-				if (jvbCheck('is_content', $config) &&
-					isset($config['integrations'][$this->service_name])) {
-					$taxonomies[] = $type;
+			foreach (Registrar::getFeatured('is_content', 'term') as $type) {
+				$registrar = Registrar::getInstance($type);
+				if ($registrar->hasIntegration($this->service_name)) {
+					$taxonomies[] = $registrar->getSlug();
 				}
 			}
 
@@ -430,7 +430,6 @@
 		} else {
 			$result =  $this->$method();
 		}
-		error_log('Action result: '.print_r($result, true));
 		if (is_wp_error($result)) {
 			return [
 				'success' => false,
@@ -651,9 +650,7 @@
 		}
 
 		try {
-			error_log('Credentials to save: '.print_r($this->credentials, true));
 			if ($this->isOAuthService && !$this->hasOAuthCredentials()){
-				error_log('Just saving credentials, we don\'t have OAuth setup yet...');
 				//If this is an OAuth service, we might only be saving the app credentials first
 				$result = true;
 			} else {
@@ -672,7 +669,7 @@
 
 	protected function clearCache():array
 	{
-		$success = $this->cache->clear();
+		$success = $this->cache->flush();
 		return [
 			'success'	=> $success,
 		];
@@ -761,6 +758,9 @@
 		array  $options = []
 	): array|WP_Error
 	{
+		if (!$this->is_healthy) {
+			return new WP_Error('unhealthy', 'Connection marked unhealthy. Skipping fetch');
+		}
 		$this->ensureInitialized();
 		if (!$this->isSetUp()){
 			$this->logError('Connection not setup for '.$this->service_name, [
@@ -780,15 +780,15 @@
 
 		while ($attempt < $this->maxRetries) {
 			try {
-				$this->logDebug('[Integrations] Making request to '.$this->service_name);
+//				$this->logDebug('[Integrations] Making request to '.$this->service_name);
 				$result = $this->executeRequest($method, $endpoint, $data, $baseKey, $options);
 				if (!$result) {
 					return new WP_Error('Request Error');
 				}
 				$this->recordRequest($method, $endpoint);
-				$this->logDebug('[Integrations]', [
-					'response' => $result
-				]);
+//				$this->logDebug('[Integrations]', [
+//					'response' => $result
+//				]);
 				// Reset error stats on success
 				$this->resetErrorStats();
 				return $result;
@@ -837,7 +837,7 @@
 			return null;
 		}
 
-		$this->logDebug("$method request to: $url: ".print_r($args, true));
+//		$this->logDebug("$method request to: $url: ".print_r($args, true));
 
 		// Make the request
 		$response = match($method) {
@@ -868,9 +868,9 @@
 			if ($retry_count === 0) {
 				$retry_count++;
 
-				$this->logDebug('Got 401, attempting token refresh...');
+//				$this->logDebug('Got 401, attempting token refresh...');
 				if ($this->refreshOAuthToken()) {
-					$this->logDebug('Token refreshed successfully, retrying request...');
+//					$this->logDebug('Token refreshed successfully, retrying request...');
 
 					// Rebuild request args with new token
 					$args = $this->buildRequestArgs($method, $data, $options);
@@ -926,7 +926,8 @@
 		if (!$force && $ttl > 0) {
 			$cached = $this->cache->get($cacheKey);
 			if ($cached !== false) {
-				$this->logDebug("Cache hit for: $cacheKey");
+
+//				$this->logDebug("Cache hit for: $cacheKey");
 				return $cached;
 			}
 		}
@@ -1384,7 +1385,7 @@
 					// Only attempt refresh once per request
 					if (!$this->token_refresh_attempted) {
 						$this->token_refresh_attempted = true;
-						$this->logDebug('OAuth token expired, attempting refresh');
+//						$this->logDebug('OAuth token expired, attempting refresh');
 
 						if (!$this->refreshOAuthToken()) {
 							$this->logError('Failed to refresh expired OAuth token - stopping execution');
@@ -1393,14 +1394,14 @@
 						}
 					} else {
 						// Already attempted refresh in this request
-						$this->logDebug('Token refresh already attempted, skipping');
+//						$this->logDebug('Token refresh already attempted, skipping');
 						return;
 					}
 				}
 				// Check if we should proactively refresh (before expiry)
 				elseif ($this->shouldRefreshToken() && !$this->token_refresh_attempted) {
 					$this->token_refresh_attempted = true;
-					$this->logDebug('OAuth token should be refreshed proactively');
+//					$this->logDebug('OAuth token should be refreshed proactively');
 					if (!$this->refreshOAuthToken()) {
 						$this->logError('Failed to proactively refresh OAuth token');
 						// Not critical - token is still valid, so continue
@@ -1625,10 +1626,7 @@
 			$params = $this->addOAuthParams($params);
 		}
 
-		$auth_url = $this->oauth['authorize'] . '?' . http_build_query($params);
-
-
-		return $auth_url;
+		return $this->oauth['authorize'] . '?' . http_build_query($params);
 	}
 
 	/**
@@ -1920,7 +1918,6 @@
 			return false;
 		}
 
-		// Build refresh request data
 		$request_data = [
 			'client_id' => $this->credentials['client_id'],
 			'client_secret' => $this->credentials['client_secret'],
@@ -1928,12 +1925,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;
 		}
@@ -2072,20 +2081,11 @@
 	 */
 	protected function mapFieldsToService(int $postID, array $mapping): array
 	{
-		$meta_manager = new MetaManager($postID, 'post');
-		$post = get_post($postID);
+		$meta_manager = Meta::forPost($postID);
 		$service_data = [];
 
 		foreach ($mapping as $wp_field => $service_field) {
-			$value = null;
-
-			// Check if it's a post field
-			if (property_exists($post, $wp_field)) {
-				$value = $post->$wp_field;
-			} else {
-				// It's a meta field
-				$value = $meta_manager->getValue($wp_field);
-			}
+			$value = $meta_manager->get($wp_field);
 
 			if ($value !== null && $value !== '') {
 				$this->setNestedValue($service_data, $service_field, $value);
@@ -2124,52 +2124,54 @@
 	 */
 	public function handleSavePost(int $postID, WP_Post $post, bool $update): void
 	{
-		error_log('Testing For Save Post');
-		if (jvbNoSaveIt($postID, $post)) {
-			error_log('Excluded by jvbNoSaveIt');
+		if (!$postID || $postID === 0) {
 			return;
 		}
+		$postType = jvbNoBase($post->post_type);
+
+		if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return;
+		if (wp_is_post_revision($postID)) return;
+
+		$registrar = Registrar::getInstance($postType);
+		if (!$registrar){
+			return;
+		}
+
 		if (empty($this->syncPostTypes)) {
-			error_log('No Syncable post types');
 			return;
 		}
 
-		$config = JVB_CONTENT[jvbNoBase($post->post_type)]??null;
-		if (!$config) {
-			error_log('No Config set');
-			return;
-		}
-
-		$settings = $config['integrations'][$this->service_name]??null;
+		$settings = $registrar->hasIntegration($this->service_name)??null;
 		if (!$settings) {
-			error_log('No settings');
 			return;
 		}
 
+		$settings = $registrar->getIntegrationConfig($this->service_name);
+		if (!$settings){
+			return;
+		}
+
+
 		$fields = $this->getSyncFields($postID, 'post', ['schedule_'.$this->service_name]);
-		error_log('Fields to check: '.print_r($fields, true));
 		if (!$fields['share_to_'.$this->service_name]) {
 			return;
 		}
 
 		$isShared = isset($fields["_{$this->service_name}_item_id"]);
 		if ($update && $isShared && !$fields['_keep_synced_'.$this->service_name]) {
-			error_log('Do not keep synced, not syncing with '.$this->service_name);
 			return;
 		}
 
 		if ($post->post_status !== 'publish' && !$isShared) {
-			error_log('Not published and not already shared');
 			return;
 		}
-		error_log('Sending to integration for processing...');
 		$this->handleTheSavePost($postID, $post, $update, $settings);
 	}
 
 
 	protected function getSyncFields(int $postID, string $type, array $additional = []):array
 	{
-		$meta = new MetaManager($postID, $type);
+		$meta = new Meta($postID, $type);
 		$fieldsToCheck = [
 			'share_to_' . $this->service_name,
 			'_keep_synced_' . $this->service_name,
@@ -2252,13 +2254,13 @@
 		if (!in_array($noBase, $this->syncTaxonomies)) {
 			return;
 		}
-		// Check if taxonomy is content-type
-		$config = JVB_TAXONOMY[$noBase] ?? null;
-		if (!$config || !($config['is_content'] ?? false)) {
+		$registrar = Registrar::getInstance($noBase);
+		if (!$registrar->hasFeature('is_content')) {
 			return;
 		}
 
-		$settings = $config['integrations'][$this->service_name] ?? null;
+
+		$settings = $registrar->getIntegrationConfig($this->service_name);
 		if (!$settings) {
 			return;
 		}
@@ -2635,11 +2637,6 @@
 		];
 		$this->is_healthy = true;
 		$this->saveErrorStats();
-
-		$this->logDebug('Integration health manually reset', [
-			'reset_by' => get_current_user_id(),
-			'reset_time' => time()
-		]);
 	}
 
 	/**
@@ -2774,9 +2771,6 @@
 
 		// Check for duplicate processing (idempotency)
 		if ($this->isWebhookProcessed($payload)) {
-			$this->logDebug('Webhook already processed', [
-				'webhook_id' => $this->extractWebhookId($payload)
-			]);
 			return true; // Return true to prevent retries
 		}
 
@@ -2938,7 +2932,7 @@
 			return '';
 		}
 
-		$meta = new MetaManager($this->userID, 'integrations');
+		$meta = Meta::forOptions($this->userID.'_integrations');
 		$is_connected = $this->isSetUp();
 		$credentials = $this->getCredentials();
 
@@ -2951,7 +2945,7 @@
 		?>
 		<form id="<?=$this->service_name?>" class="integration <?php echo $is_connected ? 'connected' : 'disconnected'; ?>"
 			 data-service="<?php echo esc_attr($this->service_name); ?>">
-			<div class="header row btw">
+			<div class="header row x-btw">
 				<h3><?php echo esc_html($this->title); ?></h3>
 				<div class="setup">
 					<?php if ($is_connected): ?>
@@ -3015,7 +3009,7 @@
 						$config['value'] = $credentials[$name]??'';
 						$config['autocomplete'] = 'off';
 						$config['base'] = $this->service_name.'_';
-						$meta->render('form', $name, $config);
+						echo Form::render($name, '', $config);
 					}
 				}
 				if ($this->handleWebhooks) {
@@ -3046,7 +3040,7 @@
 							$config['value'] = $credentials[$name]??'';
 							$config['base'] = $this->service_name.'_';
 							$config['autocomplete'] = 'off';
-							$meta->render('form', $name, $config);
+							Form::render($name,null, $config);
 						}
 						?>
 					</details>
@@ -3062,7 +3056,7 @@
 				}
 				?>
 			</div>
-			<div class="actions row btw wrap">
+			<div class="actions row x-btw wrap">
 				<?php
 				foreach ($this->buttons as $action => $label) {
 					if (!$is_connected && $action !== 'save_credentials') {
@@ -3189,7 +3183,6 @@
 		// Verify nonce
 		$nonce_field = 'jvb_integration_nonce_' . $service;
 		$nonce_action = 'jvb_integration_save_' . $service;
-		error_log('handleAdminPost: '.print_r($_POST, true));
 		if (!isset($_POST[$nonce_field]) || !wp_verify_nonce($_POST[$nonce_field], $nonce_action)) {
 			wp_die('Security check failed');
 		}
@@ -3286,7 +3279,7 @@
 		if (empty($types)) {
 			return;
 		}
-		$meta = new MetaManager($this->userID, 'integrations');
+		$meta = Meta::forOptions($this->userID.'_integrations');
 		?>
 		<form>
 			<h1><?= $this->title?> Defaults:</h1>
@@ -3298,16 +3291,19 @@
 
 				$config['base'] = $this->service_name.'_';
 				$config['autocomplete'] = 'off';
-				$meta->render('form', $name, $config);
+				echo Form::render($name, null, $config);
 			}
 			foreach ($this->syncPostTypes as $type) {
-				$config = JVB_CONTENT[$type];
+				$registrar = Registrar::getInstance($type);
+
+				$icon = $registrar->getIcon();
+				$icon = $icon === '' ? jvbDefaultIcon() : $icon;
 				?>
 				<details>
-					<summary><?= jvbIcon($config['icon']) ?><?= $config['singular']?> Defaults</summary>
+					<summary><?= jvbIcon($icon) ?><?= $registrar->getSingular()?> Defaults</summary>
 					<?php
-					$fields = new \JVBase\registry\providers\IntegrationFieldProvider();
-					$fields = $fields->getIntegrationFields($this->service_name, $config);
+					$fields = new AddIntegrationFields($this->service_name);
+					$fields = $fields->getIntegrationFields();
 					foreach($fields as $name=>$c) {
 						$c['required'] = false;
 						if ($c['type'] === 'number') {
@@ -3318,7 +3314,7 @@
 							$c['hint'] = $c['description'];
 							unset($c['description']);
 						}
-						$meta->render('form', $name, $c);
+						echo Form::render($name, null, $c);
 					}
 					?>
 				</details>
@@ -3344,28 +3340,10 @@
 			return [];
 		}
 
-		$key = BASE.$this->service_name.'_enabled_content_types';
-		$enabled = get_option($key);
-		if (!$enabled) {
-			$enabled = [];
-			foreach (array_merge(JVB_CONTENT, JVB_TAXONOMY) as $content => $config) {
-				if (!array_key_exists('integrations', $config)) {
-					continue;
-				}
-				if (!array_key_exists($this->service_name, $config['integrations'])) {
-					continue;
-				}
-				if (!array_key_exists('content_type', $config['integrations'][$this->service_name])) {
-					continue;
-				}
-				$type = $config['integrations'][$this->service_name]['content_type'];
-				if (!in_array($type, $enabled)) {
-					$enabled[] = $type;
-				}
-			}
-			update_option($key, $enabled);
-		}
-		return $enabled;
+		return array_filter(array_map(function($registrar) {
+			$registrar = Registrar::getInstance($registrar);
+			return $registrar->getIntegration($this->service_name)->getContentType();
+		}, Registrar::withIntegration($this->service_name)));
 	}
 
 	protected function getSupportedImage(int $imgID):int
@@ -3534,4 +3512,18 @@
 	{
 		$this->token_refresh_attempted = false;
 	}
+
+	public function getAllowedContent():array
+	{
+		return $this->allowedContent;
+	}
+
+	/**
+	 * Used by JVBase\registrar\helpers\AddIntegrationFields.php
+	 * @return array
+	 */
+	public function getAdditionalFields(?string $content_type = null):array
+	{
+		return [];
+	}
 }

--
Gitblit v1.10.0