Example #1
0
 protected function _locate($asyncSave = false)
 {
     $country = Wpjb_List_Country::getByCode($this->company_country);
     $country = trim($country['name']);
     $addr = array($this->company_location, $this->company_zip_code, $this->company_state, $country);
     $query = http_build_query(array("address" => join(", ", $addr), "sensor" => "false"));
     $url = "http://maps.googleapis.com/maps/api/geocode/json?" . $query;
     $response = wp_remote_get($url);
     if ($response instanceof WP_Error) {
         $geo = null;
     } else {
         $geo = json_decode($response["body"]);
     }
     if (!$geo || $geo->status != "OK") {
         $this->geo_status = self::GEO_MISSING;
         $this->geo_latitude = 0;
         $this->geo_longitude = 0;
     } elseif ($geo->status == "OK") {
         $this->geo_status = self::GEO_FOUND;
         $this->geo_latitude = $geo->results[0]->geometry->location->lat;
         $this->geo_longitude = $geo->results[0]->geometry->location->lng;
     }
     if ($this->id > 0 && $asyncSave) {
         $object = new self($this->id);
         $object->geo_status = $this->geo_status;
         $object->geo_latitude = $this->geo_latitude;
         $object->geo_longitude = $this->geo_longitude;
         $object->_saveGeo();
     }
 }