From c68aefb847b09daa0697de7684d3451e2e68ce1e Mon Sep 17 00:00:00 2001
From: Jake Vanderwerf <get@jakevanderwerf.ca>
Date: Thu, 09 Jul 2026 23:10:23 +0000
Subject: [PATCH] =Refactor of Integrations.php to be a bit more useful with a suite of methods for create, update, delete back and forths for integrations where sharing is enabled. Also considering a single instance with a connect method to connect as the site (no user) vs connecting as an individual user - rather than recreating instances for every user.

---
 inc/integrations/Instagram.php |   41 +++++++++++++++++++++++++++--------------
 1 files changed, 27 insertions(+), 14 deletions(-)

diff --git a/inc/integrations/Instagram.php b/inc/integrations/Instagram.php
index 63910b1..3968e45 100644
--- a/inc/integrations/Instagram.php
+++ b/inc/integrations/Instagram.php
@@ -6,6 +6,7 @@
 namespace JVBase\integrations;
 
 use JVBase\integrations\Integrations;
+use JVBase\meta\Meta;
 use WP_Error;
 use WP_REST_Request;
 use WP_REST_Response;
@@ -17,6 +18,9 @@
 
 class Instagram extends Integrations
 {
+	protected array $allowedContent = [
+		'post'
+	];
 	private string $ig_user_id = '';
 	private string $page_id = '';
 	private string $page_access_token = '';
@@ -29,11 +33,22 @@
 	private const GRAPH_API_BASE = 'https://graph.facebook.com/';
 	private const INSTAGRAM_BASE = 'https://graph.instagram.com/';
 
-	public function __construct(?int $userID = null)
+	protected static array $instances = [];
+	public static function getInstance(?int $userID = null):self
+	{
+		$key = is_null($userID) ? 'base' : $userID;
+		if (!array_key_exists($key, self::$instances)) {
+			self::$instances[$key] = new self($userID);
+		}
+
+		return self::$instances[$key];
+	}
+
+	protected function __construct(?int $userID = null)
 	{
 		$this->service_name = 'instagram';
 		$this->title = 'Instagram';
-		$this->icon = 'instagram';
+		$this->icon = 'instagram-logo';
 		$this->apiBase = [
 			'graph' => self::GRAPH_API_BASE . self::API_VERSION,
 			'instagram' => self::INSTAGRAM_BASE . self::API_VERSION
@@ -247,24 +262,22 @@
 	 */
 	public function handleTheSavePost(int $postID, \WP_Post $post, bool $update, array $settings): void
 	{
-		// Check if post has featured image (Instagram requirement)
+
+		if (!$this->hasOAuthCredentials()) {
+			error_log('OAuth Not set up for '.$this->service_name);
+			return;
+		}
 		if (!has_post_thumbnail($postID)) {
-			$this->logError('Cannot post to Instagram without featured image', [
-				'post_id' => $postID
-			]);
 			return;
 		}
 
-		// Queue the Instagram post creation
-		$this->queueOperation('create_post', [
-			'post_id' => $postID,
-			'type' => get_post_type($postID)
+		$this->queueOperation(self::$syncTo, [
+			'items'		=> [$postID],
+			'user'		=> user_can($post->post_author, 'manage_options') ? null : $post->post_author
 		], [
-			'priority' => 'normal',
-			'delay' => 60 // Wait 1 minute to ensure all metadata is saved
+			'delay'	=> 60
 		]);
-
-		update_post_meta($postID, BASE . '_instagram_sync_status', 'queued');
+		Meta::forPost($postID)->set('_'.$this->service_name.'_sync_status', 'queued');
 	}
 
 	/**

--
Gitblit v1.10.0