@@ -304,45 +304,53 @@ private function _getMapPlacemarks( $geoData ){
304
304
}
305
305
306
306
private function _googleGeocode ( $ address ) {
307
- $ geocodeResponse = wp_remote_get (
308
- sprintf ( 'http://maps.googleapis.com/maps/api/geocode/json?address=%s&sensor=false ' ,
309
- str_replace ( ' ' , '+ ' , $ address )
310
- )
311
- );
312
- if ( $ this ->_debug && is_wp_error ( $ geocodeResponse )) {
313
- print ('Something went wrong: ' . $ geocodeResponse ->get_error_message ());
314
- }
315
- if ( $ this ->_debug && $ geocodeResponse [ 'response ' ][ 'code ' ] != 200 ) {
316
- printf (
317
- __ ( '<p>%s geocode error: %d %s</p> <p>Response: %s</p> ' , self ::PREFIX ),
318
- self ::PREFIX ,
319
- $ geocodeResponse [ 'response ' ][ 'code ' ],
320
- $ geocodeResponse [ 'response ' ][ 'message ' ],
321
- strip_tags ( $ geocodeResponse [ 'body ' ] )
322
- );
323
- return false ;
324
- }
325
-
307
+ $ key = md5 ( $ address );
308
+
309
+ if ( false === ( $ geocodeResponse = get_transient ( $ key ) ) ){
310
+ $ this ->_debug && printf ('Fetching data for address "%s" from google.<br/> ' , $ address );
311
+
312
+ $ geocodeResponse = wp_remote_get (
313
+ sprintf ( 'http://maps.googleapis.com/maps/api/geocode/json?address=%s&sensor=false ' ,
314
+ str_replace ( ' ' , '+ ' , $ address )
315
+ )
316
+ );
317
+ if ( $ this ->_debug && is_wp_error ( $ geocodeResponse )) {
318
+ print ('Something went wrong: ' . $ geocodeResponse ->get_error_message ());
319
+ }
320
+ if ( $ this ->_debug && $ geocodeResponse [ 'response ' ][ 'code ' ] != 200 ) {
321
+ printf (
322
+ __ ( '<p>%s geocode error: %d %s</p> <p>Response: %s</p> ' , self ::PREFIX ),
323
+ self ::PREFIX ,
324
+ $ geocodeResponse [ 'response ' ][ 'code ' ],
325
+ $ geocodeResponse [ 'response ' ][ 'message ' ],
326
+ strip_tags ( $ geocodeResponse [ 'body ' ] )
327
+ );
328
+ return false ;
329
+ }
330
+
331
+ set_transient ( $ key , $ geocodeResponse , 3600 * 24 * 180 );
332
+ }
333
+
326
334
// Else decode response and handle geocoding related errors
327
335
$ coordinates = json_decode ( $ geocodeResponse ['body ' ] );
328
336
if ( $ this ->_debug && json_last_error () != JSON_ERROR_NONE ) {
329
337
print ('Did not get valid json response ' );
330
338
}
331
-
339
+
332
340
if ( $ this ->_debug && isset ( $ coordinates ->status ) && $ coordinates ->status == 'REQUEST_DENIED ' ) {
333
- printf ( __ ( '%s geocode error: Request Denied. ' , self ::PREFIX ), self ::PREFIX );
334
- return false ;
335
- }
336
-
337
- if ( ( $ this ->_debug && !isset ( $ coordinates ->results ) ) || ($ this ->_debug && empty ( $ coordinates ->results ) ) ) {
338
- printf (
339
- __ ( "%s geocode error: The address (%s) couldn't be geocoded, please make sure that it's correct. " ),
340
- self ::PREFIX ,
341
- $ address
342
- );
343
- return false ;
344
- }
345
-
341
+ printf ( __ ( '%s geocode error: Request Denied. ' , self ::PREFIX ), self ::PREFIX );
342
+ return false ;
343
+ }
344
+
345
+ if ( ( $ this ->_debug && !isset ( $ coordinates ->results ) ) || ($ this ->_debug && empty ( $ coordinates ->results ) ) ) {
346
+ printf (
347
+ __ ( "%s geocode error: The address (%s) couldn't be geocoded, please make sure that it's correct. " ),
348
+ self ::PREFIX ,
349
+ $ address
350
+ );
351
+ return false ;
352
+ }
353
+
346
354
// If no errors were encountered, we can go on
347
355
return array ( 'latitude ' => $ coordinates ->results [ 0 ]->geometry ->location ->lat ,
348
356
'longitude ' => $ coordinates ->results [ 0 ]->geometry ->location ->lng );
@@ -354,8 +362,6 @@ private function _googleGeocode( $address ) {
354
362
register_deactivation_hook (__FILE__ , array ('AWPP_Init ' , 'deactivate ' ) );
355
363
356
364
$ awpp = new AWPP_Init ;
357
-
358
365
}
359
366
360
-
361
- ?>
367
+ ?>
0 commit comments