Completed
Push — master ( 96e232...5c8cf8 )
by Daryl
01:20
created

Google_Maps::geocoder()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 4
cts 4
cp 1
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 0
crap 2
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
	public static function initialize() {
51
52
		self::$_source_dir = dirname( __DIR__ );
53
54
		add_action( 'wp_enqueue_scripts', array( __CLASS__, '_wp_enqueue_scripts_9' ), 9 );
55
56
	}
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
	 * @return Geocoder
69
	 */
70 1
	public static function geocoder() {
71
72 1
		if ( ! isset( static::$_geocoder ) ) {
73 1
			static::$_geocoder = new Geocoder( ['api_key' => self::api_key() ] );
74
		}
75
76 1
		return static::$_geocoder;
77
78
	}
79
80
	/**
81
	 * @param  array $args
82
	 * @return Map
83
	 */
84 1
	public static function make_new_map( $args = array() ) {
85
86 1
		$class = __NAMESPACE__ . '\Map';
87 1
		return new $class( $args );
88
89
	}
90
91
	/**
92
	 * @param string $key
93
	 */
94 1
	public static function register_api_key( $key ) {
95
96 1
		static::$_api_key = filter_var( $key, FILTER_SANITIZE_STRING );
97
98 1
	}
99
100
	/**
101
	 * @param callable $condition
102
	 */
103 1
	public static function register_script_condition( $condition ) {
104
105 1
		static::$_script_conditions[] = $condition;
106
107 1
	}
108
109
	public static function _wp_enqueue_scripts_9() {
110
111
		$key    = static::api_key();
112
		$source = sprintf( '%1$s/dist/scripts/maps.min.js', self::source_url() );
113
114
		if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
115
			$source = sprintf( '%1$s/assets/maps.js', self::source_url() );
116
		}
117
118
		wp_register_script('google-maps', "https://maps.google.com/maps/api/js?v=3&key={$key}", false, '3.0', true );
119
		wp_register_script('map-control', $source, array( 'jquery', 'google-maps' ), self::version(), true );
120
121
		$conditions = self::script_conditions();
122
123
		foreach( $conditions as $key => $condition ) {
124
125
			if ( is_callable( $condition ) ) {
126
				$conditions[ $key] = call_user_func( $condition );
127
			}
128
		}
129
130
		if ( in_array( true, $conditions ) ) {
131
			wp_enqueue_script( 'map-control' );
132
		}
133
134
	}
135
136 1
	public static function script_conditions() {
137
138 1
		return static::$_script_conditions;
139
140
	}
141
142
	/**
143
	 * @param  string $address
144
	 * @param  array  $args
145
	 * @return Marker
146
	 */
147 1 View Code Duplication
	public static function make_marker_by_address( $address, $args = array() ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
148
149 1
		$args = wp_parse_args( $args, array(
150 1
			'address' => $address,
151 1
			'geocoder' => self::geocoder(),
152
		) );
153
154 1
		return new Marker( $args );
155
156
	}
157
158
	/**
159
	 * @param float $lat
160
	 * @param float $lng
161
	 *
162
	 * @return Marker
163
	 */
164 1 View Code Duplication
	public static function make_marker_by_position( $lat, $lng, $args = array() ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
165
166 1
		$args = wp_parse_args( $args, array(
0 ignored issues
show
Unused Code introduced by
$args is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
167 1
			'geocoder' => self::geocoder(),
168
		));
169
170 1
		return new Marker( array(
171 1
			'lat' => $lat,
172 1
			'lng' => $lng,
173
174
		) );
175
176
	}
177
178
	/**
179
	 * @param  string $destination
180
	 * @param  array  $args
181
	 * @return string
182
	 */
183 1
	public static function driving_directions_href($destination, $args = array() ) {
184
185 1
		$args = wp_parse_args( $args, array(
186 1
			'start' => 'My Location',
187
		) );
188
189 1
		return sprintf( 'https://maps.google.com/maps?saddr=%1$s&daddr=%2$s', urlencode( $args['start'] ), urlencode( $destination ) );
190
	}
191
192
	/**
193
	 * @return string
194
	 */
195 1
	public static function source_dir() {
196
197 1
		return self::$_source_dir;
198
199
	}
200
201
	/**
202
	 * @return string
203
	 */
204
	public static function source_url() {
205
206
		$path = dirname( __DIR__ );
207
208
		$url = str_replace( WP_CONTENT_DIR, WP_CONTENT_URL, $path );
209
210
		if ( is_ssl() ) {
211
			$url = preg_replace( '#^https*:\/\/([a-zA-z0-9\.]*)#', 'https://$1', $url );
212
		}
213
214
		return $url;
215
216
	}
217
218 1
	public static function version() {
219
220 1
		return self::$_version;
221
222
	}
223
224
}
225