Jake Vanderwerf
2026-08-28 a9654f00c8fbc9934289d761843ddd071025013f
inc/integrations/services/GoogleMyBusinessOld.php
@@ -1,7 +1,10 @@
<?php
namespace JVBase\integrations\services;
use Exception;
use JVBase\integrations\Integrations;
use JVBase\meta\Meta;
use WP_Error;
use WP_Post;
if (!defined('ABSPATH')) {
   exit;
@@ -143,9 +146,7 @@
         ]
      );
      if (JVB_TESTING) {
         $this->cache->flush();
      }
   }
   protected function initialize(): void
@@ -174,7 +175,7 @@
      try {
         $accounts = $this->getAccounts(true);
         return !empty($accounts);
      } catch (\Exception $e) {
      } catch (Exception $e) {
         return false;
      }
   }
@@ -186,7 +187,7 @@
   {
      // Google APIs return errors in this format:
      // {"error": {"code": 401, "message": "...", "status": "UNAUTHENTICATED"}}
      return isset($response['error']) && isset($response['error']['code']);
      return isset($response['error']['code']);
   }
   protected function getRequestHeaders(): array
@@ -214,7 +215,7 @@
      return $credentials;
   }
   public function handleTheSavePost(int $postID, \WP_Post $post, bool $update, array $settings):void
   public function handleTheSavePost(int $postID, WP_Post $post, bool $update, array $settings):void
   {
      if (!$this->hasOAuthCredentials()) {
@@ -236,33 +237,18 @@
      try {
         $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':
            case 'gmb_update_menu_item':
            case 'gmb_menu_update':
            case 'gmb_menu_update_' . $this->userID:
               return $gmb->processMenuUpdate($operation, $data);
            case 'gmb_create_event':
            case 'gmb_update_event':
               return $gmb->syncEventPost($operation, $data);
            case 'gmb_create_offer':
            case 'gmb_update_offer':
               return $gmb->syncOfferPost($operation, $data);
            case 'gmb_create_post':
            case 'gmb_update_post':
               return $gmb->syncStandardPost($operation, $data);
            case 'gmb_specialHours':
               return $gmb->setSpecialHours($data['date'], $data['data']);
            case 'gmb_business_hours':
               return $gmb->updateBusinessHours($data);
            case 'gmb_business_info':
               return $gmb->updateBusinessInfo($data);
            case 'gmb_update_attributes':
               return $gmb->updateAttributes($data);
            default:
               return $result;
         }
      } catch (\Exception $e) {
         return match ($operation->type) {
            'gmb_create_menu_item', 'gmb_update_menu_item', 'gmb_menu_update', 'gmb_menu_update_' . $this->userID => $gmb->processMenuUpdate($operation, $data),
            'gmb_create_event', 'gmb_update_event' => $gmb->syncEventPost($operation, $data),
            'gmb_create_offer', 'gmb_update_offer' => $gmb->syncOfferPost($operation, $data),
            'gmb_create_post', 'gmb_update_post' => $gmb->syncStandardPost($operation, $data),
            'gmb_specialHours' => $gmb->setSpecialHours($data['date'], $data['data']),
            'gmb_business_hours' => $gmb->updateBusinessHours($data),
            'gmb_business_info' => $gmb->updateBusinessInfo($data),
            'gmb_update_attributes' => $gmb->updateAttributes($data),
            default => $result,
         };
      } catch (Exception $e) {
         error_log('[GMB] Operation processing error: ' . $e->getMessage());
         return [
            'success' => false,
@@ -479,7 +465,7 @@
            'result' => (is_array($result)) ? array_merge($result, ['menu'=>$menu_data]) : ['menu'=>$menu_data],
         ];
      } catch (\Exception $e) {
      } catch (Exception $e) {
         error_log('GMB Menu Update Error: ' . $e->getMessage());
         return [
            'success'   => false,
@@ -516,7 +502,7 @@
               }
            }
         }
      } catch (\Exception $e) {
      } catch (Exception $e) {
         error_log('[GMB] Error finding location resource name: ' . $e->getMessage());
      }
