Completed
Push — master ( ed2425...d8adec )
by Daryl
01:55
created

Google_Maps::make_marker_by_address()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 9
rs 9.6666
cc 1
eloc 4
nc 1
nop 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 122.

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
92
        wp_register_script('google-maps', "https://maps.google.com/maps/api/js?key={$key}", false, '3.0', true );
93
        wp_register_script('map-control', home_url( '/vendor/clubdeuce/wplib-olm-google-maps/assets/maps.js' ), array( 'jquery', 'google-maps' ), '0.1', true );
94
95
        array_walk(static::$_script_conditions, function( $function ) {
96
            return is_callable( $function ) ? call_user_func( $function ) : $function;
97
        } );
98
99
        if ( in_array( true, static::$_script_conditions ) ) {
100
            wp_enqueue_script( 'map-control' );
101
        }
102
103
    }
104
105
    /**
106
     * @param  string $address
107
     * @param  array  $args
108
     * @return Marker
109
     */
110
    static function make_marker_by_address( $address, $args ) {
111
112
        $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...
113
            'address' => $address,
114
        ) );
115
116
        return new Marker( $args );
117
118
    }
119
120
}
121
122
Google_Maps::on_load();
123