These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
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 | static function on_load() { |
||
32 | |||
33 | self::add_class_action( 'wp_enqueue_scripts', 9 ); |
||
34 | |||
35 | } |
||
36 | |||
37 | /** |
||
38 | * @return string |
||
39 | */ |
||
40 | static function api_key() { |
||
41 | |||
42 | return static::$_api_key; |
||
43 | |||
44 | } |
||
45 | |||
46 | /** |
||
47 | * @return Geocoder |
||
48 | */ |
||
49 | static function geocoder() { |
||
50 | |||
51 | if ( ! isset( static::$_geocoder ) ) { |
||
52 | static::$_geocoder = new Geocoder( ['api_key' => self::api_key() ] ); |
||
53 | } |
||
54 | |||
55 | return static::$_geocoder; |
||
56 | |||
57 | } |
||
58 | |||
59 | /** |
||
60 | * @param array $args |
||
61 | * @return Map |
||
62 | */ |
||
63 | static function make_new_map( $args = array() ) { |
||
64 | |||
65 | $class = static::INSTANCE_CLASS; |
||
66 | return new $class( $args ); |
||
67 | |||
68 | } |
||
69 | |||
70 | /** |
||
71 | * @param string $key |
||
72 | */ |
||
73 | static function register_api_key( $key ) { |
||
74 | |||
75 | static::$_api_key = filter_var( $key, FILTER_SANITIZE_STRING ); |
||
76 | |||
77 | } |
||
78 | |||
79 | /** |
||
80 | * @param callable $condition |
||
81 | */ |
||
82 | static function register_script_condition( $condition ) { |
||
83 | |||
84 | static::$_script_conditions[] = $condition; |
||
85 | |||
86 | } |
||
87 | |||
88 | static function _wp_enqueue_scripts_9() { |
||
89 | |||
90 | $key = static::api_key(); |
||
91 | $source = home_url( '/vendor/clubdeuce/wplib-olm-google-maps/dist/scripts/maps.min.js' ); |
||
92 | |||
93 | if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) { |
||
94 | $source = home_url( '/vendor/clubdeuce/wplib-olm-google-maps/assets/maps.js' ); |
||
95 | } |
||
96 | |||
97 | wp_register_script('google-maps', "https://maps.google.com/maps/api/js?v=3&key={$key}", false, '3.0', true ); |
||
98 | wp_register_script('map-control', $source, array( 'jquery', 'google-maps' ), '0.1.2', true ); |
||
99 | |||
100 | array_walk(static::$_script_conditions, function( $function ) { |
||
101 | return is_callable( $function ) ? call_user_func( $function ) : $function; |
||
102 | } ); |
||
103 | |||
104 | if ( in_array( true, static::$_script_conditions ) ) { |
||
105 | wp_enqueue_script( 'map-control' ); |
||
106 | } |
||
107 | |||
108 | } |
||
109 | |||
110 | /** |
||
111 | * @param string $address |
||
112 | * @param array $args |
||
113 | * @return Marker |
||
114 | */ |
||
115 | static function make_marker_by_address( $address, $args = array() ) { |
||
116 | |||
117 | $args = wp_parse_args( $args, array( |
||
118 | 'address' => $address, |
||
119 | ) ); |
||
120 | |||
121 | return new Marker( $args ); |
||
122 | |||
123 | } |
||
124 | |||
125 | /** |
||
126 | * @param string $destination |
||
127 | * @param array $args |
||
128 | * @return string |
||
129 | */ |
||
130 | static function driving_directions_href($destination, $args = array() ) { |
||
131 | |||
132 | $args = wp_parse_args( $args, array( |
||
1 ignored issue
–
show
Coding Style
introduced
by
![]() |
|||
133 | 'start' => 'My Location', |
||
134 | ) ); |
||
135 | |||
136 | return sprintf( 'https://maps.google.com/maps?saddr=%1$s&daddr=%2$s', urlencode( $args['start'] ), urlencode( $destination ) ); |
||
137 | } |
||
138 | |||
139 | } |
||
140 | |||
141 | Google_Maps::on_load(); |
||
142 |