timesheet source code
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

182 linhas
5.5KB

  1. <?php
  2. namespace Biukop;
  3. require_once(ABSPATH . "wp-config.php");
  4. require_once(ABSPATH . "wp-includes/wp-db.php");
  5. class AddrMap{
  6. private $user1; //wp-user
  7. private $user2; //wp-user
  8. private $origin;//addr
  9. private $destination; //addr
  10. private $json;//google response
  11. private $dist;//distance in meter
  12. private $error;//bool error
  13. public function __construct(String $user1_login, String $user2_login) {
  14. $this->error = false;
  15. $this->user1 = get_user_by('login', $user1_login);
  16. $this->user2 = get_user_by('login', $user2_login);
  17. //convert user to their address
  18. $this->origin = $this->get_user_address($user1_login);
  19. $this->destination = $this->get_user_address($user2_login);
  20. //get distance from db
  21. $this->dist = $this->get_distance_from_db();
  22. if ($this->dist == false){
  23. //get distance from google map
  24. $response = $this->get_google_direction($this->origin, $this->destination);
  25. if ($response == false){
  26. //mark error
  27. $this->error == true;
  28. }else{
  29. $this->json = $response;
  30. $this->dist = $this->convert_response_to_dist_in_meter($response);
  31. }
  32. //store it in db for future use
  33. $this->update_db();
  34. }
  35. }
  36. public function has_error(){
  37. return $this->error;
  38. }
  39. public function get_error_msg(){
  40. if($this->error)
  41. return "Cannot get distance getween $this->user1->display_name and $this->user2->display_name";
  42. else
  43. return "";
  44. }
  45. public function get_dist(){
  46. if ($this->error)
  47. return 0;
  48. return $this->dist;
  49. }
  50. public function get_dist_in_km(){
  51. if ($this->error)
  52. return 0;
  53. return $this->dist/1000;
  54. }
  55. public function get_route_json()
  56. {
  57. if ($this->error)
  58. return null;
  59. return $this->json;
  60. }
  61. private function update_db()
  62. {
  63. global $wpdb;
  64. $table_name= $this->get_table_name();
  65. $sql = "INSERT INTO $table_name (origin,destination,response,distance) VALUES ('%s', '%s', '%s', %d) ;";
  66. $query = $wpdb->prepare($sql, array(
  67. $this->origin,
  68. $this->destination,
  69. json_encode($this->json),
  70. $this->dist,
  71. ));
  72. $results = $wpdb->get_results($query);
  73. if ( $results == false) {
  74. //failed
  75. $lastid = $wpdb->insert_id;
  76. $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",
  77. date('Y-m-d, H:i:s'),
  78. $this->user1->ID,
  79. $this->user2->ID,
  80. $this->origin,
  81. $this->destination,
  82. json_encode($this->json),
  83. $this->dist,
  84. $wpdb->last_error
  85. );
  86. $error_log = plugin_dir_path(__FILE__) . "/logs/addr.log";
  87. file_put_contents($error_log, $msg, FILE_APPEND);
  88. }
  89. }
  90. private function get_user_address($login){
  91. $user = get_user_by('login', $login);
  92. $addr = get_user_meta($user->ID, 'address', true);
  93. return $addr;
  94. }
  95. private function get_table_name()
  96. {
  97. global $wpdb;
  98. $table_name = $wpdb->prefix . 'acare_addr_distance';
  99. return $table_name;
  100. }
  101. private function get_distance_from_db(){
  102. global $wpdb;
  103. $table_name = $this->get_table_name();
  104. $sql = "SELECT * FROM $table_name WHERE origin='%s' and destination='%s' ";
  105. $query = $wpdb->prepare($sql, array(
  106. $this->origin,
  107. $this->destination,
  108. ));
  109. $row = $wpdb->get_row($query);
  110. if ( $row == null )
  111. return false;
  112. $this->json = json_decode($row->response);
  113. return (int) $row->distance; //in meters;
  114. }
  115. private function super_json_decode($json, &$value, $assoc = false, $depth = 512, $options = 0) {
  116. $pValue = false;
  117. $result = json_decode($json, $assoc, $depth);
  118. if(json_last_error() == JSON_ERROR_NONE) {
  119. $value = $result;
  120. return true;
  121. }
  122. return false;
  123. }
  124. private function convert_response_to_dist_in_meter($resp){
  125. $distance = -1;
  126. if (count($resp->routes) >0)
  127. {
  128. $legs = $resp->routes[0]->legs;
  129. foreach ($legs as $l){
  130. $distance += $l->distance->value;
  131. }
  132. }
  133. return $distance;
  134. }
  135. private function get_google_direction($origin, $destination) {
  136. $data = "";
  137. $arg = array(
  138. 'origin' => $origin,
  139. 'destination' => $destination,
  140. 'key'=>'AIzaSyDMRjRh3wu0zTQneYboTk5VgQio3XABz_o',
  141. );
  142. $ch = curl_init("https://maps.googleapis.com/maps/api/directions/json?". http_build_query($arg));
  143. curl_setopt_array($ch, array(
  144. CURLOPT_CONNECTTIMEOUT => 10,
  145. CURLOPT_RETURNTRANSFER => true,
  146. CURLOPT_TIMEOUT => 60,
  147. ));
  148. $response = false;
  149. if($this->super_json_decode(curl_exec($ch), $data)) {
  150. $response = $data;
  151. }
  152. $info = curl_getinfo($ch);
  153. if($info['http_code'] !== 200) {
  154. return false;
  155. }
  156. return $response;
  157. //var_dump($response);
  158. }
  159. }