From f94860aacd6200fb24c9e7431eb379a368cb392d Mon Sep 17 00:00:00 2001
From: Jake Vanderwerf <get@jakevanderwerf.ca>
Date: Fri, 10 Jul 2026 00:35:44 +0000
Subject: [PATCH] =Refactored CredentialsManager.php to utilize a CustomTable instance instead

---
 inc/integrations/Integrations.php |  161 ++++++++++++++++++++++-------------------------------
 1 files changed, 66 insertions(+), 95 deletions(-)

diff --git a/inc/integrations/Integrations.php b/inc/integrations/Integrations.php
index f7ce033..6f6232c 100644
--- a/inc/integrations/Integrations.php
+++ b/inc/integrations/Integrations.php
@@ -2312,93 +2312,6 @@
 
 	}
 
-	protected function getTermFieldMapping(string $taxonomy): array
-	{
-		return apply_filters(
-			"jvb_{$this->service_name}_term_field_mapping",
-			[],
-			$taxonomy,
-			$this
-		);
-	}
-
-	/**
-	 * Share post to external service (override in child classes)
-	 */
-	protected function sharePost(WP_Post $post): ?string
-	{
-		throw new Exception('sharePost must be implemented by child class');
-	}
-
-	/**
-	 * Update shared post on external service (override in child classes)
-	 */
-	protected function updateSharedPost(WP_Post $post, string $external_id): void
-	{
-		throw new Exception('updateSharedPost must be implemented by child class');
-	}
-
-	/**
-	 * Remove post from external service (override in child classes)
-	 */
-	protected function unsharePost(WP_Post $post, string $external_id): void
-	{
-		throw new Exception('unsharePost must be implemented by child class');
-	}
-
-	/**
-	 * Get sync capabilities
-	 */
-	public function getSyncCapabilities(): array
-	{
-		return $this->canSync;
-	}
-
-	/**
-	 * Check if a specific sync capability is supported
-	 */
-	public function supportsSyncCapability(string $capability): bool
-	{
-		return $this->canSync[$capability] ?? false;
-	}
-
-	/**
-	 * Get posts shared to this service
-	 */
-	public function getSharedPosts(array $args = []): array
-	{
-		$defaults = [
-			'post_type' => $this->syncPostTypes,
-			'meta_query' => [
-				[
-					'key' => BASE."_{$this->service_name}_item_id",
-					'compare' => 'EXISTS'
-				]
-			],
-			'posts_per_page' => -1
-		];
-
-		$args = wp_parse_args($args, $defaults);
-		return get_posts($args);
-	}
-
-	/**
-	 * Check if post is shared to this service
-	 */
-	public function isPostShared(int $postID): bool
-	{
-		$external_id = get_post_meta($postID, BASE."_{$this->service_name}_item_id", true);
-		return $external_id !== '';
-	}
-
-	/**
-	 * Get external ID for a post
-	 */
-	public function getPostExternalId(int $postID): ?string
-	{
-		$external_id = get_post_meta($postID, BASE."_{$this->service_name}_item_id", true);
-		return $external_id ?: null;
-	}
 
 	/*******************************************************************
 	 * UTILITIES
@@ -2793,14 +2706,6 @@
 		return strtolower($this->service_name);
 	}
 
-	/**
-	 * Refresh credentials from storage and re-initialize
-	 */
-	public function refreshCredentials(): void
-	{
-		$this->credentials = [];
-		$this->ensureInitialized();
-	}
 
 	/**
 	 * Enhanced webhook handling with common patterns
@@ -4001,8 +3906,74 @@
 	/*********************************************************************************
 	 * Syncing WP items from Service
 	*********************************************************************************/
+	/**
+	 * @param array $itemData
+	 * @return bool
+	 * @throws Exception
+	 */
 	protected function createItemFromService(array $itemData):bool
 	{
+		throw new Exception('createItemFromService must be implemented by child class '.$this->service_name);
+	}
 
+	/**
+	 * @param array $itemData
+	 * @return bool
+	 * @throws Exception
+	 */
+	protected function updateItemFromService(array $itemData):bool
+	{
+		throw new Exception('updateItemFromService must be implemented by child class '.$this->service_name);
+	}
+
+	/**
+	 * @param array $itemData
+	 * @return bool
+	 * @throws Exception
+	 */
+	protected function deleteItemFromService(array $itemData):bool
+	{
+		throw new Exception('deleteItemFromService must be implemented by child class '.$this->service_name);
+	}
+
+	/****************************************************************
+	 * CONNECT to a user's integration
+	****************************************************************/
+	protected array $connections = [];
+	public function connectAs(?int $userID = null):bool
+	{
+		if (!$this->userCanConnect($userID)) {
+			return false;
+		}
+		$this->cleanup();
+		$this->userID = $userID;
+		$this->ensureInitialized();
+		return true;
+	}
+
+	public function userCanConnect(?int $userID):bool
+	{
+		$user = get_userdata($userID);
+		if (!$user || is_wp_error($user)) {
+			return false;
+		}
+		if (current_user_can('manage_options')) {
+			return true;
+		}
+		$role = jvbUserRole($userID);
+		$registrar = Registrar::getInstance($role);
+		if (!$registrar || !$registrar->hasIntegration($this->service_name)) {
+			return false;
+		}
+
+		return (bool) Meta::forUser($userID)->get('integration_'.$this->service_name.'_connected');
+	}
+
+	protected function cleanup():void
+	{
+		$this->userID = false;
+		$this->credentials = [];
+		$this->request_history = [];
+		$this->resetTokenRefreshFlag();
 	}
 }

--
Gitblit v1.10.0