| | |
| | | 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'; |
| | |
| | | |
| | | 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': |
| | |
| | | ]; |
| | | } |
| | | |
| | | $gmb = new self($data['userID']??null); |
| | | $gmb = self::getInstance($data['userID']??null); |
| | | |
| | | // Build the complete menu structure |
| | | $menu_data = $gmb->collectMenu($menu_items); |