@@ -698,7 +684,7 @@
         $location_name = $this->getSelectedLocationResourceName();
         if (empty($patch_data)) {
            throw new \Exception('No valid fields to update');
            throw new Exception('No valid fields to update');
         }
         // Create updateMask from the fields being updated
@@ -716,7 +702,7 @@
            'result' => $response
         ];
      } catch (\Exception $e) {
      } catch (Exception $e) {
         $this->logError($e->getMessage(), [
            'method' => 'updateBusinessInfo'
         ]);
@@ -758,7 +744,7 @@
            'result' => $response
         ];
      } catch (\Exception $e) {
      } catch (Exception $e) {
         $this->logError($e->getMessage(), [
            'method' => 'updateAttributes'
         ]);
@@ -919,7 +905,7 @@
      try {
         // Validate required fields
         if (empty($post_data['summary'])) {
            throw new \Exception('Post summary is required');
            throw new Exception('Post summary is required');
         }
         // Build the post object according to API docs
@@ -960,7 +946,7 @@
         return $response;
      } catch (\Exception $e) {
      } catch (Exception $e) {
         $this->logError($e->getMessage(), [
            'method' => 'createPost'
         ]);
@@ -1000,7 +986,7 @@
         return !empty($response);
      } catch (\Exception $e) {
      } catch (Exception $e) {
         $this->logError($e->getMessage(), [
            'method' => 'updatePost'
@@ -1017,7 +1003,7 @@
      try {
         $this->makeRequest('DELETE', "/v1/accounts/{$post_name}", [], 'posts');
         return true;
      } catch (\Exception $e) {
      } catch (Exception $e) {
         $this->logError($e->getMessage(), [
            'method' => 'deletePost'
@@ -1043,7 +1029,7 @@
      if (empty($location_name)) {
         error_log('[GMB] ERROR: No location selected or found');
         throw new \Exception('No location selected. Please select a location first.');
         throw new Exception('No location selected. Please select a location first.');
      }
      try {
@@ -1052,7 +1038,7 @@
         if (empty($periods)) {
            error_log('[GMB] ERROR: No valid periods found after validation');
            throw new \Exception('No valid business hours provided');
            throw new Exception('No valid business hours provided');
         }
         $update_data = [
@@ -1088,7 +1074,7 @@
            'result' => $response
         ];
      } catch (\Exception $e) {
      } catch (Exception $e) {
         $this->logError($e->getMessage(), [
            'method' => 'updateBusinessHours'
         ]);
@@ -1178,7 +1164,7 @@
      $location_name = $this->getSelectedLocationResourceName();
      if (empty($location_name)) {
         throw new \Exception('No location selected. Please select a location first.');
         throw new Exception('No location selected. Please select a location first.');
      }
      try {
@@ -1207,8 +1193,8 @@
               }
            }
            if (!empty($options['close'])) {
               $close_time = $this->formatTime($options['close']);
            if (!empty($options['closed'])) {
               $close_time = $this->formatTime($options['closed']);
               if ($close_time) {
                  $special_hour_period['closeTime'] = $close_time;
               }
@@ -1253,7 +1239,7 @@
            'results'   => $response
         ];
      } catch (\Exception $e) {
      } catch (Exception $e) {
         $this->logError($e->getMessage(), [
            'method' => 'setSpecialHours'
         ]);
@@ -1282,7 +1268,7 @@
         return $response !== null;
      } catch (\Exception $e) {
      } catch (Exception $e) {
         $this->logError($e->getMessage(), [
            'method' => 'clearSpecialHours'
@@ -1338,7 +1324,7 @@
         ];
         if (!in_array($category, $valid_categories)) {
            throw new \Exception('Invalid photo category: ' . $category);
            throw new Exception('Invalid photo category: ' . $category);
         }
         $mediaKey = $this->getImageMediaKey($imgID);
         if ($mediaKey) {
@@ -1366,7 +1352,7 @@
            return $mediaKey;
         }
         return null;
      } catch (\Exception $e) {
      } catch (Exception $e) {
         $this->logError($e->getMessage(), [
            'method' => 'uploadPhotoFromUrl'
         ]);
@@ -1425,10 +1411,10 @@
   {
      $this->ensureInitialized();
      if (!$this->location) {
         throw new \Exception('No location selected');
         throw new Exception('No location selected');
      }
      if (!$this->account_id) {
         throw new \Exception('No account configured');
         throw new Exception('No account configured');
      }
      $location = $this->getSelectedLocationResourceName();
@@ -1459,7 +1445,7 @@
         return $reviews;
      } catch (\Exception $e) {
      } catch (Exception $e) {
         $this->logError($e->getMessage(), [
            'method' => 'getReviews'
         ]);
@@ -1494,7 +1480,7 @@
         return null;
      } catch (\Exception $e) {
      } catch (Exception $e) {
         $this->logError('Failed to get reviews view URL: ' . $e->getMessage(), [
            'method' => 'getReviewsViewUrl'
         ]);
@@ -1529,7 +1515,7 @@
         return null;
      } catch (\Exception $e) {
      } catch (Exception $e) {
         $this->logError('Failed to get review URL: ' . $e->getMessage(), [
            'method' => 'getReviewUrl'
         ]);
@@ -1557,9 +1543,9 @@
      try {
         $response = $this->getRequest("/v1/{$account_name}/locations", $params,'accounts', $ttl, $force);
         return isset($response['locations']) ? $response['locations'] : [];
         return $response['locations'] ?? [];
      } catch (\Exception $e) {
      } catch (Exception $e) {
         error_log('[GMB] Error getting locations for ' . $account_name . ': ' . $e->getMessage());
         return [];
      }
@@ -1612,7 +1598,7 @@
            'message' => 'No accounts found'
         ];
      } catch (\Exception $e) {
      } catch (Exception $e) {
         error_log('[GMB] Error refreshing stored data: ' . $e->getMessage());
         return [
            'success' => false,
@@ -1706,7 +1692,7 @@
            ?>
         </div>
         <?php
      } catch (\Exception $e) {
      } catch (Exception $e) {
         echo '<p>Error loading business locations.</p>';
      }
   }
@@ -1840,7 +1826,7 @@
         }
         if (!$selected_account) {
            throw new \Exception('Invalid account selected');
            throw new Exception('Invalid account selected');
         }
         // Update credentials with selected account
@@ -1855,7 +1841,7 @@
            'reload' => true
         ];
      } catch (\Exception $e) {
      } catch (Exception $e) {
         $this->logError('Failed to select account', [
            'error' => $e->getMessage(),
            'data' => $data
@@ -1884,12 +1870,12 @@
         }
         if (empty($location_name)) {
            throw new \Exception('No location selected');
            throw new Exception('No location selected');
         }
         // Verify this is a valid location for the selected account
         if (empty($this->credentials['account'])) {
            throw new \Exception('No account selected. Please select an account first.');
            throw new Exception('No account selected. Please select an account first.');
         }
         $locations = $this->getLocations($this->credentials['account']);
@@ -1904,7 +1890,7 @@
         }
         if (!$valid_location) {
            throw new \Exception('Invalid location selected');
            throw new Exception('Invalid location selected');
         }
         // Update credentials with selected location
@@ -1922,7 +1908,7 @@
            'reload' => false  // No need to reload for location selection
         ];
      } catch (\Exception $e) {
      } catch (Exception $e) {
         $this->logError('Failed to select location', [
            'error' => $e->getMessage(),
            'data' => $data
@@ -1950,7 +1936,7 @@
            'has_refresh_token' => !empty($this->credentials['refresh_token'])
         ];
      } catch (\Exception $e) {
      } catch (Exception $e) {
         return [
            'success' => false,
            'valid' => false,
@@ -2065,7 +2051,7 @@
   public function getFoodMenus(): ?array
   {
      if (!$this->credentials['location']) {
         throw new \Exception('No location selected. Please select a location first.');
         throw new Exception('No location selected. Please select a location first.');
      }
      $response = $this->getRequest(
@@ -2082,7 +2068,7 @@
   public function updateFoodMenus(array $menus): bool
   {
      if (!$this->credentials['location']) {
         throw new \Exception('No location selected. Please select a location first.');
         throw new Exception('No location selected. Please select a location first.');
      }
      try {
@@ -2104,7 +2090,7 @@
         ]);
         return !empty($response);
      } catch (\Exception $e) {
      } catch (Exception $e) {
         $this->logError($e->getMessage(), [
            'method' => 'updateFoodMenus'
         ]);
@@ -2166,7 +2152,7 @@
   {
      // Extract mediaKey from format: accounts/{accountId}/locations/{locationId}/media/{mediaKey}
      $parts = explode('/media/', $mediaResourceName);
      return isset($parts[1]) ? $parts[1] : '';
      return $parts[1] ?? '';
   }
   /**
@@ -2175,7 +2161,7 @@
   public function getLocationMedia(): array
   {
      if (!$this->credentials['location']) {
         throw new \Exception('No location selected. Please select a location first.');
         throw new Exception('No location selected. Please select a location first.');
      }
      try {
@@ -2186,7 +2172,7 @@
         );
         return $response['mediaItems'] ?? [];
      } catch (\Exception $e) {
      } catch (Exception $e) {
         $this->logError('Failed to get location media: ' . $e->getMessage());
         return [];
      }
@@ -2214,7 +2200,7 @@
            'cached' => !$force
         ];
      } catch (\Exception $e) {
      } catch (Exception $e) {
         return new WP_Error('failure', 'Something went wrong: '.$e->getMessage());
      }
   }
@@ -2235,7 +2221,7 @@
         }
         if (empty($selected_account)) {
            throw new \Exception('No account selected');
            throw new Exception('No account selected');
         }
         // Save the selected account
@@ -2259,7 +2245,7 @@
            'reload' => true  // Trigger page refresh to show location dropdown
         ];
      } catch (\Exception $e) {
      } catch (Exception $e) {
         $this->logError('Failed to update location', [
            'error' => $e->getMessage(),
            'data' => $data
@@ -2279,7 +2265,7 @@
         $this->cache->flush();
         return true;
      } catch (\Exception $e) {
      } catch (Exception $e) {
         error_log('[GMB] Error clearing stored data: ' . $e->getMessage());
         return false;
      }
@@ -2288,13 +2274,13 @@
   public function getPriceLists(): ?array
   {
      if (!$this->credentials['location']) {
         throw new \Exception('No location selected. Please select a location first.');
         throw new Exception('No location selected. Please select a location first.');
      }
      try {
         $location_data = $this->getLocation($this->credentials['location']);
         return $location_data['priceLists'] ?? [];
      } catch (\Exception $e) {
      } catch (Exception $e) {
         $this->logError($e->getMessage(), [
            'method' => 'getPriceLists'
@@ -2389,7 +2375,7 @@
      $location_name = $this->getSelectedLocationResourceName();
      if (empty($location_name)) {
         throw new \Exception('No location selected. Please select a location first.');
         throw new Exception('No location selected. Please select a location first.');
      }
      $post_data = [
@@ -2416,7 +2402,7 @@
      $location_name = $this->getSelectedLocationResourceName();
      if (empty($location_name)) {
         throw new \Exception('No location selected. Please select a location first.');
         throw new Exception('No location selected. Please select a location first.');
      }
      return $this->updateBusinessInfo($location_name, $updates);
@@ -2432,7 +2418,7 @@
      $location_name = $this->getSelectedLocationResourceName();
      if (empty($location_name)) {
         throw new \Exception('No location selected. Please select a location first.');
         throw new Exception('No location selected. Please select a location first.');
      }
      return $this->clearSpecialHours($location_name);
@@ -2531,7 +2517,7 @@
      return [$gmb_menu]; // GMB expects an array of menus
   }
   protected function buildMenuItem(\WP_Post $item, array $defaults):array
   protected function buildMenuItem(WP_Post $item, array $defaults):array
   {
      $meta = Meta::forPost($item->ID);
      $fields = $this->mappedMenuFields($item->post_type);