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/GoogleMyBusiness.php | 86 +++++++++++++++++++++---------------------
1 files changed, 43 insertions(+), 43 deletions(-)
diff --git a/inc/integrations/GoogleMyBusiness.php b/inc/integrations/GoogleMyBusiness.php
index 6e823b2..a66a81a 100644
--- a/inc/integrations/GoogleMyBusiness.php
+++ b/inc/integrations/GoogleMyBusiness.php
@@ -1,8 +1,7 @@
<?php
namespace JVBase\integrations;
-use JVBase\meta\MetaManager;
-use JVBase\managers\CacheManager;
+use JVBase\meta\Meta;
use WP_Error;
if (!defined('ABSPATH')) {
exit;
@@ -10,6 +9,11 @@
class GoogleMyBusiness extends Integrations
{
+ protected array $allowedContent = [
+ 'menu_item',
+ 'post',
+ 'event',
+ ];
private ?string $access_token = null;
protected string $readMask = 'name,title,storefrontAddress,metadata,openInfo,storeCode,categories,phoneNumbers,labels,specialHours';
private ?string $location = null;
@@ -18,7 +22,17 @@
private ?string $client_secret = null;
private ?string $account_id = null;
- 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 = 'gmb';
$this->title = 'Google My Business';
@@ -130,7 +144,7 @@
);
if (JVB_TESTING) {
- $this->cache->clear();
+ $this->cache->flush();
}
}
@@ -202,39 +216,25 @@
public function handleTheSavePost(int $postID, \WP_Post $post, bool $update, array $settings):void
{
- $type = $settings['content_type']??'post'; //can be 'post', 'offer', 'event', 'hours', 'info',
- $initial = $settings['initial']?? false;
- $syncOnUpdate = $settings['update']??false;
- if ($update && !$syncOnUpdate) {
- return;
- }
- if (!$initial) {
+
+ if (!$this->hasOAuthCredentials()) {
+ error_log('OAuth Not set up for '.$this->service_name);
return;
}
- $options = $data = [];
- switch ($type) {
- case 'menu_item':
- $options = ['delay' => 60];
- break;
- }
- if (!user_can($post->post_author, 'manage_options')) {
- $data['user'] = $post->post_author;
- }
- $data['post_id'] = $postID;
- $operation = ($update) ? 'update_' : 'create_';
-
- $this->queueOperation(
- $operation.$type,
- $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');
}
public function processOperation(\WP_Error|array $result, object $operation, array $data):\WP_Error|array
{
try {
- $gmb = (array_key_exists('user', $data)) ? new self((int)$data['user']) : $this;
+ $gmb = (array_key_exists('user', $data) && $data['user'] !== 0) ? $this->getInstance((int)$data['user']) : $this;
$gmb->ensureInitialized();
switch ($operation->type) {
case 'gmb_create_menu_item':
@@ -283,7 +283,7 @@
}
$postID = $data['post_id'];
- $meta = new MetaManager($postID, 'post');
+ $meta = Meta::forPost($postID);
$fields = [
'start_date',
'end_date',
@@ -319,7 +319,7 @@
$result = $this->updatePost($fields["_{$this->service_name}_item_id"], $data);
} else {
$result = $this->createPost($data);
- $meta->updateValue("_{$this->service_name}_item_id", $result['name']);
+ $meta->set("_{$this->service_name}_item_id", $result['name']);
}
return [
@@ -340,7 +340,7 @@
}
$postID = $data['post_id'];
- $meta = new MetaManager($postID, 'post');
+ $meta = Meta::forPost($postID);
$fields = [
'post_excerpt',
'post_title',
@@ -372,7 +372,7 @@
$result = $this->updatePost($fields["_{$this->service_name}_item_id"], $data);
} else {
$result = $this->createPost($data);
- $meta->updateValue("_{$this->service_name}_item_id", $result['name']);
+ $meta->set("_{$this->service_name}_item_id", $result['name']);
}
return [
@@ -394,7 +394,7 @@
}
$postID = $data['post_id'];
- $meta = new MetaManager($postID, 'post');
+ $meta = Meta::forPost($postID);
$fields = [
'post_excerpt',
'post_title',
@@ -423,7 +423,7 @@
$result = $this->updatePost($fields["_{$this->service_name}_item_id"], $data);
} else {
$result = $this->createPost($data);
- $meta->updateValue("_{$this->service_name}_item_id", $result['name']);
+ $meta->set("_{$this->service_name}_item_id", $result['name']);
}
return [
@@ -467,7 +467,7 @@
];
}
- $gmb = new self($data['userID']??null);
+ $gmb = self::getInstance($data['userID']??null);
// Build the complete menu structure
$menu_data = $gmb->collectMenu($menu_items);
@@ -2276,7 +2276,7 @@
{
try {
// Use the static method to clear the entire cache group
- $this->cache->clear();
+ $this->cache->flush();
return true;
} catch (\Exception $e) {
@@ -2471,7 +2471,7 @@
protected function collectMenu(array $menu_items): array
{
- $defaultMeta = new MetaManager($this->userID, 'integrations');
+ $defaultMeta = Meta::forOptions($this->userID.'_integrations');
$defaults = ['menu_name', 'menu_description', 'default_section', 'cuisines', 'source_url', 'language', 'default_currency'];
$defaults = $defaultMeta->getAll($defaults);
@@ -2532,7 +2532,7 @@
protected function buildMenuItem(\WP_Post $item, array $defaults):array
{
- $meta = new MetaManager($item->ID, 'post');
+ $meta = Meta::forPost($item->ID);
$fields = $this->mappedMenuFields($item->post_type);
$values = $meta->getAll(array_values($fields));
@@ -2661,8 +2661,8 @@
protected function getMenuSectionsOrder(array $sections_map):array
{
- $optionsMeta = new MetaManager(null, 'options');
- $sectionOrder = $optionsMeta->getValue('menu_section_order');
+ $optionsMeta = Meta::forOptions('options');
+ $sectionOrder = $optionsMeta->get('menu_section_order');
// Build final GMB menu structure
$ordered = [];
@@ -2698,8 +2698,8 @@
// Collect cuisines from individual items if specified
foreach ($menu_items as $item) {
- $meta = new MetaManager($item->ID, 'post');
- $item_cuisines = $meta->getValue('cuisines');
+ $meta = Meta::forPost($item->ID);
+ $item_cuisines = $meta->get('cuisines');
if (!empty($item_cuisines)) {
$item_cuisines = is_array($item_cuisines) ?
--
Gitblit v1.10.0