Making WordPress.org


Ignore:
Timestamp:
04/07/2017 05:20:45 AM (8 years ago)
Author:
iandunn
Message:

Events: Restore original $location_name variable for re-use

The unmodified name will be required in an upcoming commit. Additionally, code is more self-documenting and clear when variables aren't re-purposed.

Since these lines are already being modified, go ahead and improve the formatting, to make them more readable.

File:
1 edited

Legend:

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

    r5168 r5275  
    22
    33namespace Dotorg\API\Events;
     4use stdClass;
    45
    56/**
     
    169170}
    170171
     172/**
     173 * Look for the given location in the Geonames database
     174 *
     175 * @param string $location_name
     176 * @param string $timezone
     177 * @param string $country
     178 *
     179 * @return stdClass|null
     180 */
    171181function guess_location_from_geonames( $location_name, $timezone, $country ) {
    172182    global $wpdb;
     
    175185    // And we sort by population desc, assuming that the biggest matching location is the most likely one.
    176186
    177     // Strip quotes from the search query and enclose it in double quotes, to force an exact literal search
    178     $location_name = '"' . strtr( $location_name, [ '"' => '', "'" => '' ] ) . '"';
    179     $row = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM geoname WHERE MATCH(name,asciiname,alternatenames) AGAINST(%s IN BOOLEAN MODE) ORDER BY FIELD(%s, country) DESC, FIELD(%s, timezone) DESC, population DESC LIMIT 1", $location_name, $country, $timezone ) );
     187    // Strip all quotes from the search query, and then enclose it in double quotes, to force an exact literal search
     188    $quoted_location_name = sprintf(
     189        '"%s"',
     190        strtr( $location_name, [ '"' => '', "'" => '' ] )
     191    );
     192
     193    $row = $wpdb->get_row( $wpdb->prepare( "
     194        SELECT *
     195        FROM geoname
     196        WHERE
     197            MATCH( name, asciiname, alternatenames )
     198            AGAINST( %s IN BOOLEAN MODE )
     199        ORDER BY
     200            FIELD( %s, country  ) DESC,
     201            FIELD( %s, timezone ) DESC,
     202            population DESC
     203        LIMIT 1",
     204        $quoted_location_name,
     205        $country,
     206        $timezone
     207    ) );
    180208
    181209    return $row;
Note: See TracChangeset for help on using the changeset viewer.