diff --git 1.0/index.php 1.0/index.php
index 908cfa2..554f258 100644
|
|
|
function guess_location_from_city( $location_name, $timezone, $country_code ) { |
| 216 | 216 | |
| 217 | 217 | $cache_key = 'guess_location_from_city:' . md5( $location_name . ':' . $timezone . ':' . $country_code ); |
| 218 | 218 | $guess = wp_cache_get( $cache_key, $cache_group ); |
| | 219 | $guess = false; // @todo tmp disable caching for manual testing |
| 219 | 220 | |
| 220 | 221 | if ( $guess ) { |
| 221 | 222 | if ( '__NOT_FOUND__' == $guess ) { |
| … |
… |
function guess_location_from_geonames( $location_name, $timezone, $country, $wil |
| 267 | 268 | // The FIELD() orderings give preference to rows that match the country and/or timezone, without excluding rows that don't match. |
| 268 | 269 | // And we sort by population desc, assuming that the biggest matching location is the most likely one. |
| 269 | 270 | |
| | 271 | add_db_table( 'geoip', 'geoname_summary_utf8mb4_test' ); // @todo tmp |
| | 272 | |
| 270 | 273 | // Exact match |
| 271 | | $row = $wpdb->get_row( $wpdb->prepare( " |
| | 274 | $query = " |
| 272 | 275 | SELECT name, latitude, longitude, country |
| 273 | | FROM geoname_summary |
| | 276 | FROM geoname_summary_utf8mb4_test |
| 274 | 277 | WHERE name = %s |
| 275 | 278 | ORDER BY |
| 276 | 279 | FIELD( %s, country ) DESC, |
| 277 | 280 | FIELD( %s, timezone ) DESC, |
| 278 | 281 | population DESC |
| 279 | | LIMIT 1", |
| 280 | | $location_name, |
| 281 | | $country, |
| 282 | | $timezone |
| 283 | | ) ); |
| | 282 | LIMIT 1"; |
| | 283 | |
| | 284 | $prepared_query = $wpdb->prepare( $query, $location_name, $country, $timezone ); |
| | 285 | $db_handle = $wpdb->db_connect( $prepared_query ); |
| | 286 | |
| | 287 | $wpdb->set_charset( $db_handle, 'utf8' ); // The content in this table requires a UTF8 connection. |
| | 288 | $row = $wpdb->get_row( $prepared_query ); |
| | 289 | $wpdb->set_charset( $db_handle, 'latin1' ); // Revert to the default charset to avoid affecting other queries. |
| 284 | 290 | |
| 285 | 291 | // Wildcard match |
| 286 | 292 | if ( ! $row && $wildcard && 'ASCII' !== mb_detect_encoding( $location_name ) ) { |
| 287 | | $row = $wpdb->get_row( $wpdb->prepare( " |
| | 293 | $query = " |
| 288 | 294 | SELECT name, latitude, longitude, country |
| 289 | | FROM geoname_summary |
| | 295 | FROM geoname_summary_utf8mb4_test |
| 290 | 296 | WHERE name LIKE %s |
| 291 | 297 | ORDER BY |
| 292 | 298 | FIELD( %s, country ) DESC, |
| 293 | 299 | FIELD( %s, timezone ) DESC, |
| 294 | 300 | population DESC |
| 295 | | LIMIT 1", |
| 296 | | $wpdb->esc_like( $location_name ) . '%', |
| 297 | | $country, |
| 298 | | $timezone |
| 299 | | ) ); |
| | 301 | LIMIT 1"; |
| | 302 | |
| | 303 | $prepared_query = $wpdb->prepare( $query, $wpdb->esc_like( $location_name ) . '%', $country, $timezone ); |
| | 304 | $db_handle = $wpdb->db_connect( $prepared_query ); |
| | 305 | |
| | 306 | $wpdb->set_charset( $db_handle, 'utf8' ); // The content in this table requires a UTF8 connection. |
| | 307 | $row = $wpdb->get_row( $prepared_query ); |
| | 308 | $wpdb->set_charset( $db_handle, 'latin1' ); // Revert to the default charset to avoid affecting other queries. |
| 300 | 309 | } |
| 301 | 310 | |
| 302 | 311 | // Suffix the "State", good in some countries (western countries) horrible in others |
| … |
… |
function guess_location_from_geonames( $location_name, $timezone, $country, $wil |
| 306 | 315 | // $row->name .= ', ' . $row->state; |
| 307 | 316 | // } |
| 308 | 317 | |
| | 318 | // Strip off null bytes - @todo should be possible to use avoid these in the raw data? |
| | 319 | if ( ! empty( $row->name ) ) { |
| | 320 | $row->name = trim( $row->name ); |
| | 321 | } |
| | 322 | |
| 309 | 323 | return $row; |
| 310 | 324 | } |
| 311 | 325 | |