Making WordPress.org


Ignore:
Timestamp:
03/01/2018 01:55:23 AM (7 years ago)
Author:
iandunn
Message:

Official WordPress Events: Cache responses from Geocoding API.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/official-wordpress-events/official-wordpress-events.php

    r6779 r6811  
    344344                        } else {
    345345                            $geocoded_location = $this->reverse_geocode( $meetup->group->group_lat, $meetup->group->group_lon );
    346                             $location_parts    = $this->parse_reverse_geocode_address( $geocoded_location->address_components );
     346                            $location_parts    = $this->parse_reverse_geocode_address( $geocoded_location );
    347347                            $location          = sprintf(
    348348                                '%s%s%s',
     
    444444    protected function reverse_geocode( $latitude, $longitude ) {
    445445        $address  = false;
     446        $cache_key       = 'geocode_' . md5( $latitude, $longitude );
     447        $cached_response = wp_cache_get( $cache_key, 'events' );
     448
     449        if ( false !== $cached_response ) {
     450            return $cached_response;
     451        }
     452
     453        // Rough attempt at avoiding rate limit.
     454        usleep( 75000 );
     455
    446456        $response = $this->remote_get( sprintf(
    447457            'https://maps.googleapis.com/maps/api/geocode/json?latlng=%s,%s&sensor=false',
     
    455465            $body = json_decode( wp_remote_retrieve_body( $response ) );
    456466
    457             if ( isset( $body->results[0] ) ) {
    458                 $address = $body->results[0];
     467            if ( isset( $body->results[0]->address_components ) ) {
     468                $address = $body->results[0]->address_components;
     469                wp_cache_set( $cache_key, $address, 'events', 0 );
    459470            }
    460471        }
Note: See TracChangeset for help on using the changeset viewer.