WordPress.org

Making WordPress.org

Changeset 5355


Ignore:
Timestamp:
04/17/2017 09:06:59 PM (3 years ago)
Author:
iandunn
Message:

Events: Add country names to descriptions

There are situations where it's helpful to give clients both the country name and country code, because they may have only passed one of them.

See https://github.com/coreymckrill/nearby-wordpress-events/issues/35

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

    r5311 r5355  
    417417            if ( ! $location && $guess ) {
    418418                $location = array(
    419                     'country' => $guess,
     419                    'country'     => $guess['country_short'],
     420                    'description' => $guess['country_long'],
    420421                );
    421422            }
     
    462463 * @param string $location_name
    463464 *
    464  * @return false|string false on failure; a country code on success
     465 * @return false|array false on failure; an array with country details on success
    465466 */
    466467function guess_location_from_country( $location_name ) {
    467468    // Check if they entered only the country name, e.g. "Germany" or "New Zealand"
    468     $country_code = get_country_code_from_name( $location_name );
     469    $country             = get_country_from_name( $location_name );
    469470    $location_word_count = str_word_count( $location_name );
    470471    $location_name_parts = explode( ' ', $location_name );
    471 
    472     // Check if they entered only the country code, e.g., "GB"
    473     if ( ! $country_code ) {
    474         $valid_country_codes = get_valid_country_codes();
    475 
    476         if ( in_array( $location_name, $valid_country_codes, true ) ) {
    477             $country_code = $location_name;
    478         }
    479     }
     472    $valid_country_codes = get_valid_country_codes();
    480473
    481474    /*
    482475     * Multi-word queries may contain cities, regions, and countries, so try to extract just the country
    483476     */
    484     if ( ! $country_code && $location_word_count >= 2 ) {
     477    if ( ! $country && $location_word_count >= 2 ) {
    485478        // Catch input like "Vancouver Canada"
    486479        $country_id   = $location_name_parts[ $location_word_count - 1 ];
    487         $country_code = get_country_code_from_name( $country_id );
    488 
    489         // Catch input like "London GB"
    490         if ( ! $country_code ) {
    491             if ( in_array( $country_id, $valid_country_codes, true ) ) {
    492                 $country_code = $country_id;
    493             }
    494         }
    495     }
    496 
    497     if ( ! $country_code && $location_word_count >= 3 ) {
     480        $country      = get_country_from_name( $country_id );
     481    }
     482
     483    if ( ! $country && $location_word_count >= 3 ) {
    498484        // Catch input like "Santiago De Los Caballeros, Dominican Republic"
    499485        $country_name = sprintf(
     
    502488            $location_name_parts[ $location_word_count - 1 ]
    503489        );
    504         $country_code = get_country_code_from_name( $country_name );
    505     }
    506 
    507     if ( ! $country_code && $location_word_count >= 4 ) {
     490        $country = get_country_from_name( $country_name );
     491    }
     492
     493    if ( ! $country && $location_word_count >= 4 ) {
    508494        // Catch input like "Kaga-Bandoro, Central African Republic"
    509495        $country_name = sprintf(
     
    513499            $location_name_parts[ $location_word_count - 1 ]
    514500        );
    515         $country_code = get_country_code_from_name( $country_name );
    516     }
    517 
    518     return $country_code;
     501        $country = get_country_from_name( $country_name );
     502    }
     503
     504    return $country;
    519505}
    520506
     
    531517
    532518/**
    533  * Get the country code that corresponds to the given country name
     519 * Get the country that corresponds to the given country name
    534520 *
    535521 * @param string $country_name
    536522 *
    537  * @return false|string false on failure; a country code on success
    538  */
    539 function get_country_code_from_name( $country_name ) {
     523 * @return false|array false on failure; an array with country details on success
     524 */
     525function get_country_from_name( $country_name ) {
    540526    global $wpdb;
    541527
    542     $country_code = $wpdb->get_var( $wpdb->prepare( "
    543         SELECT country_short
     528    $country = $wpdb->get_row( $wpdb->prepare( "
     529        SELECT country_short, country_long
    544530        FROM ip2location
    545         WHERE country_long = %s
     531        WHERE
     532            country_long  = %s OR
     533            country_short = %s
    546534        LIMIT 1",
     535        $country_name,
    547536        $country_name
    548     ) );
    549 
     537    ), 'ARRAY_A' );
    550538
    551539    // Convert all errors to boolean false for consistency
    552     if ( empty( $country_code ) ) {
    553         $country_code = false;
    554     }
    555 
    556     return $country_code;
     540    if ( empty( $country ) ) {
     541        $country = false;
     542    }
     543
     544    return $country;
    557545}
    558546
  • sites/trunk/api.wordpress.org/public_html/events/1.0/tests/test-index.php

    r5311 r5355  
    124124            ),
    125125            'expected' => array(
    126                 'country' => 'AU'
     126                'country' => 'AU',
    127127            ),
    128128        ),
     
    139139            ),
    140140            'expected' => array(
    141                 'country' => 'ID'
     141                'country' => 'ID',
     142                'description' => 'indonesia',
    142143            ),
    143144        ),
     
    487488            'expected' => array(
    488489                'country' => 'CA',
     490                'description' => 'canada',
    489491            ),
    490492        ),
     
    498500            'expected' => array(
    499501                'country' => 'DO',
     502                'description' => 'dominican republic',
    500503            ),
    501504        ),
     
    509512            'expected' => array(
    510513                'country' => 'CF',
     514                'description' => 'central african republic',
    511515            ),
    512516        ),
     
    520524            'expected' => array(
    521525                'country' => 'GB',
     526                'description' => 'united kingdom',
    522527            ),
    523528        ),
     
    531536            'expected' => array(
    532537                'country' => 'BI',
     538                'description' => 'burundi',
    533539            ),
    534540        ),
Note: See TracChangeset for help on using the changeset viewer.