1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Clubdeuce\WPGoogleMaps; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Class Geocoder |
7
|
|
|
* @package Clubdeuce\WPGoogleMaps |
8
|
|
|
*/ |
9
|
|
|
class Geocoder { |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @var string |
13
|
|
|
*/ |
14
|
|
|
protected $_api_key; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var HTTP |
18
|
|
|
*/ |
19
|
|
|
protected $_http; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Geocoder constructor. |
23
|
|
|
* |
24
|
|
|
* @param array $args |
25
|
|
|
*/ |
26
|
1 |
|
public function __construct( $args = array() ) { |
27
|
|
|
|
28
|
1 |
|
$args = wp_parse_args( $args, array( |
29
|
1 |
|
'api_key' => Google_Maps::api_key(), |
30
|
1 |
|
'http' => new HTTP(), |
31
|
|
|
) ); |
32
|
|
|
|
33
|
1 |
|
$this->_api_key = $args['api_key']; |
34
|
1 |
|
$this->_http = $args['http']; |
35
|
|
|
|
36
|
1 |
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @return string |
40
|
|
|
*/ |
41
|
1 |
|
public function api_key() { |
42
|
|
|
|
43
|
1 |
|
return $this->_api_key; |
44
|
|
|
|
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param string $address |
49
|
|
|
* @return Location|\WP_Error |
50
|
|
|
*/ |
51
|
3 |
|
public function geocode( $address ) { |
52
|
|
|
|
53
|
3 |
|
$url = $this->_make_url( $address ); |
54
|
|
|
|
55
|
3 |
|
$response = $this->_http->make_request( $url ); |
56
|
|
|
|
57
|
|
|
do { |
58
|
3 |
|
if ( is_a( $response, \WP_Error::class ) ) { |
59
|
1 |
|
$location = $response; |
60
|
1 |
|
break; |
61
|
|
|
} |
62
|
|
|
|
63
|
2 |
|
$results = json_decode( wp_remote_retrieve_body( $response ), true ); |
64
|
|
|
|
65
|
2 |
|
if ( 0 === count( $results['results'] ) ) { |
66
|
1 |
|
$error_message = isset( $results['error_message'] ) ? $results['error_message'] : 'Unknown error'; |
67
|
1 |
|
$location = new \WP_Error( '100', sprintf( '%1$s: %2$s', $results['status'], $error_message ) ); |
68
|
1 |
|
break; |
69
|
|
|
} |
70
|
|
|
|
71
|
1 |
|
$location = $this->_make_location( $results['results'][0] ); |
72
|
1 |
|
} while ( false ); |
73
|
|
|
|
74
|
3 |
|
return $location; |
75
|
|
|
|
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param string $address |
80
|
|
|
* @return string |
81
|
|
|
*/ |
82
|
2 |
|
private function _make_url( $address ) { |
83
|
|
|
|
84
|
2 |
|
return sprintf( |
85
|
2 |
|
'https://maps.googleapis.com/maps/api/geocode/json?address=%1$s&key=%2$s', |
86
|
2 |
|
urlencode( filter_var( $address, FILTER_SANITIZE_STRING ) ), |
87
|
2 |
|
$this->api_key() |
88
|
|
|
); |
89
|
|
|
|
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Convert the response body into an a Location object |
94
|
|
|
* |
95
|
|
|
* @param array $results |
96
|
|
|
* @return Location |
97
|
|
|
*/ |
98
|
2 |
|
private function _make_location( $results ) { |
99
|
|
|
|
100
|
2 |
|
$response = new Location( array( |
101
|
2 |
|
'address' => $results['formatted_address'], |
102
|
2 |
|
'formatted_address' => $results['formatted_address'], |
103
|
2 |
|
'state' => $this->_get_state_from_results( $results ), |
104
|
2 |
|
'zip_code' => $this->_get_zip_from_results( $results ), |
105
|
2 |
|
'latitude' => $results['geometry']['location']['lat'], |
106
|
2 |
|
'longitude' => $results['geometry']['location']['lng'], |
107
|
2 |
|
'place_id' => $results['place_id'], |
108
|
2 |
|
'types' => $results['types'], |
109
|
2 |
|
'viewport' => $results['geometry']['viewport'], |
110
|
|
|
) ); |
111
|
|
|
|
112
|
2 |
|
return $response; |
113
|
|
|
|
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @param array $results |
118
|
|
|
* @return string |
119
|
|
|
*/ |
120
|
1 |
|
private function _get_state_from_results( $results ) { |
121
|
|
|
|
122
|
1 |
|
return $this->_get_value_from_results( 'administrative_area_level_1', $results ); |
123
|
|
|
|
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @param array $results |
128
|
|
|
* @return string |
129
|
|
|
*/ |
130
|
1 |
|
private function _get_zip_from_results( $results ) { |
131
|
|
|
|
132
|
1 |
|
return $this->_get_value_from_results( 'postal_code', $results ); |
133
|
|
|
|
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @param string $value |
138
|
|
|
* @param array $results |
139
|
|
|
* @return string |
140
|
|
|
*/ |
141
|
2 |
|
private function _get_value_from_results( $value, $results ) { |
142
|
|
|
|
143
|
2 |
|
$result_value = ''; |
144
|
|
|
|
145
|
2 |
|
if ( isset( $results['address_components'] ) ) { |
146
|
2 |
|
foreach ( $results['address_components'] as $component ) { |
147
|
2 |
|
if ( $component['types'][0] === $value ) { |
148
|
2 |
|
$result_value = $component['short_name']; |
149
|
2 |
|
break; |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|
154
|
2 |
|
return $result_value; |
155
|
|
|
|
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
} |
159
|
|
|
|