Last active
June 10, 2017 13:31
-
-
Save goranefbl/fef879f15197352e67fcb596d9b9e4cf to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Povuci sve lokacije najblize lat i lon koji su prosledjeni | |
function get_posts_by_geo_distance($post_type,$lat_key,$lng_key,$source_lat,$source_lng) { | |
global $wpdb; | |
$sql =<<<SQL | |
SELECT | |
rl.ID, | |
rl.post_title AS location, | |
ROUND(6371*2*ASIN(SQRT(POWER(SIN(({$source_lat}-abs(lat.lat))*pi()/180/2),2)+ | |
COS({$source_lat}*pi()/180)*COS(abs(lat.lat)*pi()/180)* | |
POWER(SIN(({$source_lng}-lng.lng)*pi()/180/2),2))),3) AS distance | |
FROM | |
am2_2017_posts rl | |
RIGHT JOIN (SELECT *,CAST(meta_value AS DECIMAL(11,7)) AS lat FROM am2_2017_postmeta lat WHERE lat.meta_key='{$lat_key}') lat ON lat.post_id = rl.ID | |
RIGHT JOIN (SELECT *,CAST(meta_value AS DECIMAL(11,7)) AS lng FROM am2_2017_postmeta lng WHERE lng.meta_key='{$lng_key}') lng ON lng.post_id = rl.ID | |
WHERE | |
rl.post_type='{$post_type}' AND rl.post_name<>'auto-draft' | |
ORDER BY | |
distance | |
LIMIT | |
20 | |
SQL; | |
$sql = $wpdb->prepare($sql,$source_lat,$source_lat,$source_lng); | |
return $wpdb->get_results($sql); | |
} | |
// | |
function am2_get_current_user_geo_data( $args = array() ) { | |
//$ip = '184.151.231.247'; | |
$ip = $_SERVER['REMOTE_ADDR']; | |
// Skini geoip sa githubua naravno | |
$data_json = file_get_contents(get_bloginfo('template_directory').'/includes/geoip/src/geoip.php?ip='.$ip); | |
$data = json_decode($data_json); | |
$user_latitude = $data->latitude; | |
$user_longitude = $data->longitude; | |
if(empty($data->latitude) || empty($data->longitude)) { | |
$user_latitude = 0; | |
$user_longitude = 0; | |
} | |
// Dobijes 20 lokacija oko korisnika | |
$locations = get_posts_by_geo_distance('store','nlatitude','nlongitude',$user_latitude,$user_longitude); | |
//ostatak... | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment