Making WordPress.org

Changeset 5137


Ignore:
Timestamp:
03/09/2017 09:18:35 PM (8 years ago)
Author:
iandunn
Message:

Events API: Refactor get_location() for single point of return

This is necessary so that the final value can be cached, which will be done in r5138.

Location:
sites/trunk/api.wordpress.org/public_html/events/1.0
Files:
2 edited

Legend:

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

    r5128 r5137  
    183183}
    184184
     185/**
     186 * Determine a location for the given IPv4 address
     187 *
     188 * @todo - Add support for IPv6 addresses. Otherwise, this will quickly lose effectiveness. As of March 2017, IPv6
     189 *         adoption is at 16% globally and rising relatively fast. Some countries are as high as 30%.
     190 *         See https://www.google.com/intl/en/ipv6/statistics.html#tab=ipv6-adoption for current stats.
     191 *
     192 * @param string $dotted_ip
     193 *
     194 * @return null|object `null` on failure; an object on success
     195 */
    185196function guess_location_from_ip( $dotted_ip ) {
    186197    global $wpdb;
     
    203214    // For a country request, no lat/long are returned.
    204215    if ( isset( $args['country'] ) ) {
    205         return array(
     216        $location = array(
    206217            'country' => $args['country'],
    207218        );
     
    209220
    210221    $country_code = null;
    211     if ( isset( $args['locale'] ) && preg_match( '/^[a-z]+[-_]([a-z]+)$/i', $args['locale'], $match ) ) {
     222    if ( ! $location && ( isset( $args['locale'] ) && preg_match( '/^[a-z]+[-_]([a-z]+)$/i', $args['locale'], $match ) ) ) {
    212223        $country_code = $match[1];
    213224    }
    214225
    215226    // Location was provided by the user:
    216     if ( isset( $args['location_name'] ) ) {
     227    if ( ! $location && isset( $args['location_name'] ) ) {
    217228        $guess = guess_location_from_city( $args['location_name'], $args['timezone'] ?? '', $country_code  );
    218229
    219230        if ( $guess ) {
    220             return array(
     231            $location = array(
    221232                'description' => $guess->name,
    222233                'latitude' => $guess->latitude,
     
    224235                'country' => $guess->country,
    225236            );
    226         }
    227 
    228         $guess = guess_location_from_country( $args['location_name'] );
     237        } else {
     238            $guess = guess_location_from_country( $args['location_name'] );
     239
     240            if ( ! $location && $guess ) {
     241                $location = array(
     242                    'country' => $guess,
     243                );
     244            }
     245        }
     246    }
     247
     248    // IP:
     249    if ( ! $location && isset( $args['ip'] ) ) {
     250        $guess = guess_location_from_ip( $args['ip'] );
    229251
    230252        if ( $guess ) {
    231             return array(
    232                 'country' => $guess,
    233             );
    234         }
    235     }
    236 
    237     // IP:
    238     if ( isset( $args['ip'] ) ) {
    239         $guess = guess_location_from_ip( $args['ip'] );
    240         if ( $guess ) {
    241             return array(
     253            $location = array(
    242254                'description' => $guess->ip_city,
    243255                'latitude' => $guess->ip_latitude,
     
    249261
    250262    if (
    251         ! empty( $args['latitude'] )  && is_numeric( $args['latitude'] ) &&
    252         ! empty( $args['longitude'] ) && is_numeric( $args['longitude'] )
     263        ! $location && (
     264            ! empty( $args['latitude'] )  && is_numeric( $args['latitude'] ) &&
     265            ! empty( $args['longitude'] ) && is_numeric( $args['longitude'] )
     266        )
    253267    ) {
    254268        $city = get_city_from_coordinates( $args['latitude'], $args['longitude'] );
    255269
    256         return array(
     270        $location = array(
    257271            'description' => $city ? $city : "{$args['latitude']}, {$args['longitude']}",
    258272            'latitude'  => $args['latitude'],
     
    261275    }
    262276
    263     // If any of these are specified, and no localitity was guessed based on the above checks, bail with no location.
    264     if ( isset( $args['location_name'] ) || isset( $args['ip'] ) || ! empty( $args['latitude'] ) || ! empty( $args['longitude'] ) ) {
    265         return false;
    266     }
    267 
    268     // No specific location details.
    269     return array();
     277    if ( ! $location ) {
     278        if ( isset( $args['location_name'] ) || isset( $args['ip'] ) || ! empty( $args['latitude'] ) || ! empty( $args['longitude'] ) ) {
     279            // If any of these are specified, and no localitity was guessed based on the above checks, bail with no location.
     280            $location = false;
     281        } else {
     282            // No specific location details.
     283            $location = array();
     284        }
     285    }
     286
     287    return $location;
    270288}
    271289
  • sites/trunk/api.wordpress.org/public_html/events/1.0/tests/test-index.php

    r5128 r5137  
    156156        ),
    157157
     158        /*
     159         * No input was provided
     160         */
     161        'input-empty' => array(
     162            'input'    => array(),
     163            'expected' => array(),
     164        ),
     165
    158166
    159167        /*
Note: See TracChangeset for help on using the changeset viewer.