Jake Vanderwerf
2026-08-07 a8ff85042ffbfe94b678fd818c02deb613bfd116
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
<?php
namespace JVBase\integrations\services;
use Exception;
use JVBase\integrations\Admin;
use JVBase\integrations\Instance;
use JVBase\integrations\Integrations;
use JVBase\integrations\OAuth;
use JVBase\integrations\OAuthCredentials;
use JVBase\integrations\OAuthURLs;
use JVBase\integrations\Requests;
use JVBase\integrations\UserConnection;
 
 
if (!defined('ABSPATH')) {
    exit;
}
 
class GoogleMyBusiness extends Integrations
{
    use Admin, Instance, OAuth, Requests, UserConnection;
 
    protected string $readMask = 'name,title,storefrontAddress,metadata,openInfo,storeCode,categories,phoneNumbers,labels,specialHours';
 
    protected function __construct()
    {
        $this->service_name = 'gmb';
        $this->title = 'Google My Business';
        $this->icon = 'storefront';
 
        $this->canSync = [
            'initial'   => true,
            'update'    => true,
            'delete'    => true,
        ];
        $this->supportsWebp = false;
 
        $this->defineFields();
        $this->defineInstructions();
        $this->defineOAuth();
        $this->defineActions();
 
        parent::__construct();
    }
        protected function defineFields():void
        {
            $client_id = $this->addField(
                'client_id',
                'OAuth Client ID',
                'text',
                'manage_options'
            );
                $client_id->setRequired();
                $client_id->setHint('From your Google Cloud Console OAuth credentials.');
 
            $secret = $this->addField(
                'client_secret',
                'OAuth Client Secret',
                'text',
                'manage_options',
            );
                $secret->setSubType('password');
                $secret->setRequired();
 
            if ($this->getAccessToken()) {
                $accs = $this->addField('accounts', 'Accounts', 'select', [$this, 'testUserCanConnect']);
                    $accs->setHidden();
                    $accs->setOptions($this->getAccountOptions());
 
                $acc = $this->addField('account_id', 'Set Account', 'select', [$this, 'testUserCanConnect']);
                    $acc->setOptions($this->loadCredentials()['accounts']??[]);
                    $acc->setHint('Select the account to connect to.');
 
                if ($this->getAccountID()) {
                    $locs = $this->addField('locations', 'Locations', 'select', [$this, 'testUserCanConnect']);
                    $locs->setHidden();
                    $locs->setOptions($this->getLocationOptions());
 
                    $loc = $this->addField('location_id', 'Set Location', 'select', [$this, 'testUserCanConnect']);
                    $loc->setOptions($this->loadCredentials()['locations']??[]);
                    $loc->setHint('Select the location to connect to.');
                }
            }
 
        }
            public function testUserCanConnect():bool
            {
                $user = get_current_user_id();
                return $this->userCanConnect($user);
            }
        protected function defineInstructions():void
        {
            $this->instructions = [
                'Go to <a href="https://console.cloud.google.com/apis/credentials" target="_blank">Google Cloud Console</a>',
                'Create OAuth 2.0 Client ID credentials',
                'Enable Google My Business API',
                'Add redirect URI: <code>'.esc_html($this->getRedirectUri()).'</code>',
                'Use the "Authorize" button below to complete OAuth flow'
            ];
        }
        protected function defineOAuth():void
        {
            $this->oauth = new OAuthURLs(
                'https://accounts.google.com/o/oauth2/v2/auth',
                'https://oauth2.googleapis.com/token',
                'https://oauth2.googleapis.com/revoke'
            );
 
            $this->oathScopes = [
                'https://www.googleapis.com/auth/business.manage',
                'https://www.googleapis.com/auth/userinfo.email',
                'https://www.googleapis.com/auth/userinfo.profile',
                'https://www.googleapis.com/auth/plus.business.manage'
            ];
        }
        protected function defineActions():void
        {
            $this->addAction(
                'Update Location',
                [$this, 'handleUpdateLocation'],
                'update_location',
            );
        }
            public function handleUpdateLocation(array $data):array
            {
                if (empty($data['location_id']??'')) {
                    return $this->response(false, 'No location selected');
                }
 
                $credentials = [
                    'location_id'   => sanitize_text_field($data['location_id'])
                ];
                return $this->saveCredentials($credentials, false);
            }
 
