|
- <?php
- namespace Biukop;
- require_once(ABSPATH . "wp-config.php");
- require_once(ABSPATH . "wp-includes/wp-db.php");
-
- class AddrMap{
- private $user1; //wp-user
- private $user2; //wp-user
- private $origin;//addr
- private $destination; //addr
- private $json;//google response
- private $dist;//distance in meter
- private $error;//bool error
- public function __construct(String $user1_login, String $user2_login) {
- $this->error = false;
- $this->user1 = get_user_by('login', $user1_login);
- $this->user2 = get_user_by('login', $user2_login);
-
- //convert user to their address
- $this->origin = $this->get_user_address($user1_login);
- $this->destination = $this->get_user_address($user2_login);
-
- //get distance from db
- $this->dist = $this->get_distance_from_db();
- if ($this->dist == false){
- //get distance from google map
- $response = $this->get_google_direction($this->origin, $this->destination);
- if ($response == false){
- //mark error
- $this->error == true;
- }else{
- $this->json = $response;
- $this->dist = $this->convert_response_to_dist_in_meter($response);
- }
- //store it in db for future use
- $this->update_db();
- }
-
- }
-
- public function has_error(){
- return $this->error;
- }
-
- public function get_error_msg(){
- if($this->error)
- return "Cannot get distance getween $this->user1->display_name and $this->user2->display_name";
- else
- return "";
- }
-
- public function get_dist(){
- if ($this->error)
- return 0;
- return $this->dist;
- }
-
- public function get_dist_in_km(){
- if ($this->error)
- return 0;
- return $this->dist/1000;
- }
-
- public function get_route_json()
- {
- if ($this->error)
- return null;
- return $this->json;
- }
-
- private function update_db()
- {
- global $wpdb;
- $table_name= $this->get_table_name();
- $sql = "INSERT INTO $table_name (origin,destination,response,distance) VALUES ('%s', '%s', '%s', %d) ;";
- $query = $wpdb->prepare($sql, array(
- $this->origin,
- $this->destination,
- json_encode($this->json),
- $this->dist,
- ));
- $results = $wpdb->get_results($query);
-
- if ( $results == false) {
- //failed
- $lastid = $wpdb->insert_id;
- $msg = sprintf("Insert into address distance db error: date=%s, user1_id=%s, user2_id=%s, orign=%s, dest=%s, resp=%s, dist=%d, db_error=%s",
- date('Y-m-d, H:i:s'),
- $this->user1->ID,
- $this->user2->ID,
- $this->origin,
- $this->destination,
- json_encode($this->json),
- $this->dist,
- $wpdb->last_error
- );
- $error_log = plugin_dir_path(__FILE__) . "/logs/addr.log";
- file_put_contents($error_log, $msg, FILE_APPEND);
- }
- }
-
-
- private function get_user_address($login){
- $user = get_user_by('login', $login);
- $addr = get_user_meta($user->ID, 'address', true);
- return $addr;
- }
-
-
- private function get_table_name()
- {
- global $wpdb;
- $table_name = $wpdb->prefix . 'acare_addr_distance';
- return $table_name;
- }
-
- private function get_distance_from_db(){
- global $wpdb;
- $table_name = $this->get_table_name();
- $sql = "SELECT * FROM $table_name WHERE origin='%s' and destination='%s' ";
- $query = $wpdb->prepare($sql, array(
- $this->origin,
- $this->destination,
- ));
- $row = $wpdb->get_row($query);
- if ( $row == null )
- return false;
- $this->json = json_decode($row->response);
- return (int) $row->distance; //in meters;
- }
-
- private function super_json_decode($json, &$value, $assoc = false, $depth = 512, $options = 0) {
- $pValue = false;
- $result = json_decode($json, $assoc, $depth);
- if(json_last_error() == JSON_ERROR_NONE) {
- $value = $result;
- return true;
- }
- return false;
- }
-
- private function convert_response_to_dist_in_meter($resp){
- $distance = -1;
- if (count($resp->routes) >0)
- {
- $legs = $resp->routes[0]->legs;
- foreach ($legs as $l){
- $distance += $l->distance->value;
- }
- }
- return $distance;
- }
-
- private function get_google_direction($origin, $destination) {
- $data = "";
- $arg = array(
- 'origin' => $origin,
- 'destination' => $destination,
- 'key'=>'AIzaSyDMRjRh3wu0zTQneYboTk5VgQio3XABz_o',
- );
-
- $ch = curl_init("https://maps.googleapis.com/maps/api/directions/json?". http_build_query($arg));
- curl_setopt_array($ch, array(
- CURLOPT_CONNECTTIMEOUT => 10,
- CURLOPT_RETURNTRANSFER => true,
- CURLOPT_TIMEOUT => 60,
- ));
-
- $response = false;
- if($this->super_json_decode(curl_exec($ch), $data)) {
- $response = $data;
- }
-
- $info = curl_getinfo($ch);
- if($info['http_code'] !== 200) {
- return false;
- }
-
- return $response;
- //var_dump($response);
- }
- }
|