Making WordPress.org


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

Events: Ignore en_US locales to avoid false-positives

Props dd32

File:
1 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
Note: See TracChangeset for help on using the changeset viewer.