Making WordPress.org

Changeset 5098


Ignore:
Timestamp:
03/07/2017 12:57:51 AM (8 years ago)
Author:
iandunn
Message:

Events API: Try to match country names if the city match failed

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

    r5033 r5098  
    179179    if ( isset( $args['location_name'] ) ) {
    180180        $guess = guess_location_from_geonames( $args['location_name'], $args['timezone'] ?? '', $country_code );
     181        $location_word_count = str_word_count( $args['location_name'] );
     182
     183        /*
     184         * Check if they entered only the country name, e.g. "Germany" or "New Zealand"
     185         *
     186         * This isn't perfect because some of the country names in the database are in a format that regular
     187         * people wouldn't type -- e.g., "Venezuela, Bolvarian Republic Of" -- but this will still match a
     188         * majority of them.
     189         *
     190         * Currently, this only works with English names because that's the only data we have.
     191         */
     192        if ( ! $guess ) {
     193            $location_country_code = get_country_code_from_name( $args['location_name'] );
     194
     195            if ( $location_country_code ) {
     196                return array(
     197                    'country' => $location_country_code,
     198                );
     199            }
     200        }
     201
    181202        if ( $guess ) {
    182203            return array(
     
    221242    // No specific location details.
    222243    return array();
     244}
     245
     246/**
     247 * Get the country code that corresponds to the given country name
     248 *
     249 * @param string $country_name
     250 *
     251 * @return false|string
     252 */
     253function get_country_code_from_name( $country_name ) {
     254    global $wpdb;
     255
     256    $country_code = $wpdb->get_var( $wpdb->prepare( "
     257        SELECT country_short
     258        FROM ip2location
     259        WHERE country_long = %s
     260        LIMIT 1",
     261        $country_name
     262    ) );
     263
     264
     265    // Convert all errors to boolean false for consistency
     266    if ( empty( $country_code ) ) {
     267        $country_code = false;
     268    }
     269
     270    return $country_code;
    223271}
    224272
  • sites/trunk/api.wordpress.org/public_html/events/1.0/tests/test-index.php

    r5066 r5098  
    9999     $cases = array(
    100100        /*
    101          * Only the country is given
    102          */
    103         'country-australia' => array(
     101         * Only the country code is given
     102         */
     103        'country-code-australia' => array(
    104104            'input' => array(
    105105                'country' => 'AU',
     
    107107            'expected' => array(
    108108                'country' => 'AU'
     109            ),
     110        ),
     111
     112
     113        /*
     114         * The country name, locale, and timezone are given
     115         */
     116        'country-exonym-1-word' => array(
     117            'input' => array(
     118                'location_name' => 'Indonesia',
     119                'locale'        => 'id_ID',
     120                'timezone'      => 'Asia/Jakarta',
     121            ),
     122            'expected' => array(
     123                'country' => 'ID'
     124            ),
     125        ),
     126
     127        'country-exonym-2-words' => array(
     128            'input' => array(
     129                'location_name' => 'Bosnia and Herzegovina',
     130                'locale'        => 'bs_BA',
     131                'timezone'      => 'Europe/Sarajevo',
     132            ),
     133            'expected' => array(
     134                'country' => 'BA'
    109135            ),
    110136        ),
Note: See TracChangeset for help on using the changeset viewer.