Jake Vanderwerf
2026-09-15 01e9a8e40351a87f6548a4f55de842d6f6918ad0
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
<?php
/**
 * Google Maps Integration
 * File: /inc/integrations/GoogleMapsIntegration.php
 */
 
namespace JVBase\integrations\services;
use JVBase\integrations\Admin;
use JVBase\Instance;
use JVBase\integrations\Integrations;
use JVBase\integrations\Requests;
 
if (!defined('ABSPATH')) {
    exit;
}
 
class GoogleMaps extends Integrations
{
    use Admin, Instance, Requests;
    protected function __construct()
    {
        $this->service_name = 'maps';
        $this->title = 'Google Maps';
        $this->icon = 'map-pin';
 
        $this->apiBase = 'https://maps.googleapis.com/maps/api/';
        $this->defineFields();
        $this->defineInstructions();
        $this->defineActions();
 
        parent::__construct();
        // Auto-enqueue maps script when needed
        add_action('wp_enqueue_scripts', [$this, 'maybeEnqueueMapsScript']);
    }
        protected function defineFields():void
        {
            $pub = $this->addField('public_key', 'Maps Platform API Key', 'text', 'manage_options');
                $pub->setHint('Restrict this key by HTTP referrers for security. Used for displaying maps in browsers.');
                $pub->setSubType('password');
                $pub->setRequired();
 
            $priv = $this->addField('private_key', 'Geocoding & Places API Key', 'text', 'manage_options');
                $priv->setHint('Restrict this key by IP Addresses. Used for geocoding and server-side operations.');
                $priv->setRequired();
                $priv->setSubType('password');
 
            $map_id = $this->addField('map_id', 'Map ID', 'text', 'manage_options');
                $map_id->setHint('Required for custom styling. Create a Map ID in Google Cloud Console -> Maps -> Map Management.');
 
            $zoom = $this->addField('default_zoom', 'Default Zoom', 'text', 'manage_options');
                $zoom->setSubType('number');
                $zoom->setDefault(11);
                $zoom->setMin(1);
                $zoom->setMax(20);
 
            $lng = $this->addField('default_lng', 'Default Longitude', 'text', 'manage_options');
                $lng->setSubType('number');
                $lng->setDefault(-113.4938);
 
            $lat = $this->addField('default_lat', 'Default Latitude', 'text', 'manage_options');
                $lng->setSubType('number');
                $lng->setDefault(53.5461);
        }
 
        protected function defineInstructions():void
        {
            $this->instructions = [
                'Create two API keys in <a href="https://console.cloud.google.com/apis/credentials" target="_blank">Google Cloud Console</a>',
                'Enable <strong>Geocoding API + Places API</strong> for frontend key, restricted by your server IP address',
                'Enable <strong>Maps Platform API</strong> for server key, restricted by your website domain',
            ];
        }
 
        protected function defineActions():void
        {
            $this->addAction(
                'reverse_geocode',
                [$this, 'processReverseGeocode']
            );
 
            $this->addAction(
                'geocode',
                [$this, 'processGeocode']
            );
        }
            public function processReverseGeocode(array $data):array
            {
                if (!array_key_exists('lat', $data) || !array_key_exists('lng', $data)) {
                    return $this->response(false, 'Invalid coordinates provided', $data);
                }
                $lat = floatval($data['lat']);
                $lng = floatval($data['lng']);
 
                $result = $this->reverseGeocode($lat, $lng);
                if ($result) {
                    return $this->response(true, 'Successfully reversed Geocode', ['data'=> $result]);
                }
                return $this->response(false, 'Reverse geocoding failed');
            }
 
            public function processGeocode(array $data):array
            {
                if (!array_key_exists('address', $data)) {
                    return $this->response(false, 'No address provided');
                }
                $result = $this->geocodeAddress(sanitize_text_field($data['address']));
                if ($result) {
                    return $this->response(true, 'Successfully geocoded address', ['data' => $result]);
                }
                return $this->response(false, 'Geocoding failed');
            }
    protected function initialize(): void
    {
        // Register Google Maps API
        wp_register_script(
            'google-maps-api',
            $this->getJavaScriptApiUrl(['places', 'geometry', 'marker']),
            [],
            null,
            [
                'in_footer' => true,
                'strategy' => 'async'
            ]
        );
 
        // Register our maps handler
        wp_register_script(
            'jvb-maps',
            JVB_URL . 'assets/js/min/maps.min.js',
            ['google-maps-api'],
            '1.0.0',
            [
                'in_footer' => true,
                'strategy' => 'defer'
            ]
        );
    }
        public function getJavaScriptApiUrl(array $libraries = ['places', 'marker', 'geometry']): string
        {
            if (empty($this->getPublicKey())) {
                return '';
            }
 
            $params = [
                'key' => $this->getPublicKey(),
                'libraries' => implode(',', $libraries),
                'callback' => 'initGoogleMaps',
            ];
 
            return $this->apiBase . 'js?' . http_build_query($params);
        }
 
 
    public function maybeEnqueueMapsScript():void
    {
        if (!$this->isSetUp()) {
            return;
        }
        if (!$this->shouldEnqueueMaps()) {
            return;
        }
 
        // Add initialization script
        wp_add_inline_script('google-maps-api',
            sprintf(
            '
                // Set map defaults
                window.jvbMapDefaults = {
                    lat: %s,
                    lng: %s,
                    zoom: %s,
                    mapId: "%s"
                };
 
                // Google Maps initialization callback
                window.initGoogleMaps = function() {
                    // Dispatch ready event
                    document.dispatchEvent(new Event("googleMapsReady"));
 
                    // Call any registered callbacks
                    if (window.googleMapsCallbacks && Array.isArray(window.googleMapsCallbacks)) {
                        window.googleMapsCallbacks.forEach(function(callback) {
                            if (typeof callback === "function") {
                                callback();
                            }
                        });
                        window.googleMapsCallbacks = [];
                    }
                };
 
                // Initialize callback queue
                window.googleMapsCallbacks = window.googleMapsCallbacks || [];
                ',
                $this->getDefaultLat(),
                $this->getDefaultLng(),
                $this->getDefaultZoom(),
                esc_js($this->getMapID())
            ),
            'before');
 
        // Enqueue scripts
        wp_enqueue_script('google-maps-api');
        wp_enqueue_script('jvb-maps');
 
        // Add AJAX URL and nonce for JavaScript AJAX calls
        wp_localize_script('jvb-maps', 'jvbMapsAjax', [
            'ajaxurl' => admin_url('admin-ajax.php'),
            'nonce' => wp_create_nonce('jvb_integration_' . $this->service_name),
            'service' => $this->service_name
        ]);
    }
        protected function shouldEnqueueMaps():bool
        {
            //See if it's a singular post/page that has the map feature flagged
            if (is_singular() && get_post_meta(get_the_ID(), BASE.'has_map', true) === true) {
                return true;
            }
            if (is_tax() && get_term_meta(get_queried_object_id(), BASE . 'has_map', true) === true) {
                return true;
            }
 
            // Allow filtering
            return apply_filters('jvb_should_enqueue_maps', false);
        }
 
