Completed
Push — master ( 40b8f9...e7d3a7 )
by Daryl
01:32
created

component-google-maps.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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 218.

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 $_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 1
    static function api_key() {
54
55 1
        return static::$_api_key;
56
57
    }
58
59
    /**
60
     * @return Geocoder
61
     */
62 1
    static function geocoder() {
63
64 1
        if ( ! isset( static::$_geocoder ) ) {
65 1
            static::$_geocoder = new Geocoder( ['api_key' => self::api_key() ] );
66
        }
67
68 1
        return static::$_geocoder;
69
70
    }
71
72
    /**
73
     * @param  array $args
74
     * @return Map
75
     */
76 1
    static function make_new_map( $args = array() ) {
77
78 1
        $class = static::INSTANCE_CLASS;
79 1
        return new $class( $args );
80
81
    }
82
83
    /**
84
     * @param string $key
85
     */
86 1
    static function register_api_key( $key ) {
87
88 1
        static::$_api_key = filter_var( $key, FILTER_SANITIZE_STRING );
89
90 1
    }
91
92
    /**
93
     * @param callable $condition
94
     */
95 1
    static function register_script_condition( $condition ) {
96
97 1
        static::$_script_conditions[] = $condition;
98
99 1
    }
100
101 2
    static function _wp_enqueue_scripts_9() {
102
103 2
        $key = static::api_key();
104 2
        $source = sprintf( '%1$s/dist/scripts/maps.min.js', self::source_url() );
105
106 2
        if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
107 2
            $source = sprintf( '%1$s/assets/maps.js', self::source_url() );
108
        }
109
110 2
        wp_register_script('google-maps', "https://maps.google.com/maps/api/js?v=3&key={$key}", false, '3.0', true );
111 2
        wp_register_script('map-control', $source, array( 'jquery', 'google-maps' ), '0.1.2', true );
112
113 2
        $conditions = array_map( array( __CLASS__, '_evaluate_condition' ), static::$_script_conditions );
114
115 2
        if ( in_array( true, $conditions ) ) {
116 1
            wp_enqueue_script( 'map-control' );
117
        }
118
119 2
    }
120
121 1
    static function script_conditions() {
122
123 1
        return static::$_script_conditions;
124
125
    }
126
127
    /**
128
     * @param  string $address
129
     * @param  array  $args
130
     * @return Marker
131
     */
132 1
    static function make_marker_by_address( $address, $args = array() ) {
133
134 1
        $args = wp_parse_args( $args, array(
135 1
            'address' => $address,
136
        ) );
137
138 1
        return new Marker( $args );
139
140
    }
141
142
    /**
143
     * @param  string $destination
144
     * @param  array  $args
145
     * @return string
146
     */
147 2
    static function driving_directions_href($destination, $args = array() ) {
148
149 2
        $args = wp_parse_args( $args, array(
150 2
            'start' => 'My Location',
151
        ) );
152
153 2
        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
        $url = self::$_source_url;
191
192
        if ( is_ssl() ) {
193
            $url = preg_replace( '#^https*:\/\/([a-zA-z0-9\.]*)#', 'https://$1', $url );
194
        }
195
196
        return $url;
197
198
    }
199
200
    /**
201
     * @param  string|\Closure $callable
202
     * @return bool
203
     */
204 1
    private static function _evaluate_condition( $callable ) {
205
206 1
        $result = false;
207
208 1
        if ( is_callable( $callable ) ) {
209 1
            $result = call_user_func( $callable );
210
        }
211
212 1
        return $result;
213
214
    }
215
216
}
217
218
Google_Maps::on_load();
219