Completed
Push — master ( 1a7397...8cd977 )
by Daryl
04:36
created

Google_Maps::_wp_enqueue_scripts_9()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 19
ccs 0
cts 11
cp 0
rs 9.2
c 0
b 0
f 0
cc 4
eloc 10
nc 4
nop 0
crap 20
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.1.6';
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
	static function initialize() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
51
52
		self::$_source_dir = dirname( __DIR__ );
53
54
		spl_autoload_register( array ( __CLASS__, 'autoload' ) );
55
		add_action( 'wp_enqueue_scripts', array( __CLASS__, '_wp_enqueue_scripts_9' ), 9 );
56
57
	}
58
59
	/**
60
	 * @param string $class_name
61
	 */
62
	static function autoload( $class_name ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
63
64
		$parts = explode( '\\', $class_name );
65
		$class = strtolower( str_replace( '_', '-', end ( $parts ) ) );
66
67
		foreach( array( 'includes', 'includes/helpers', 'includes/models', 'includes/views' ) as $dir ) {
68
			$filename = Google_Maps::source_dir() . "/{$dir}/class-{$class}.php";
69
			if ( file_exists( $filename ) ) {
70
				require_once $filename;
71
			}
72
		}
73
74
	}
75
76
	/**
77
	 * @return string
78
	 */
79
	static function api_key() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
80
81
		return static::$_api_key;
82
83
	}
84
85
	/**
86
	 * @return Geocoder
87
	 */
88
	static function geocoder() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
89
90
		if ( ! isset( static::$_geocoder ) ) {
91
			static::$_geocoder = new Geocoder( ['api_key' => self::api_key() ] );
92
		}
93
94
		return static::$_geocoder;
95
96
	}
97
98
	/**
99
	 * @param  array $args
100
	 * @return Map
101
	 */
102
	static function make_new_map( $args = array() ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
103
104
		$class = __NAMESPACE__ . '\Map';
105
		return new $class( $args );
106
107
	}
108
109
	/**
110
	 * @param string $key
111
	 */
112
	static function register_api_key( $key ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
113
114
		static::$_api_key = filter_var( $key, FILTER_SANITIZE_STRING );
115
116
	}
117
118
	/**
119
	 * @param callable $condition
120
	 */
121
	static function register_script_condition( $condition ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
122
123
		static::$_script_conditions[] = $condition;
124
125
	}
126
127
	static function _wp_enqueue_scripts_9() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
128
129
		$key    = static::api_key();
130
		$source = sprintf( '%1$s/dist/scripts/maps.min.js', self::source_url() );
131
132
		if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
133
			$source = sprintf( '%1$s/assets/maps.js', self::source_url() );
134
		}
135
136
		wp_register_script('google-maps', "https://maps.google.com/maps/api/js?v=3&key={$key}", false, '3.0', true );
137
		wp_register_script('map-control', $source, array( 'jquery', 'google-maps' ), self::version(), true );
138
139
		$conditions = array_map( array( __CLASS__, '_evaluate_condition' ), static::$_script_conditions );
140
141
		if ( in_array( true, $conditions ) ) {
142
			wp_enqueue_script( 'map-control' );
143
		}
144
145
	}
146
147
	static function script_conditions() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
148
149
		return static::$_script_conditions;
150
151
	}
152
153
	/**
154
	 * @param  string $address
155
	 * @param  array  $args
156
	 * @return Marker
157
	 */
158
	static function make_marker_by_address( $address, $args = array() ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
159
160
		$args = wp_parse_args( $args, array(
161
			'address' => $address,
162
			'geocoder' => self::geocoder(),
163
		) );
164
165
		return new Marker( $args );
166
167
	}
168
169
	/**
170
	 * @param  string $destination
171
	 * @param  array  $args
172
	 * @return string
173
	 */
174
	static function driving_directions_href($destination, $args = array() ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
175
176
		$args = wp_parse_args( $args, array(
177
			'start' => 'My Location',
178
		) );
179
180
		return sprintf( 'https://maps.google.com/maps?saddr=%1$s&daddr=%2$s', urlencode( $args['start'] ), urlencode( $destination ) );
181
	}
182
183
	/**
184
	 * @param string $path
185
	 */
186
	static function register_source_dir( $path ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
187
188
		if ( is_dir( $path ) ) {
189
			self::$_source_dir = $path;
190
		}
191
192
	}
193
194
	/**
195
	 * @return string
196
	 */
197
	static function source_dir() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
198
199
		return self::$_source_dir;
200
201
	}
202
203
	/**
204
	 * @param $url
205
	 */
206
	static function register_source_url( $url ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
207
208
		self::$_source_url = $url;
209
210
	}
211
212
	/**
213
	 * @return string
214
	 */
215
	static function source_url() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
216
217
		$path = dirname( __DIR__ );
218
219
		$url = str_replace( WP_CONTENT_DIR, WP_CONTENT_URL, $path );
220
221
		if ( is_ssl() ) {
222
			$url = preg_replace( '#^https*:\/\/([a-zA-z0-9\.]*)#', 'https://$1', $url );
223
		}
224
225
		return $url;
226
227
	}
228
229
	static function version() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
230
231
		return self::$_version;
232
233
	}
234
235
	/**
236
	 * @param  string|\Closure $callable
237
	 * @return bool
238
	 */
239
	private static function _evaluate_condition( $callable ) {
240
241
		$result = false;
242
243
		if ( is_callable( $callable ) ) {
244
			$result = call_user_func( $callable );
245
		}
246
247
		return $result;
248
249
	}
250
251
}
252