    public function getServiceDescription():string
    {
        return 'Allows for a map field, utilizing Google Maps';
    }
    /**********************************************************************
     * UTILITY
    **********************************************************************/
    protected function getPrivateKey():string|false
    {
        return $this->loadCredentials()['private_key']??false;
    }
    protected function getPublicKey():string|false
    {
        return $this->loadCredentials()['public_key']??false;
    }
 
    protected function getMapID():string|false
    {
        return $this->loadCredentials()['map_id']??false;
    }
    protected function getDefaultZoom():string|false
    {
        return $this->loadCredentials()['default_zoom']??false;
    }
    protected function getDefaultLng():string|false
    {
        return $this->loadCredentials()['default_lng']??false;
    }
    protected function getDefaultLat():string|false
    {
        return $this->loadCredentials()['default_lat']??false;
    }
 
 
    /**
     * Generate map application links
     */
    public function generateMapLinks(float $lat, float $lng, ?string $address = null): array
    {
        $query = $address ? urlencode($address) : "{$lat},{$lng}";
 
        return [
            'google' => "https://www.google.com/maps/search/?api=1&query={$query}",
            'apple' => "https://maps.apple.com/?ll={$lat},{$lng}",
            'waze' => "https://waze.com/ul?ll={$lat},{$lng}&navigate=yes"
        ];
    }
    private function parseAddressComponents(array $components):array
    {
        $parsed = [];
        $check = [
            'street_number' => 'long_name',
            'street'        => 'long_name',
            'city'          => 'long_name',
            'province'      => 'short_name',
            'country'       => 'short_name',
            'postal_code'   => 'long_name'
        ];
        foreach ($components as $c) {
            $types = $c['types'];
            foreach ($check as $ch => $index) {
                if (in_array($ch, $types)) {
                    $parsed[$ch] = $c[$index];
                }
            }
        }
        if (!empty($parsed['street_number']) && !empty($parsed['street'])) {
            $parsed['street'] = $parsed['street_number'] . ' ' . $parsed['street'];
        }
        return $parsed;
    }
 
    public function reverseGeocode(float $lat, float $lng):array|false
    {
        if (!$this->isSetUp()) {
            return false;
        }
        $params = [
            'latlng'    => "{$lat},{$lng}",
            'key'       => $this->getPrivateKey()
        ];
        return $this->getRequest(
            sprintf(
                '%s/geocode/json',
                $this->apiBase,
            ),
            $params
        );
    }
        protected function handleGeocodeResponse(array $data, string $method):array
        {
            if (!array_key_exists('status', $data) || $data['status'] !== 'OK' || !array_key_exists('results', $data) || empty($data['results'])) {
                return $this->response(false, 'Invalid response provided');
            }
            $result = $data['results'][0];
            return $this->response(true, 'Successfully reversed geocode',[
                'lat'               => $result['geometry']['location']['lat'],
                'lng'               => $result['geometry']['location']['lng'],
                'formatted_address' => $result['formatted_address'],
                'place_id'          => $result['place_id']??'',
                'components'        => $this->parseAddressComponents($result['address_compenents']??[])
            ]);
        }
 
    public function geocodeAddress(string $address):array|false
    {
        if (!$this->isSetUp()) {
            return false;
        }
        $params = [
            'address'   => $address,
            'key'       => $this->getPrivateKey()
        ];
        return $this->getRequest(
            sprintf(
                '%s/geocode/json',
                $this->apiBase
            )
        );
    }
 
    protected function getRequestHeaders(): array
    {
        return [
            'Accept' => 'application/json',
            'Content-Type' => 'application/json'
        ];
    }
 
    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');
        }
        $endpoint = str_replace($this->apiBase, '', $endpoint);
        return  match ($endpoint) {
            '/geocode/json' => $this->handleGeocodeResponse($response, $method),
            default => $this->response(false, 'Unhandled response for '.$endpoint.' endpoint, with method: '.$method),
        };
    }
 
    protected function performConnectionTest():bool
    {
        if (!$this->isSetUp()) {
            return false;
        }
 
        $test = 'Edmonton, Alberta, Canada';
        $result = $this->geocodeAddress($test);
        return $result['success'];
    }
}