Skip to content

Commit 7fe0d88

Browse files
committed
using wp transient cache to store geocoded data for a period of 180 days
1 parent 3acce76 commit 7fe0d88

File tree

2 files changed

+44
-38
lines changed

2 files changed

+44
-38
lines changed

awpp.css

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222
}
2323

2424
.awpp_placemark div {
25-
margin: 0 !important;
26-
}
25+
margin: 0 !important;
26+
}
2727

2828
#awpp-map-dom-element img {
2929
width: auto !important;

awpp.php

+42-36
Original file line numberDiff line numberDiff line change
@@ -304,45 +304,53 @@ private function _getMapPlacemarks( $geoData ){
304304
}
305305

306306
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+
326334
// Else decode response and handle geocoding related errors
327335
$coordinates = json_decode( $geocodeResponse['body'] );
328336
if( $this->_debug && json_last_error() != JSON_ERROR_NONE ) {
329337
print('Did not get valid json response');
330338
}
331-
339+
332340
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+
346354
// If no errors were encountered, we can go on
347355
return array( 'latitude' => $coordinates->results[ 0 ]->geometry->location->lat,
348356
'longitude' => $coordinates->results[ 0 ]->geometry->location->lng );
@@ -354,8 +362,6 @@ private function _googleGeocode( $address ) {
354362
register_deactivation_hook(__FILE__, array('AWPP_Init', 'deactivate') );
355363

356364
$awpp = new AWPP_Init;
357-
358365
}
359366

360-
361-
?>
367+
?>

0 commit comments

Comments
 (0)