1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Clubdeuce\WPLib\Components\GoogleMaps; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Class Geocoder |
7
|
|
|
* @package Clubdeuce\WPLib\Components\GoogleMaps |
8
|
|
|
*/ |
9
|
|
|
class Geocoder { |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @var string |
13
|
|
|
*/ |
14
|
|
|
protected $_api_key; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* KSA_Geocoder constructor. |
18
|
|
|
* |
19
|
|
|
* @param array $args |
20
|
|
|
*/ |
21
|
|
|
function __construct( $args = array() ) { |
22
|
|
|
|
23
|
|
|
$args = wp_parse_args( $args, array( |
|
|
|
|
24
|
|
|
'api_key' => '', |
25
|
|
|
) ); |
26
|
|
|
|
27
|
|
|
$this->_api_key = $args['api_key']; |
28
|
|
|
|
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @return string |
33
|
|
|
*/ |
34
|
|
|
function api_key() { |
35
|
|
|
|
36
|
|
|
return $this->_api_key; |
37
|
|
|
|
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param string $address |
42
|
|
|
* @return Location|\WP_Error |
43
|
|
|
*/ |
44
|
|
|
function geocode( $address ) { |
45
|
|
|
|
46
|
|
|
$url = $this->_make_url( $address ); |
47
|
|
|
|
48
|
|
|
$response = $this->_make_request( $url ); |
49
|
|
|
|
50
|
|
|
return $this->_make_location( $response ); |
51
|
|
|
|
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param string $address |
56
|
|
|
* @return string |
57
|
|
|
*/ |
58
|
|
|
private function _make_url( $address ) { |
59
|
|
|
|
60
|
|
|
return sprintf( |
61
|
|
|
'https://maps.googleapis.com/maps/api/geocode/json?address=%1$s&key=%2$s', |
62
|
|
|
urlencode( filter_var( $address, FILTER_SANITIZE_STRING ) ), |
63
|
|
|
self::api_key() |
64
|
|
|
); |
65
|
|
|
|
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Convert the response body into an a Location object |
70
|
|
|
* |
71
|
|
|
* @param array $response |
72
|
|
|
* @return Location |
73
|
|
|
*/ |
74
|
|
|
private function _make_location( $response ) { |
75
|
|
|
|
76
|
|
|
return new Location( array( |
77
|
|
|
'address' => $response['results'][0]['formatted_address'], |
78
|
|
|
'formatted_address' => $response['results'][0]['formatted_address'], |
79
|
|
|
'latitude' => $response['results'][0]['geometry']['location']['lat'], |
80
|
|
|
'longitude' => $response['results'][0]['geometry']['location']['lng'], |
81
|
|
|
'place_id' => $response['results'][0]['place_id'], |
82
|
|
|
'types' => $response['results'][0]['types'], |
83
|
|
|
'viewport' => $response['results'][0]['geometry']['viewport'], |
84
|
|
|
) ); |
85
|
|
|
|
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param $url |
91
|
|
|
* @return array|\WP_Error |
92
|
|
|
*/ |
93
|
|
|
private function _make_request( $url ) { |
94
|
|
|
|
95
|
|
|
$return = new \WP_Error( 1, 'Invalid URL', $url ); |
96
|
|
|
|
97
|
|
|
if ( wp_http_validate_url( $url ) ) { |
98
|
|
|
$request = $this->_get_data( $url ); |
99
|
|
|
|
100
|
|
|
$return = new \WP_Error( $request['response']['code'], $request['response']['message'] ); |
101
|
|
|
|
102
|
|
|
if ( 200 == $request['response']['code'] ) { |
103
|
|
|
$return = json_decode( $request['body'], true ); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
return $return; |
109
|
|
|
|
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @param $url |
114
|
|
|
* @return array|\WP_Error |
115
|
|
|
*/ |
116
|
|
|
private function _get_data( $url ) { |
117
|
|
|
|
118
|
|
|
$cache_key = md5( serialize( $url ) ); |
119
|
|
|
|
120
|
|
|
if ( ! $data = wp_cache_get( $cache_key ) ) { |
121
|
|
|
$data = wp_remote_get( $url ); |
122
|
|
|
wp_cache_add( $cache_key, $data, 300 ); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
return $data; |
126
|
|
|
|
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
} |
130
|
|
|
|