Last active
February 3, 2020 10:16
-
-
Save 2Fwebd/c12144c7e330246fa25ad1a27c267a71 to your computer and use it in GitHub Desktop.
This file contains 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 | |
/** | |
* The private Google API key used for the GeoCoding API | |
* | |
* @var string | |
*/ | |
private $privateApiKey = 'aaaaaaa'; | |
/** | |
* Woffice_Members_Map constructor. | |
* | |
*/ | |
public function __construct() | |
{ | |
// We only run this class if BuddyPress is enabled and so is the xProfile component | |
if (!function_exists('bp_is_active') || !bp_is_active('xprofile')) { | |
return; | |
} | |
// WordPress Actions | |
add_action('xprofile_updated_profile', array($this, 'createProfileField')); | |
add_action('xprofile_updated_profile', array($this, 'saveSingleMember')); | |
} | |
/** | |
* Save a single member's position | |
* | |
* This is called whenever a new member is being saved | |
* | |
* @param int $user_id | |
*/ | |
public function saveSingleMember($user_id) { | |
$users_coordinates = get_option('woffice_map_locations'); | |
$users_coordinates = json_decode($users_coordinates, true); | |
$users_coordinates = (empty($users_coordinates)) ? array() : $users_coordinates; | |
$already_exists = false; | |
foreach ($users_coordinates as $key=>$coordinate_array) { | |
if ((int) $coordinate_array['user_id'] !== $user_id) { | |
continue; | |
} | |
$already_exists = true; | |
$new_user_coordinates = $this->getMemberCoordinates($user_id); | |
if (!empty($new_user_coordinates)) { | |
$users_coordinates[$key] = $new_user_coordinates; | |
} | |
break; | |
} | |
if (!$already_exists) { | |
$new_user_coordinates = $this->getMemberCoordinates($user_id); | |
if (!empty($new_user_coordinates)) { | |
array_push($users_coordinates, $new_user_coordinates); | |
} | |
} | |
update_option('woffice_map_locations', json_encode($users_coordinates)); | |
} | |
/** | |
* Returns the member's lat / lng and name if an address is set in the profile field | |
* | |
* @param int $user_id | |
* @return array - can be empty | |
*/ | |
private function getMemberCoordinates($user_id) { | |
$user_data = array(); | |
// We get the field data | |
$address = xprofile_get_field_data($this->fieldName, $user_id); | |
if (empty($address)) { | |
return $user_data; | |
} | |
$location = urlencode($address); | |
// We call the Google API API | |
$json_decoded = $this->apiRequest($location); | |
error_log(json_encode($json_decoded)); | |
if (empty($json_decoded) || $json_decoded['status'] !== "OK") | |
return $user_data; | |
// We return a formatted response array | |
return array( | |
'name' => $json_decoded['results'][0]['formatted_address'], | |
'lat' => $json_decoded['results'][0]['geometry']['location']['lat'], | |
'long' => $json_decoded['results'][0]['geometry']['location']['lng'], | |
'user_id' => $user_id | |
); | |
} | |
/** | |
* Makes an API call to the Geocoding API | |
* | |
* We use WordPress request wrapper functions | |
* | |
* @param string $address - url encoded address | |
* @return array | |
*/ | |
private function apiRequest($address) { | |
$request = wp_remote_get("https://maps.google.com/maps/api/geocode/json?address=" . $address . "&sensor=false&key=" . $this->privateApiKey); | |
$response = wp_remote_retrieve_body($request); | |
$json_decoded = json_decode($response, true); | |
return $json_decoded; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment