Making WordPress.org

Changeset 4881


Ignore:
Timestamp:
02/04/2017 10:56:31 AM (10 years ago)
Author:
dd32
Message:

Events: Add caching and expire headers to responses.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/api.wordpress.org/public_html/events/1.0/index.php

    r4878 r4881  
    5555$events = get_events( $event_args );
    5656
     57header( 'Expires: ' . gmdate( 'r', time() + $ttl ) );
     58header( 'Access-Control-Allow-Origin: *' );
    5759header( 'Content-Type: application/json; charset=UTF-8' );
    5860echo wp_json_encode( compact( 'location', 'events', 'ttl', 'debug' ) );
     61
    5962
    6063function guess_location_from_geonames( $location_name, $timezone, $country ) {
    … …  
    8386        if ( isset( $args['country'] ) ) {
    8487                return array(
    85                         // TODO include a 'description' key of the country name?
    8688                        'country'     => $args['country'],
    8789                );
    … …  
    118120        }
    119121                               
    120         if ( !empty( $args['latitude'] ) && ! empty( $args['longitude'] ) ) {
     122        if (
     123                ! empty( $args['latitude'] )  && is_numeric( $args['latitude'] ) &&
     124                ! empty( $args['longitude'] ) && is_numeric( $args['longitude'] )
     125        ) {
    121126                // TODO: Ensure that the data here is rounded to city-level and return the name of the city/region.
    122127                return array(
    … …  
    145150
    146151        $cache_key = 'events:' . md5( serialize( $args ) );
    147 /*      if ( false !== ( $data = wp_cache_get( $cache_key, $cache_group ) ) ) {
     152        if ( false !== ( $data = wp_cache_get( $cache_key, $cache_group ) ) ) {
    148153                return $data;
    149         }*/
     154        }
    150155
    151156        $wheres = array();
    … …  
    180185
    181186        // Allow queries for limiting to specific countries.
    182         if ( !empty( $args['country'] ) ) {
     187        if ( !empty( $args['country'] ) && preg_match( '![a-z]{2}!i', $args['country'] ) ) {
    183188                $wheres[] = '`country` = %s';
    184                 // TODO: Maybe: Sanitize to 2-character country code?
    185189                $sql_values[] = $args['country'];
    186190        }
    … …  
    188192        // Just show upcoming events
    189193        $wheres[] = '`date_utc` >= %s';
    190         $sql_values[] = gmdate( 'Y-m-d' );
     194        // Dates are in local-time not UTC, so the API output will contain events that have already happened in some parts of the world.
     195        // TODO update this when the UTC dates are stored.
     196        $sql_values[] = gmdate( 'Y-m-d', time() - ( 24 * 60 * 60 ) );
    191197
    192198        // Limit
    … …  
    232238        }
    233239
    234 //      wp_cache_set( $cache_key, $events, $cache_group, $cache_life );
     240        wp_cache_set( $cache_key, $events, $cache_group, $cache_life );
    235241        return $events;
    236242}
Note: See TracChangeset for help on using the changeset viewer.