Completed
Push — master ( 898e77...f00d09 )
by Daryl
04:31
created

Google_Maps::api_key()   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 141.

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
        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(
1 ignored issue
show
Coding Style introduced by
Consider using a different name than the parameter $args. This often makes code more readable.
Loading history...
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
Consider using a different name than the parameter $args. This often makes code more readable.
Loading history...
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