    protected function initialize(): void
    {
        // TODO: Implement initialize() method.
    }
 
    protected function getRequestHeaders(): array
    {
        // TODO: Implement getRequestHeaders() method.
    }
 
    protected function handleResponse(\WP_Error|array $response, string $method, string $endpoint): array
    {
        if (is_wp_error($response) && $response->get_error_message() === 'rate_limit_exceeded') {
            return $this->response(false, 'Rate limit exceeded');
        }
 
        return match($endpoint) {
            'https://mybusinessaccountmanagement.googleapis.com/v1/accounts' => $this->handleGetAccountsResponse($response, $method),
            sprintf('https://mybusinessbusinessinformation.googleapis.com/v1/%s/locations', $this->getAccountID()??'') => $this->handleGetLocationsResponse($response, $method),
            default => $this->response(false, 'Unhandled response for '.$endpoint.' endpoint, with method: '.$method)
        };
    }
 
    protected function refreshAccessToken(OAuthCredentials $credentials): array
    {
        // TODO: Implement refreshAccessToken() method.
    }
 
    /*********************************************************
     * UTILITY
    *********************************************************/
    protected function getAccountID():string|false
    {
        return $this->loadCredentials()['account_id']??false;
    }
    protected function getAccountOptions(bool $force = false):array
    {
        $stored = $this->loadCredentials()['accounts']??[];
        if ($force || empty($stored)) {
            $stored = $this->getAccounts();
        }
        return $stored;
    }
 
    protected function getLocationID():string|false
    {
        return $this->loadCredentials()['location_id']??false;
    }
    protected function getLocationOptions(bool $force = false):array
    {
        $stored = $this->loadCredentials()['locations']??[];
        if ($force || empty($stored)) {
            $stored = $this->getLocations();
        }
        return $stored;
    }
    protected function getLocations():array|false
    {
        if (!$this->getAccessToken() || !$this->getAccountID()) {
            return false;
        }
        try {
            return $this->getRequest(
                sprintf(
                    '/v1/%s/locations',
                    $this->getAccountID()
                ),
                [
                    'readMask'  => $this->readMask
                ]
            );
        } catch (Exception $e)
        {
            $this->logError('getLocations', 'Could not get locations: '.$e->getMessage());
            return [];
        }
    }
        protected function handleGetLocationsResponse(array $response, string $method):array
        {
            $locations = [];
            if (isset($response['locations'])) {
                foreach ($response['locations'] as $loc) {
                    if (isset($loc['name']) && isset($loc['title'])) {
                        $locations[$loc['name']] = $loc['title'];
                    }
                }
            }
            return $locations;
        }
 
    protected function getAccounts(bool $force = false):array|false
    {
        if (!$this->getAccessToken()) {
            return false;
        }
 
        return $this->getRequest(
            'https://mybusinessaccountmanagement.googleapis.com/v1/accounts',
            [],
            $force
        );
    }
        protected function handleGetAccountsResponse(array $response, string $method):array
        {
            $accounts = [];
            if (isset($response['accounts']) && is_array($response['accounts'])) {
                $accounts = $response['accounts'];
            } elseif (is_array($response) && isset($response['name'])) {
                $accounts = [$response];
            }
            $options = [];
            foreach ($accounts as $account) {
                if (isset($account['name']) && isset($account['accountName'])) {
                    $options[$account['name']] = $account['accountName'];
                }
            }
            return $options;
        }
}