Completed
Push — master ( 064ff4...fbf81a )
by Daryl
01:37
created

Google_Maps::version()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 5
ccs 0
cts 2
cp 0
rs 9.4285
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 9 and the first side effect is on line 229.

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.

Loading history...
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 $_version = '0.1.5';
17
18
    /**
19
     * @var string
20
     */
21
    protected static $_api_key = '';
22
23
    /**
24
     * @var Geocoder
25
     */
26
    protected static $_geocoder;
27
28
    /**
29
     * These conditions will be used to determine whether to enqueue the Google Maps JS.
30
     *
31
     * @var array
32
     */
33
    protected static $_script_conditions = array();
34
35
    /**
36
     * The path to this module's directory
37
     *
38
     * @var string
39
     */
40
    protected static $_source_dir = '/vendor/clubdeuce/wplib-olm-google-maps';
41
42
    /**
43
     * The url to this module's directory
44
     *
45
     * @var string
46
     */
47
    protected static $_source_url;
48
49
    static function on_load() {
50
51
        self::add_class_action( 'wp_enqueue_scripts', 9 );
52
53
    }
54
55
    /**
56
     * @return string
57
     */
58 1
    static function api_key() {
59
60 1
        return static::$_api_key;
61
62
    }
63
64
    /**
65
     * @return Geocoder
66
     */
67 1
    static function geocoder() {
68
69 1
        if ( ! isset( static::$_geocoder ) ) {
70 1
            static::$_geocoder = new Geocoder( ['api_key' => self::api_key() ] );
71
        }
72
73 1
        return static::$_geocoder;
74
75
    }
76
77
    /**
78
     * @param  array $args
79
     * @return Map
80
     */
81 1
    static function make_new_map( $args = array() ) {
82
83 1
        $class = static::INSTANCE_CLASS;
84 1
        return new $class( $args );
85
86
    }
87
88
    /**
89
     * @param string $key
90
     */
91 1
    static function register_api_key( $key ) {
92
93 1
        static::$_api_key = filter_var( $key, FILTER_SANITIZE_STRING );
94
95 1
    }
96
97
    /**
98
     * @param callable $condition
99
     */
100 1
    static function register_script_condition( $condition ) {
101
102 1
        static::$_script_conditions[] = $condition;
103
104 1
    }
105
106 2
    static function _wp_enqueue_scripts_9() {
107
108 2
        $key = static::api_key();
109 2
        $source = sprintf( '%1$s/dist/scripts/maps.min.js', self::source_url() );
110
111 2
        if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
112 2
            $source = sprintf( '%1$s/assets/maps.js', self::source_url() );
113
        }
114
115 2
        wp_register_script('google-maps', "https://maps.google.com/maps/api/js?v=3&key={$key}", false, '3.0', true );
116 2
        wp_register_script('map-control', $source, array( 'jquery', 'google-maps' ), self::version(), true );
117
118 2
        $conditions = array_map( array( __CLASS__, '_evaluate_condition' ), static::$_script_conditions );
119
120 2
        if ( in_array( true, $conditions ) ) {
121 1
            wp_enqueue_script( 'map-control' );
122
        }
123
124 2
    }
125
126 1
    static function script_conditions() {
127
128 1
        return static::$_script_conditions;
129
130
    }
131
132
    /**
133
     * @param  string $address
134
     * @param  array  $args
135
     * @return Marker
136
     */
137 1
    static function make_marker_by_address( $address, $args = array() ) {
138
139 1
        $args = wp_parse_args( $args, array(
140 1
            'address' => $address,
141
        ) );
142
143 1
        return new Marker( $args );
144
145
    }
146
147
    /**
148
     * @param  string $destination
149
     * @param  array  $args
150
     * @return string
151
     */
152 2
    static function driving_directions_href($destination, $args = array() ) {
153
154 2
        $args = wp_parse_args( $args, array(
155 2
            'start' => 'My Location',
156
        ) );
157
158 2
        return sprintf( 'https://maps.google.com/maps?saddr=%1$s&daddr=%2$s', urlencode( $args['start'] ), urlencode( $destination ) );
159
    }
160
161
    /**
162
     * @param string $path
163
     */
164
    static function register_source_dir( $path ) {
165
166
        if ( is_dir( $path ) ) {
167
            self::$_source_dir = $path;
168
        }
169
170
    }
171
172
    /**
173
     * @return string
174
     */
175
    static function source_dir() {
176
177
        return self::$_source_dir;
178
179
    }
180
181
    /**
182
     * @param $url
183
     */
184
    static function register_source_url( $url ) {
185
186
        self::$_source_url = $url;
187
188
    }
189
190
    /**
191
     * @return string
192
     */
193
    static function source_url() {
194
195
        $url = self::$_source_url;
196
197
        if ( is_ssl() ) {
198
            $url = preg_replace( '#^https*:\/\/([a-zA-z0-9\.]*)#', 'https://$1', $url );
199
        }
200
201
        return $url;
202
203
    }
204
205
    static function version() {
206
207
    	return self::$_version;
208
209
    }
210
211
    /**
212
     * @param  string|\Closure $callable
213
     * @return bool
214
     */
215 1
    private static function _evaluate_condition( $callable ) {
216
217 1
        $result = false;
218
219 1
        if ( is_callable( $callable ) ) {
220 1
            $result = call_user_func( $callable );
221
        }
222
223 1
        return $result;
224
225
    }
226
227
}
228
229
Google_Maps::on_load();
230