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 | 36 +++++++++++++++++++++++-------------
1 files changed, 23 insertions(+), 13 deletions(-)
diff --git a/inc/integrations/Instagram.php b/inc/integrations/Instagram.php
index b2d34d7..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;
@@ -32,7 +33,18 @@
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';
@@ -250,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