1 | <?php |
||
2 | |||
3 | namespace Clubdeuce\WPGoogleMaps; |
||
4 | |||
5 | /** |
||
6 | * Class Google_Maps |
||
7 | * @package Clubdeuce\WPGoogleMaps |
||
8 | */ |
||
9 | class Google_Maps { |
||
10 | |||
11 | /** |
||
12 | * @var string |
||
13 | */ |
||
14 | protected static $_version = '0.3.0'; |
||
15 | |||
16 | /** |
||
17 | * @var string |
||
18 | */ |
||
19 | protected static $_api_key = ''; |
||
20 | |||
21 | /** |
||
22 | * @var Geocoder |
||
23 | */ |
||
24 | protected static $_geocoder; |
||
25 | |||
26 | /** |
||
27 | * These conditions will be used to determine whether to enqueue the Google Maps JS. |
||
28 | * |
||
29 | * @var array |
||
30 | */ |
||
31 | protected static $_script_conditions = array(); |
||
32 | |||
33 | /** |
||
34 | * The path to this library's directory |
||
35 | * |
||
36 | * @var string |
||
37 | */ |
||
38 | protected static $_source_dir; |
||
39 | |||
40 | /** |
||
41 | * The url to this module's directory |
||
42 | * |
||
43 | * @var string |
||
44 | */ |
||
45 | protected static $_source_url; |
||
46 | |||
47 | /** |
||
48 | * |
||
49 | */ |
||
50 | 1 | public static function initialize() { |
|
51 | |||
52 | 1 | self::$_source_dir = dirname( __DIR__ ); |
|
53 | |||
54 | 1 | add_action( 'wp_enqueue_scripts', array( __CLASS__, '_wp_enqueue_scripts_9' ), 9 ); |
|
55 | |||
56 | 1 | } |
|
57 | |||
58 | /** |
||
59 | * @return string |
||
60 | */ |
||
61 | 1 | public static function api_key() { |
|
62 | |||
63 | 1 | return static::$_api_key; |
|
64 | |||
65 | } |
||
66 | |||
67 | /** |
||
68 | * @param array $args |
||
69 | * @return Map |
||
70 | */ |
||
71 | 1 | public static function make_new_map( $args = array() ) { |
|
72 | |||
73 | 1 | $class = __NAMESPACE__ . '\Map'; |
|
74 | 1 | return new $class( $args ); |
|
75 | |||
76 | } |
||
77 | |||
78 | /** |
||
79 | * @param string $key |
||
80 | */ |
||
81 | 1 | public static function register_api_key( $key ) { |
|
82 | |||
83 | 1 | static::$_api_key = filter_var( $key, FILTER_SANITIZE_STRING ); |
|
84 | |||
85 | 1 | } |
|
86 | |||
87 | /** |
||
88 | * @param callable|bool $condition |
||
89 | */ |
||
90 | 1 | public static function register_script_condition( $condition ) { |
|
91 | |||
92 | 1 | static::$_script_conditions[] = $condition; |
|
93 | |||
94 | 1 | } |
|
95 | |||
96 | 1 | public static function _wp_enqueue_scripts_9() { |
|
97 | |||
98 | 1 | self::_register_scripts(); |
|
99 | |||
100 | 1 | if ( self::_evaluate_conditions() ) { |
|
101 | 1 | wp_enqueue_script( 'map-control' ); |
|
102 | //wp_enqueue_script( 'google-marker-clusterer' ); |
||
103 | 1 | wp_enqueue_style( 'map' ); |
|
104 | } |
||
105 | 1 | } |
|
106 | |||
107 | /** |
||
108 | * @param string $address |
||
109 | * @param array $args |
||
110 | * @return Marker |
||
111 | */ |
||
112 | 1 | public static function make_marker_by_address( $address, $args = array() ) { |
|
113 | |||
114 | 1 | $args = wp_parse_args( $args, array( |
|
115 | 1 | 'address' => $address, |
|
116 | 1 | 'geocoder' => self::geocoder(), |
|
117 | ) ); |
||
118 | |||
119 | 1 | return new Marker( $args ); |
|
120 | |||
121 | } |
||
122 | |||
123 | /** |
||
124 | * @param float $lat |
||
125 | * @param float $lng |
||
126 | * |
||
127 | * @return Marker |
||
128 | */ |
||
129 | 1 | public static function make_marker_by_position( $lat, $lng, $args = array() ) { |
|
130 | |||
131 | 1 | $args = wp_parse_args( $args, array( |
|
132 | 1 | 'geocoder' => self::geocoder(), |
|
133 | 1 | 'latitude' => $lat, |
|
134 | 1 | 'longitude' => $lng, |
|
135 | )); |
||
136 | |||
137 | 1 | return new Marker( $args ); |
|
138 | |||
139 | } |
||
140 | |||
141 | /** |
||
142 | * @param string $destination |
||
143 | * @param array $args |
||
144 | * @return string |
||
145 | */ |
||
146 | 1 | public static function driving_directions_href($destination, $args = array() ) { |
|
147 | |||
148 | 1 | $args = wp_parse_args( $args, array( |
|
149 | 1 | 'start' => 'My Location', |
|
150 | ) ); |
||
151 | |||
152 | 1 | return sprintf( 'https://maps.google.com/maps?saddr=%1$s&daddr=%2$s', urlencode( $args['start'] ), urlencode( $destination ) ); |
|
153 | } |
||
154 | |||
155 | /** |
||
156 | * @return string |
||
157 | */ |
||
158 | 1 | public static function source_dir() { |
|
159 | |||
160 | 1 | return self::$_source_dir; |
|
161 | |||
162 | } |
||
163 | |||
164 | /** |
||
165 | * @return string |
||
166 | */ |
||
167 | public static function source_url() { |
||
168 | |||
169 | $path = dirname( __DIR__ ); |
||
170 | |||
171 | $url = str_replace( WP_CONTENT_DIR, WP_CONTENT_URL, $path ); |
||
172 | |||
173 | if ( is_ssl() ) { |
||
174 | $url = preg_replace( '#^https*:\/\/([a-zA-z0-9\.]*)#', 'https://$1', $url ); |
||
175 | } |
||
176 | |||
177 | return $url; |
||
178 | |||
179 | } |
||
180 | |||
181 | /** |
||
182 | * @return string |
||
183 | */ |
||
184 | 1 | public static function version() { |
|
185 | |||
186 | 1 | return self::$_version; |
|
187 | |||
188 | } |
||
189 | |||
190 | /** |
||
191 | * @return Geocoder |
||
192 | */ |
||
193 | 1 | public static function geocoder() { |
|
194 | |||
195 | 1 | if ( ! isset( static::$_geocoder ) ) { |
|
196 | 1 | static::$_geocoder = new Geocoder( [ 'api_key' => self::api_key() ] ); |
|
197 | } |
||
198 | |||
199 | 1 | return static::$_geocoder; |
|
200 | |||
201 | } |
||
202 | |||
203 | /** |
||
204 | * @return bool |
||
205 | */ |
||
206 | 1 | protected static function _evaluate_conditions() { |
|
207 | |||
208 | 1 | $result = false; |
|
209 | 1 | $conditions = self::_script_conditions(); |
|
210 | |||
211 | 1 | foreach( $conditions as $key => $condition ) { |
|
212 | 1 | if ( is_callable( $condition ) ) { |
|
213 | 1 | $conditions[ $key ] = call_user_func( $condition ); |
|
214 | } |
||
215 | } |
||
216 | |||
217 | 1 | if ( in_array( true, $conditions ) ) { |
|
218 | 1 | $result = true; |
|
219 | } |
||
220 | |||
221 | 1 | return $result; |
|
222 | |||
223 | } |
||
224 | |||
225 | /** |
||
226 | * |
||
227 | */ |
||
228 | 1 | protected static function _register_scripts() { |
|
229 | |||
230 | 1 | $key = static::api_key(); |
|
231 | 1 | $source = sprintf( '%1$s/dist/main.js', self::source_url() ); |
|
232 | |||
233 | 1 | wp_register_script( 'google-maps', "https://maps.google.com/maps/api/js?v=3&key={$key}", false, '3.0', true ); |
|
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
234 | 1 | wp_register_script( 'marker-cluster', self::source_url() . '/assets/markerclusterer.js', array( 'jquery', 'google-maps' ), self::version(), true ); |
|
235 | 1 | wp_register_script( 'map-control', $source, array( 'jquery', 'google-maps', 'marker-cluster' ), self::version(), true ); |
|
236 | 1 | wp_register_style( 'map', self::source_url() . '/assets/map.css' ); |
|
237 | |||
238 | 1 | } |
|
239 | |||
240 | /** |
||
241 | * @return array |
||
242 | */ |
||
243 | 1 | protected static function _script_conditions() { |
|
244 | |||
245 | 1 | return static::$_script_conditions; |
|
246 | |||
247 | } |
||
248 | |||
249 | } |
||
250 |