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/Facebook.php |   35 +++++++++++------------------------
 1 files changed, 11 insertions(+), 24 deletions(-)

diff --git a/inc/integrations/Facebook.php b/inc/integrations/Facebook.php
index f30ee8f..2812607 100644
--- a/inc/integrations/Facebook.php
+++ b/inc/integrations/Facebook.php
@@ -360,31 +360,18 @@
 	 */
 	protected function handleTheSavePost(int $postID, WP_Post $post, bool $update, array $settings): void
 	{
-		$post_type = $settings['fb_type'] ?? 'post';
-		$sync_immediately = $settings['immediate'] ?? false;
 
-		// Determine the Facebook post type
-		$fb_endpoint = self::FB_POST_TYPES[$post_type] ?? 'feed';
-
-		$operation_data = [
-			'post_id' => $postID,
-			'fb_type' => $post_type,
-			'endpoint' => $fb_endpoint,
-			'page_id' => $this->page_id
-		];
-
-		// Check for scheduled posting
-		$schedule_time = get_post_meta($postID, BASE . 'schedule_facebook', true);
-		$options = [];
-
-		if ($schedule_time && strtotime($schedule_time) > time()) {
-			$options['scheduled'] = strtotime($schedule_time);
-		} elseif (!$sync_immediately) {
-			$options['delay'] = 300; // 5 minute delay for batching
+		if (!$this->hasOAuthCredentials()) {
+			error_log('OAuth Not set up for '.$this->service_name);
+			return;
 		}
-
-		$operation = $update ? 'update_post' : 'create_post';
-		$this->queueOperation($operation, $operation_data, $options);
+		$this->queueOperation(self::$syncTo, [
+			'items'		=> [$postID],
+			'user'		=> user_can($post->post_author, 'manage_options') ? null : $post->post_author
+		], [
+			'delay'	=> 60
+		]);
+		Meta::forPost($postID)->set('_'.$this->service_name.'_sync_status', 'queued');
 	}
 
 	/**
@@ -393,7 +380,7 @@
 	public function processOperation(WP_Error|array $result, object $operation, array $data): WP_Error|array
 	{
 		try {
-			$fb = (array_key_exists('user', $data)) ? new self((int)$data['user']) : $this;
+			$fb = (array_key_exists('user', $data) && $data['user'] !== 0) ? $this->getInstance((int)$data['user']) : $this;
 			switch ($operation->type) {
 				case 'facebook_create_post':
 					return $fb->createFacebookPost($data);

--
Gitblit v1.10.0