From a6413da67a4579866f0fd0323ddb23a7b6b9654a Mon Sep 17 00:00:00 2001
From: Jake Vanderwerf <get@jakevanderwerf.ca>
Date: Fri, 18 Sep 2026 17:00:53 +0000
Subject: [PATCH] =Cleaning up the Integrations setup a bit, started the Helcim integration work too

---
 inc/integrations/_Base.php |  105 ++++++++++++++++++++++++++++++++++++++++++++++------
 1 files changed, 92 insertions(+), 13 deletions(-)

diff --git a/inc/integrations/_Base.php b/inc/integrations/_Base.php
index 2d62970..d381877 100644
--- a/inc/integrations/_Base.php
+++ b/inc/integrations/_Base.php
@@ -1,7 +1,9 @@
 <?php
 namespace JVBase\integrations;
 
+use JVBase\inc\managers\ErrorHelper;
 use JVBase\managers\Cache;
+use JVBase\managers\Errors;
 use JVBase\managers\IconsManager;
 
 if (!defined('ABSPATH')) {
@@ -9,6 +11,7 @@
 }
 
 trait _Base {
+	use ErrorHelper;
 	/**
 	 * @var string Unique identifier for this service (ex: square, facebook)
 	 */
@@ -30,14 +33,8 @@
 	 */
 	protected string $apiVersion;
 	protected Cache $cache;
-	//Use Trait "Requests" to set this
-	protected bool $hasRequests = false;
-	protected ?RateLimits $requestLimiter = null;
-	//Use Trait "OAuth" to set this
-	protected ?RateLimits $oauthLimiter = null;
-	protected bool $hasOAuth = false;
-	//Use trait "Webhooks" to set this true
-	protected bool $hasWebhooks = false;
+	protected ?string $cacheName = null;
+
 
 	/**
 	 * @var array Each item in the array becomes a li element in the ordered list of instructions
@@ -60,9 +57,39 @@
 	 * @var bool Flag to indicate there are extra options that warrant a dedicated options page for this service
 	 * Usually used for default field values for syncable services
 	 */
-	public bool $hasExtraOptions = false;
+	public static bool $hasExtraOptions = false;
+	public bool $hasBatchCreate = false;
+	public bool $hasBatchUpdate = false;
+	public bool $hasBatchDelete = false;
+	public bool $canCreateOnUpdate = false;
+	/**
+	 * Post Syncing Capabilities
+	 * Define what sync operations this integration supports
+	 */
+	protected array $canSync = [
+		'initial' => false,    // Can share new posts to the service
+		'update' => false,     // Can update already-shared posts
+		'delete' => false,     // Can remove posts from the service
+	];
 
+	/**
+	 * @var array // 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 $syncPostTypes = [];
+	/**
+	 * @var array Taxonomies that can be synced (e.g., ['category', 'section']): usually built by Registrar.php if the integration name exists as a key in [ 'integrations' => []]
+	 */
+	protected array $syncTaxonomies = [];
+	/**
+	 * @var array User roles that can be synced (e.g., ['customer']): usually built by Registrar.php if the integration name exists as a key in [ 'integrations' => []]
+	 */
+	protected array $syncUsers = [];
+	/**
+	 * @var array Integration's specific content types. An array of [ TYPE => [$fields] ] Set by child classes' setContentTypes
+	 */
+	protected array $contentTypes = [];
 
+	protected array $credentials;
 
 
 	/**
@@ -70,18 +97,38 @@
 	 */
 	public function isSetUp(): bool
 	{
-		return !empty($this->loadCredentials());
+		$tmp = null;
+		if ($this->userID) {
+			$tmp = $this->userID;
+			$this->switchUser();
+		}
+		$isSetUp = !empty($this->loadCredentials()??[]);
+		if ($tmp) {
+			$this->switchUser($tmp);
+		}
+		return $isSetUp;
 	}
 
 	public function loadCredentials():array
 	{
-		return Auth::getInstance()->getCredentials($this->service_name, $this->userID);
+		if (!isset($this->credentials)) {
+			$this->credentials = Auth::getInstance()->getCredentials($this->service_name, $this->userID);
+		}
+		return $this->credentials;
 	}
 
 	protected function logError(string $method, string $message, array $context = []):void
 	{
+
 		$context['service'] = $this->service_name;
-		JVB()->error()->log('['.self::class.']'.$method, $message, $context);
+		Errors::log($message, array_merge(
+			[
+				'service'	=> $this->service_name,
+				'method'	=> $method,
+				'class'		=> static::class,
+			],
+			$context
+		));
 	}
 
 	protected function getReturnURL():string
@@ -103,8 +150,40 @@
 	{
 		if (is_admin()) {
 			$icons = IconsManager::for('admin');
-			return $icons->getRawSvg($icon);
+			return $icons->getRawSvg($icon)??'';
 		}
 		return jvbDashIcon($icon);
 	}
+
+	protected function initializeRateLimiters():void
+	{
+		if (method_exists($this, 'initializeRequestLimiter')) {
+			$this->initializeRequestLimiter();
+		}
+		if (method_exists($this, 'initializeOAuthRateLimiter')) {
+			$this->initializeOAuthRateLimiter();
+		}
+	}
+
+	public function response(bool $success, string $message, array $data = []):array
+	{
+		$response = [
+			'success'	=> $success,
+			'message'	=> $message
+		];
+		if (!empty($data)) {
+			$response['data'] = $data;
+		}
+		return $response;
+	}
+
+	public function getAllowedContent():array
+	{
+		return array_keys($this->contentTypes);
+	}
+
+	public function getServiceName():string
+	{
+		return $this->service_name;
+	}
 }

--
Gitblit v1.10.0