Completed
Push — master ( 7dd0f8...48ecf0 )
by Daryl
02:02
created

Google_Maps::script_conditions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 5
rs 9.4285
cc 1
eloc 2
nc 1
nop 0
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 161.

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
    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
        $conditions = array_map( array( __CLASS__, '_evaluate_condition' ), static::$_script_conditions );
101
102
        if ( in_array( true, $conditions ) ) {
103
            wp_enqueue_script( 'map-control' );
104
        }
105
106
    }
107
108
    static function script_conditions() {
109
110
        return static::$_script_conditions;
111
112
    }
113
114
    /**
115
     * @param  string $address
116
     * @param  array  $args
117
     * @return Marker
118
     */
119
    static function make_marker_by_address( $address, $args = array() ) {
120
121
        $args = wp_parse_args( $args, array(
122
            'address' => $address,
123
        ) );
124
125
        return new Marker( $args );
126
127
    }
128
129
    /**
130
     * @param  string $destination
131
     * @param  array  $args
132
     * @return string
133
     */
134
    static function driving_directions_href($destination, $args = array() ) {
135
136
        $args = wp_parse_args( $args, array(
137
            'start' => 'My Location',
138
        ) );
139
140
        return sprintf( 'https://maps.google.com/maps?saddr=%1$s&daddr=%2$s', urlencode( $args['start'] ), urlencode( $destination ) );
141
    }
142
143
    /**
144
     * @param  string|\Closure $callable
145
     * @return bool
146
     */
147
    private static function _evaluate_condition( $callable ) {
148
149
        $result = false;
150
151
        if ( is_callable( $callable ) ) {
152
            $result = call_user_func( $callable );
153
        }
154
155
        return $result;
156
157
    }
158
159
}
160
161
Google_Maps::on_load();
162