clubdeuce /
wplib-olm-google-maps
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 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
Loading history...
|
|||
| 113 | 'address' => $address, |
||
| 114 | ) ); |
||
| 115 | |||
| 116 | return new Marker( $args ); |
||
| 117 | |||
| 118 | } |
||
| 119 | |||
| 120 | } |
||
| 121 | |||
| 122 | Google_Maps::on_load(); |
||
| 123 |