1
|
|
|
<?php |
|
|
|
|
2
|
|
|
namespace Clubdeuce\WPLib\Components; |
3
|
|
|
|
4
|
|
|
|
5
|
|
|
use Clubdeuce\WPLib\Components\GoogleMaps\Geocoder; |
6
|
|
|
use Clubdeuce\WPLib\Components\GoogleMaps\Map; |
7
|
|
|
use Clubdeuce\WPLib\Components\GoogleMaps\Marker; |
8
|
|
|
|
9
|
|
|
class Google_Maps extends \WPLib_Module_Base { |
10
|
|
|
|
11
|
|
|
const INSTANCE_CLASS = 'Clubdeuce\WPLib\Components\GoogleMaps\Map'; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @var string |
15
|
|
|
*/ |
16
|
|
|
protected static $_api_key = ''; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var Geocoder |
20
|
|
|
*/ |
21
|
|
|
protected static $_geocoder; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* These conditions will be used to determine whether to enqueue the Google Maps JS. |
25
|
|
|
* |
26
|
|
|
* @var array |
27
|
|
|
*/ |
28
|
|
|
protected static $_script_conditions = array(); |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* The path to this module's directory |
32
|
|
|
* |
33
|
|
|
* @var string |
34
|
|
|
*/ |
35
|
|
|
protected static $_source_dir = '/vendor/clubdeuce/wplib-olm-google-maps'; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* The url to this module's directory |
39
|
|
|
* |
40
|
|
|
* @var string |
41
|
|
|
*/ |
42
|
|
|
protected static $_source_url; |
43
|
|
|
|
44
|
|
|
static function on_load() { |
45
|
|
|
|
46
|
|
|
self::add_class_action( 'wp_enqueue_scripts', 9 ); |
47
|
|
|
|
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @return string |
52
|
|
|
*/ |
53
|
|
|
static function api_key() { |
54
|
|
|
|
55
|
|
|
return static::$_api_key; |
56
|
|
|
|
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @return Geocoder |
61
|
|
|
*/ |
62
|
|
|
static function geocoder() { |
63
|
|
|
|
64
|
|
|
if ( ! isset( static::$_geocoder ) ) { |
65
|
|
|
static::$_geocoder = new Geocoder( ['api_key' => self::api_key() ] ); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
return static::$_geocoder; |
69
|
|
|
|
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param array $args |
74
|
|
|
* @return Map |
75
|
|
|
*/ |
76
|
|
|
static function make_new_map( $args = array() ) { |
77
|
|
|
|
78
|
|
|
$class = static::INSTANCE_CLASS; |
79
|
|
|
return new $class( $args ); |
80
|
|
|
|
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param string $key |
85
|
|
|
*/ |
86
|
|
|
static function register_api_key( $key ) { |
87
|
|
|
|
88
|
|
|
static::$_api_key = filter_var( $key, FILTER_SANITIZE_STRING ); |
89
|
|
|
|
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param callable $condition |
94
|
|
|
*/ |
95
|
|
|
static function register_script_condition( $condition ) { |
96
|
|
|
|
97
|
|
|
static::$_script_conditions[] = $condition; |
98
|
|
|
|
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
static function _wp_enqueue_scripts_9() { |
102
|
|
|
|
103
|
|
|
$key = static::api_key(); |
104
|
|
|
$source = sprintf( '%1$s/dist/scripts/maps.min.js', self::source_url() ); |
105
|
|
|
|
106
|
|
|
if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) { |
107
|
|
|
$source = sprintf( '%1$s/assets/maps.js', self::source_url() ); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
wp_register_script('google-maps', "https://maps.google.com/maps/api/js?v=3&key={$key}", false, '3.0', true ); |
111
|
|
|
wp_register_script('map-control', $source, array( 'jquery', 'google-maps' ), '0.1.2', true ); |
112
|
|
|
|
113
|
|
|
$conditions = array_map( array( __CLASS__, '_evaluate_condition' ), static::$_script_conditions ); |
114
|
|
|
|
115
|
|
|
if ( in_array( true, $conditions ) ) { |
116
|
|
|
wp_enqueue_script( 'map-control' ); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
static function script_conditions() { |
122
|
|
|
|
123
|
|
|
return static::$_script_conditions; |
124
|
|
|
|
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @param string $address |
129
|
|
|
* @param array $args |
130
|
|
|
* @return Marker |
131
|
|
|
*/ |
132
|
|
|
static function make_marker_by_address( $address, $args = array() ) { |
133
|
|
|
|
134
|
|
|
$args = wp_parse_args( $args, array( |
135
|
|
|
'address' => $address, |
136
|
|
|
) ); |
137
|
|
|
|
138
|
|
|
return new Marker( $args ); |
139
|
|
|
|
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @param string $destination |
144
|
|
|
* @param array $args |
145
|
|
|
* @return string |
146
|
|
|
*/ |
147
|
|
|
static function driving_directions_href($destination, $args = array() ) { |
148
|
|
|
|
149
|
|
|
$args = wp_parse_args( $args, array( |
150
|
|
|
'start' => 'My Location', |
151
|
|
|
) ); |
152
|
|
|
|
153
|
|
|
return sprintf( 'https://maps.google.com/maps?saddr=%1$s&daddr=%2$s', urlencode( $args['start'] ), urlencode( $destination ) ); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @param string $path |
158
|
|
|
*/ |
159
|
|
|
static function register_source_dir( $path ) { |
160
|
|
|
|
161
|
|
|
if ( is_dir( $path ) ) { |
162
|
|
|
self::$_source_dir = $path; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* @return string |
169
|
|
|
*/ |
170
|
|
|
static function source_dir() { |
171
|
|
|
|
172
|
|
|
return self::$_source_dir; |
173
|
|
|
|
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* @param $url |
178
|
|
|
*/ |
179
|
|
|
static function register_source_url( $url ) { |
180
|
|
|
|
181
|
|
|
self::$_source_url = $url; |
182
|
|
|
|
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* @return string |
187
|
|
|
*/ |
188
|
|
|
static function source_url() { |
189
|
|
|
|
190
|
|
|
return self::$_source_url; |
191
|
|
|
|
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* @param string|\Closure $callable |
196
|
|
|
* @return bool |
197
|
|
|
*/ |
198
|
|
|
private static function _evaluate_condition( $callable ) { |
199
|
|
|
|
200
|
|
|
$result = false; |
201
|
|
|
|
202
|
|
|
if ( is_callable( $callable ) ) { |
203
|
|
|
$result = call_user_func( $callable ); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
return $result; |
207
|
|
|
|
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
Google_Maps::on_load(); |
213
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.