Changeset 5355
- Timestamp:
- 04/17/2017 09:06:59 PM (8 years ago)
- 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 417 417 if ( ! $location && $guess ) { 418 418 $location = array( 419 'country' => $guess, 419 'country' => $guess['country_short'], 420 'description' => $guess['country_long'], 420 421 ); 421 422 } … … 462 463 * @param string $location_name 463 464 * 464 * @return false| string false on failure; a country codeon success465 * @return false|array false on failure; an array with country details on success 465 466 */ 466 467 function guess_location_from_country( $location_name ) { 467 468 // 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 ); 469 470 $location_word_count = str_word_count( $location_name ); 470 471 $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(); 480 473 481 474 /* 482 475 * Multi-word queries may contain cities, regions, and countries, so try to extract just the country 483 476 */ 484 if ( ! $country _code&& $location_word_count >= 2 ) {477 if ( ! $country && $location_word_count >= 2 ) { 485 478 // Catch input like "Vancouver Canada" 486 479 $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 ) { 498 484 // Catch input like "Santiago De Los Caballeros, Dominican Republic" 499 485 $country_name = sprintf( … … 502 488 $location_name_parts[ $location_word_count - 1 ] 503 489 ); 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 ) { 508 494 // Catch input like "Kaga-Bandoro, Central African Republic" 509 495 $country_name = sprintf( … … 513 499 $location_name_parts[ $location_word_count - 1 ] 514 500 ); 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; 519 505 } 520 506 … … 531 517 532 518 /** 533 * Get the country codethat corresponds to the given country name519 * Get the country that corresponds to the given country name 534 520 * 535 521 * @param string $country_name 536 522 * 537 * @return false| string false on failure; a country codeon success538 */ 539 function get_country_ code_from_name( $country_name ) {523 * @return false|array false on failure; an array with country details on success 524 */ 525 function get_country_from_name( $country_name ) { 540 526 global $wpdb; 541 527 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 544 530 FROM ip2location 545 WHERE country_long = %s 531 WHERE 532 country_long = %s OR 533 country_short = %s 546 534 LIMIT 1", 535 $country_name, 547 536 $country_name 548 ) ); 549 537 ), 'ARRAY_A' ); 550 538 551 539 // 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; 557 545 } 558 546 -
sites/trunk/api.wordpress.org/public_html/events/1.0/tests/test-index.php
r5311 r5355 124 124 ), 125 125 'expected' => array( 126 'country' => 'AU' 126 'country' => 'AU', 127 127 ), 128 128 ), … … 139 139 ), 140 140 'expected' => array( 141 'country' => 'ID' 141 'country' => 'ID', 142 'description' => 'indonesia', 142 143 ), 143 144 ), … … 487 488 'expected' => array( 488 489 'country' => 'CA', 490 'description' => 'canada', 489 491 ), 490 492 ), … … 498 500 'expected' => array( 499 501 'country' => 'DO', 502 'description' => 'dominican republic', 500 503 ), 501 504 ), … … 509 512 'expected' => array( 510 513 'country' => 'CF', 514 'description' => 'central african republic', 511 515 ), 512 516 ), … … 520 524 'expected' => array( 521 525 'country' => 'GB', 526 'description' => 'united kingdom', 522 527 ), 523 528 ), … … 531 536 'expected' => array( 532 537 'country' => 'BI', 538 'description' => 'burundi', 533 539 ), 534 540 ),
Note: See TracChangeset
for help on using the changeset viewer.