Making WordPress.org

Changeset 5478


Ignore:
Timestamp:
05/04/2017 05:11:49 PM (9 years ago)
Author:
iandunn
Message:

Events: Ignore en_US locales to avoid false-positives

Props dd32

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

    r5462 r5478  
    398398    }
    399399
    400     $country_code = null;
    401     if ( ! $location && ( isset( $args['locale'] ) && preg_match( '/^[a-z]+[-_]([a-z]+)$/i', $args['locale'], $match ) ) ) {
    402         $country_code = $match[1];
    403     }
     400    $country_code = get_country_code_from_locale( $args['locale'] ?? '' );
    404401
    405402    // Coordinates provided
     
    492489    wp_cache_set( $cache_key, $location, $cache_group, $cache_life );
    493490    return $location;
     491}
     492
     493/**
     494 * Extract the country code from the given locale
     495 *
     496 * @param string $locale
     497 *
     498 * @return string|null
     499 */
     500function get_country_code_from_locale( $locale ) {
     501    /*
     502     * `en_US` is ignored, because it's the default locale in Core, and many users never set it. That
     503     * leads to a lot of false-positives; e.g., Hampton-Sydney, Virginia, USA instead of Sydney, Australia.
     504     */
     505    if ( empty( $locale ) || 'en_US' === $locale ) {
     506        return null;
     507    }
     508
     509    preg_match( '/^[a-z]+[-_]([a-z]+)$/i', $locale, $match );
     510
     511    $country_code = $match[1] ?? null;
     512
     513    return $country_code;
    494514}
    495515
  • sites/trunk/api.wordpress.org/public_html/events/1.0/tests/test-index.php

    r5462 r5478  
    256256        ),
    257257
     258        // Many users never change the default `en_US` locale in Core
     259        'city-oceania-with-en_US' => array(
     260            'input' => array(
     261                'location_name' => 'Sydney',
     262                'locale'        => 'en_US',
     263                'timezone'      => 'Australia/Sydney',
     264            ),
     265            'expected' => array(
     266                'description' => 'sydney',
     267                'latitude'    => '-33.868',
     268                'longitude'   => '151.207',
     269                'country'     => 'AU',
     270            ),
     271        ),
     272
    258273        'city-south-america' => array(
    259274            'input' => array(
Note: See TracChangeset for help on using the changeset viewer.