-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgoogle-maps.php
146 lines (124 loc) · 3.28 KB
/
google-maps.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<?php
/**
* WP Google Maps API
*
* @package WP-Google-Maps-API
*/
/*
* Plugin Name: WP Google Maps API
* Plugin URI: https://imforza.com
* Description: Simple library for displaying custom maps.
* Author: imFORZA
* Version: 1.0.0
* Author URI: https://imforza.com
* GitHub Plugin URI: https://github.com/imforza/wp-google-maps-api
* GitHub Branch: master
* GitHub Contributors: sfgarza
*/
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) { exit; }
if ( ! class_exists( 'WPAPI_GOOGLE_MAPS' ) ) {
include_once( 'maps-widget.php' );
/**
* GreatSchools API Class.
*/
class WPAPI_GOOGLE_MAPS {
/**
* API Key.
*
* @var string
*/
static private $api_key;
/**
* Output Type.
*
* @var string
*/
static private $output;
/**
* Map data to be sent to JS.
*
* @var [Array]
*/
static private $map_data;
/**
* Default map options.
*
* @var [Array]
*/
static public $defaults = array(
'width' => '300px',
'height' => '300px',
'lat' => '-17.7134',
'lng' => '178.0650',
'info' => '',
'style' => '[]',
'zoom' => 8,
);
/**
* __construct function.
*
* @access public
* @param String $api_key : API Key.
*/
public function __construct( $api_key ) {
static::$api_key = $api_key;
add_action( 'wp_footer', array( $this, 'footer' ), 11 );
add_action( 'widgets_init', array( $this, 'register_widgets' ) );
add_action( 'wp_enqueue_scripts', array( $this, 'enqueue' ) );
add_shortcode( 'wp_google_maps', array( $this, 'shortcode' ) );
}
/**
* Enqueue JS.
*/
public function enqueue() {
wp_enqueue_script( 'wpapi-google-maps',plugins_url( 'google-maps.js', __FILE__ ), array( 'jquery' ), null, true );
}
/**
* Handle multiple google maps js api enqueues on the footer.
*/
public function footer() {
wp_localize_script( 'wpapi-google-maps', 'wpapi_gmaps', static::$map_data );
// Only enqueue google maps API if yoast hasnt enqueued it already.
if ( ! wp_script_is( 'maps-geocoder' ) ) {
wp_enqueue_script( 'google-maps-api', 'https://maps.googleapis.com/maps/api/js?key=' . static::$api_key, array(), null );
}
}
/**
* Print dat map.
*
* @param [Mixed] $map_data : Array of map data to send to js.
* @param [Bool] $echo : If html should be returned or echoed, defaults to true.
*/
public static function print_map( $map_data, $echo = true ) {
$map_data = apply_filters( 'wpapi_google_map_data', wp_parse_args( $map_data, static::$defaults ) );
static::$map_data[] = $map_data;
$index = count( static::$map_data ) - 1;
$html = '<div id="wpapi-gmap-' . $index . '" style="width:' . esc_attr( $map_data['width'] ) . ';height:' . esc_attr( $map_data['height'] ) . '"></div>';
if ( $echo ) {
echo $html;
} else {
return $html;
}
}
/**
* Shortcode for printing a single map.
*
* @param [type] $atts : shortcode attributes.
*/
public function shortcode( $atts ) {
// Set widget info.
$atts = shortcode_atts( static::$defaults, $atts, 'wp_google_maps' );
return static::print_map( $atts, false );
}
/**
* Register Google maps Widgets.
*
* @access public
* @return void
*/
public function register_widgets() {
register_widget( 'WP_API_MAPS_WIDGET' );
}
}
}