Completed
Push — master ( ab0124...f7497b )
by Daryl
01:56
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 8 and the first side effect is on line 105.

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
8
class Google_Maps extends \WPLib_Module_Base {
9
10
    const INSTANCE_CLASS = 'Clubdeuce\WPLib\Components\GoogleMaps\Map';
11
12
    /**
13
     * @var string
14
     */
15
    protected static $_api_key = '';
16
17
    /**
18
     * @var Geocoder
19
     */
20
    protected static $_geocoder;
21
22
    /**
23
     * These conditions will be used to determine whether to enqueue the Google Maps JS.
24
     *
25
     * @var array
26
     */
27
    protected static $_script_conditions = array();
28
29
30
    static function on_load() {
31
32
        self::add_class_action( 'wp_enqueue_scripts', 9 );
33
34
    }
35
36
    /**
37
     * @return string
38
     */
39
    static function api_key() {
40
41
        return static::$_api_key;
42
43
    }
44
45
    /**
46
     * @return Geocoder
47
     */
48
    static function geocoder() {
49
50
        if ( ! isset( self::$_geocoder ) ) {
51
            self::$_geocoder = new Geocoder( ['api_key' => GOOGLE_MAPS_API_KEY ] );
52
        }
53
54
        return self::$_geocoder;
55
56
    }
57
58
    /**
59
     * @param  array $args
60
     * @return Map
61
     */
62
    static function make_new_map( $args = array() ) {
63
64
        return new Map( $args );
65
66
    }
67
68
    /**
69
     * @param string $key
70
     */
71
    static function register_api_key( $key ) {
72
73
        static::$_api_key = filter_var( $key, FILTER_SANITIZE_STRING );
74
75
    }
76
77
    /**
78
     * @param callable $condition
79
     */
80
    static function register_script_condition( $condition ) {
81
82
        static::$_script_conditions[] = $condition;
83
84
    }
85
86
    static function _wp_enqueue_scripts_9() {
87
88
        $key = static::api_key();
89
90
        wp_register_script('google-maps', "https://maps.google.com/maps/api/js?key={$key}", false, '3.0', true );
91
        wp_register_script('map-control', home_url( '/vendor/clubdeuce/wplib-olm-google-maps/assets/maps.js' ), array( 'jquery', 'google-maps' ), '0.1', true );
92
93
        array_walk(static::$_script_conditions, function( $function ) {
94
            return is_callable( $function ) ? call_user_func( $function ) : $function;
95
        } );
96
97
        if ( in_array( true, static::$_script_conditions ) ) {
98
            wp_enqueue_script( 'map-control' );
99
        }
100
101
    }
102
103
}
104
105
Google_Maps::on_load();
106