|
1 | 1 | <?php |
2 | 2 | /* |
3 | 3 | Plugin Name: WP Engine GeoIP |
4 | | -Version: 1.1.4 |
| 4 | +Version: 1.2.0 |
5 | 5 | Description: Create a personalized user experienced based on location. |
6 | 6 | Author: WP Engine |
7 | 7 | Author URI: http://wpengine.com |
@@ -578,6 +578,53 @@ private function match_label_synonyms( $label ) { |
578 | 578 |
|
579 | 579 | return $label; |
580 | 580 | } |
| 581 | + |
| 582 | + /** |
| 583 | + * Utility function: Calculate distance to point |
| 584 | + * |
| 585 | + * Provided a lat/lng, calculate the distance from visitor's location |
| 586 | + * Uses the Haversine Formula, accurate for short distance but not over poles or the equator |
| 587 | + * |
| 588 | + * Note: Test against a return value of false to make sure you got a calculated distance. Example: |
| 589 | + * $geo = WPEngine\GeoIp::instance(); |
| 590 | + * if ( false !== $geo->distance_to( $latitude, $longitude ) ) { |
| 591 | + * // Do something |
| 592 | + * } |
| 593 | + * |
| 594 | + * @link http://andrew.hedges.name/experiments/haversine/ |
| 595 | + * @param float Latitude of the destination in degrees |
| 596 | + * @param float Longitude of the destination in degrees |
| 597 | + * @param bool Whether to calculate the distance in kilometers or miles |
| 598 | + * @return float distance in miles |
| 599 | + * @since 1.2 |
| 600 | + */ |
| 601 | + public function distance_to( $lat, $lng, $metric = false ) { |
| 602 | + $start_lat = deg2rad( $this->latitude() ); |
| 603 | + $start_lng = deg2rad( $this->longitude() ); |
| 604 | + |
| 605 | + // Test for null values passed into the function or a 0,0 coordinate for the user |
| 606 | + // If either exist, abort. (0,0 is the result when coordinates fail) |
| 607 | + if ( is_null( $lat ) || is_null( $lng ) || ( empty( $start_lat ) && empty( $start_lng ) ) ) { |
| 608 | + return false; |
| 609 | + } |
| 610 | + |
| 611 | + // Choose the right radius for the results: radius of the Earth in kilometers and miles |
| 612 | + $radius = $metric ? 6373 : 3961; |
| 613 | + |
| 614 | + // Sanitize the user submitted variables |
| 615 | + $lat = floatval( $lat ); |
| 616 | + $lng = floatval( $lng ); |
| 617 | + |
| 618 | + $dlng = $lng - $start_lng; |
| 619 | + $dlat = $lat - $start_lat; |
| 620 | + |
| 621 | + // Calculate the distance |
| 622 | + $a = ( sin( $dlat / 2 ) * sin( $dlat / 2 ) ) + ( cos( $lat ) * cos( $start_lat ) * sin( $dlng / 2 ) * sin( $dlng / 2 ) ); |
| 623 | + $c = 2 * atan2( sqrt( $a ), sqrt( 1 - $a ) ); |
| 624 | + $d = $radius * $c; |
| 625 | + |
| 626 | + return $d; |
| 627 | + } |
581 | 628 | } |
582 | 629 |
|
583 | 630 | // Register the GeoIP instance |
|
0 commit comments