Skip to content

Commit 7a2fdf3

Browse files
committed
Retagging 1.2 for WP 4.5 Compatability
1 parent ea4438a commit 7a2fdf3

File tree

2 files changed

+69
-8
lines changed

2 files changed

+69
-8
lines changed

readme.txt

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22
Contributors: wpengine, markkelnar, stevenkword, stephenlin, ryanshoover, taylor4484
33
Tags: wpe, wpengine, geoip, localization, geolocation
44
Requires at least: 3.0.1
5-
Tested up to: 4.5
6-
Stable tag: 1.1.4
5+
Tested up to: 4.3.1
6+
Stable tag: 1.2.0
7+
78
License: GPLv2 or later
89
License URI: http://www.gnu.org/licenses/gpl-2.0.html
910

@@ -54,7 +55,7 @@ You can use any of the following location variable shortcodes to return the vari
5455
3) Region: `[geoip-region]`
5556

5657
* In the US region will return States
57-
* In Canada region will return Providences
58+
* In Canada region will return Provinces
5859
* Outside the US/CA this will return a Region number. Please note region numbers are not unique between countries
5960

6061
4) City: `[geoip-city]`
@@ -172,6 +173,20 @@ You want to show an ad written in Spanish to all of South America except for Bra
172173
[geoip_content city="Brasilia"]Venta de la Navidad en los adaptadores USB[/geoip_content]
173174
```
174175

176+
== Calculate distance between points ==
177+
178+
You have a utility function that will calculate the distance from your provided lat/lng coordinate to the visitor's location in either miles or kilometers. This can be useful for determining approximate distances, as results may be cached at the state or country level, depending on your configuration.
179+
180+
Example use:
181+
```
182+
$latitude = 30.268246;
183+
$longitude = -97.745992;
184+
$geo = WPEngine\GeoIp::instance();
185+
if ( false !== $geo->distance_to( $latitude, $longitude ) ) {
186+
$miles_to_wp_engine = $geo->distance_to( $latitude, $longitude );
187+
}
188+
```
189+
175190
== Testing Parameters ==
176191
You can use the following URL parameters to test how your localized content will appear to visitors from various geographic locations. You can add any of the parameters below to any URL of a page using the GeoIP shortcodes or API calls:
177192

@@ -220,11 +235,10 @@ Please contact the WP Engine [Support Team](https://my.wpengine.com/support#gene
220235
2. An example post using GeoIP shortcodes
221236

222237
== Changelog ==
223-
= 1.1.4 =
224-
- Bumps version number for WP 4.5 compatibility
225238

226-
= 1.1.3 =
227-
- Bumps version number for WP 4.4.2 compatibility
239+
= 1.2.0 =
240+
- Adds a utility function for calculating distances
241+
- Bumps version number for WP 4.5 compatibility
228242

229243
= 1.1.2 =
230244
- Fixes logic for nested parameter selectors in content shortcode

wpengine-geoip.php

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
/*
33
Plugin Name: WP Engine GeoIP
4-
Version: 1.1.4
4+
Version: 1.2.0
55
Description: Create a personalized user experienced based on location.
66
Author: WP Engine
77
Author URI: http://wpengine.com
@@ -578,6 +578,53 @@ private function match_label_synonyms( $label ) {
578578

579579
return $label;
580580
}
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+
}
581628
}
582629

583630
// Register the GeoIP instance

0 commit comments

Comments
 (0)