Making WordPress.org

Changeset 4884


Ignore:
Timestamp:
02/04/2017 03:26:36 PM (10 years ago)
Author:
dd32
Message:

Events: In the event a location cannot be determined from the inputs, provide an error message and don't return events globally.

File:
1 edited

Legend:

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

    r4883 r4884  
    4040$location = get_location( $location_args );
    4141
    42 $event_args = array();
    43 if ( isset( $_REQUEST['number'] ) ) {
    44         $event_args['number'] = $_REQUEST['number'];
    45 }
    46 if ( !empty( $location['latitude'] ) ) {
    47         $event_args['nearby'] = array(
    48                 'latitude'  => $location['latitude'],
    49                 'longitude' => $location['longitude'],
    50         );
    51 }
    52 if ( !empty( $location['country'] ) ) {
    53         $event_args['country'] = $location['country'];
    54 }
    55 
    56 $events = get_events( $event_args );
     42if ( false === $location ) {
     43        // No location was determined for the request. Bail with an error.
     44        $events = array();
     45        $error = 'no_location_available';
     46} else {
     47        $event_args = array();
     48        if ( isset( $_REQUEST['number'] ) ) {
     49                $event_args['number'] = $_REQUEST['number'];
     50        }
     51        if ( !empty( $location['latitude'] ) ) {
     52                $event_args['nearby'] = array(
     53                        'latitude'  => $location['latitude'],
     54                        'longitude' => $location['longitude'],
     55                );
     56        }
     57        if ( !empty( $location['country'] ) ) {
     58                $event_args['country'] = $location['country'];
     59        }
     60
     61        $events = get_events( $event_args );
     62}
    5763
    5864header( 'Expires: ' . gmdate( 'r', time() + $ttl ) );
    5965header( 'Access-Control-Allow-Origin: *' );
    6066header( 'Content-Type: application/json; charset=UTF-8' );
    61 echo wp_json_encode( compact( 'location', 'events', 'ttl', 'debug' ) );
     67echo wp_json_encode( compact( 'error', 'location', 'events', 'ttl' ) );
    6268
    6369
     
    102108        if ( isset( $args['location_name'] ) ) {
    103109                $guess = guess_location_from_geonames( $args['location_name'], $args['timezone'] ?? '', $country_code );
    104                 if ( $guess )
     110                if ( $guess ) {
    105111                        return array(
    106112                                'description' => $guess->name,
     
    109115                                'country' => $guess->country,
    110116                        );
     117                }
    111118        }
    112119
     
    120127                                'longitude' => $guess->ip_longitude,
    121128                                'country' => $guess->country_short,
    122                                 );
     129                        );
    123130                }
    124131        }
     
    136143        }
    137144
    138         return array(
    139                 'description' => 'Global',
    140                 'latitude'  => 0,
    141                 'longitude' => 0,
    142         );
     145        // If any of these are specified, and no localitity was guessed based on the above checks, bail with no location.
     146        if ( isset( $args['location_name'] ) || isset( $args['ip'] ) || ! empty( $args['latitude'] ) || ! empty( $args['longitude'] ) ) {
     147                return false;
     148        }
     149
     150        // No specific location details.
     151        return array();
    143152}
    144153
Note: See TracChangeset for help on using the changeset viewer.