-
-
Notifications
You must be signed in to change notification settings - Fork 74
Expand file tree
/
Copy pathclass.map-shortcode.php
More file actions
328 lines (278 loc) · 11.3 KB
/
Copy pathclass.map-shortcode.php
File metadata and controls
328 lines (278 loc) · 11.3 KB
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
<?php
/**
* Map Shortcode
*
* Displays map with [leaflet-map ...atts]
*
* JavaScript equivalent : L.map("id");
*
* @category Shortcode
* @author Benjamin J DeLong <ben@bozdoz.com>
*/
// Exit if accessed directly
if (!defined('ABSPATH')) {
exit;
}
require_once LEAFLET_MAP__PLUGIN_DIR . 'shortcodes/class.shortcode.php';
/**
* Leaflet_Map_Shortcode Class
*/
class Leaflet_Map_Shortcode extends Leaflet_Shortcode
{
/**
* Instantiate class
*/
public function __construct()
{
parent::__construct();
$this->enqueue();
}
/**
* Enqueue Scripts and Styles for Leaflet
*
* @return null
*/
protected function enqueue()
{
wp_enqueue_style('leaflet_stylesheet');
// TODO needs to load leaflet_js first as module
wp_enqueue_script('wp_leaflet_map');
if (wp_script_is('leaflet_mapquest_plugin', 'registered')) {
// mapquest doesn't accept direct tile access as of July 11, 2016
wp_enqueue_script('leaflet_mapquest_plugin');
}
// enqueue user-defined scripts
// ! will fire for each map
do_action('leaflet_map_enqueue');
}
/**
* Merge shortcode options with default options
*
* @param array|string $atts key value pairs from shortcode
*
* @return array new atts, which is actually an array
*/
protected function getAtts($atts='')
{
$atts = (array) $atts;
extract($atts, EXTR_SKIP);
$settings = Leaflet_Map_Plugin_Settings::init();
$atts['zoom'] = array_key_exists('zoom', $atts) ?
$zoom : $settings->get('default_zoom');
$atts['height'] = empty($height) ?
$settings->get('default_height') : $height;
$atts['width'] = empty($width) ? $settings->get('default_width') : $width;
$atts['zoomcontrol'] = isset($zoomControl)
? $zoomControl
: (array_key_exists('zoomcontrol', $atts)
? $zoomcontrol
: $settings->get('show_zoom_controls'));
$atts['min_zoom'] = array_key_exists('min_zoom', $atts) ?
$min_zoom : $settings->get('default_min_zoom');
$atts['max_zoom'] = empty($max_zoom) ?
$settings->get('default_max_zoom') : $max_zoom;
$atts['scrollwheel'] = isset($scrollWheelZoom)
? $scrollWheelZoom
: (array_key_exists('scrollwheel', $atts)
? $scrollwheel
: $settings->get('scroll_wheel_zoom'));
$atts['doubleclickzoom'] = isset($doubleClickZoom)
? $doubleClickZoom
: (array_key_exists('doubleclickzoom', $atts)
? $doubleclickzoom
: $settings->get('double_click_zoom'));
// @deprecated backwards-compatible fit_markers
$atts['fit_markers'] = array_key_exists('fit_markers', $atts) ?
$fit_markers : $settings->get('fit_markers');
// fitbounds is what it should be called @since v2.12.0
$atts['fitbounds'] = array_key_exists('fitbounds', $atts) ?
$fitbounds : $atts['fit_markers'];
/* allow percent, but add px for ints */
$atts['height'] .= is_numeric($atts['height']) ? 'px' : '';
$atts['width'] .= is_numeric($atts['width']) ? 'px' : '';
/*
need to allow 0 or empty for removal of attribution
*/
if (!array_key_exists('attribution', $atts)) {
$atts['attribution'] = $settings->get('default_attribution');
}
/* allow a bunch of other (boolean) options */
// http://leafletjs.com/reference.html#map
$map_options = array(
'closePopupOnClick' => isset($closePopupOnClick)
? $closePopupOnClick
: (isset($closepopuponclick)
? $closepopuponclick
: null),
'trackResize' => isset($trackResize)
? $trackResize
: (isset($trackresize)
? $trackresize
: null),
'boxZoom' => isset($boxzoom)
? $boxzoom
: (isset($boxZoom)
? $boxZoom
: null),
'touchZoom' => isset($touchZoom) ? $touchZoom : null,
'dragging' => isset($dragging) ? $dragging : null,
'keyboard' => isset($keyboard) ? $keyboard : null,
'zoomAnimation' => isset($zoomAnimation) ? $zoomAnimation : null,
'fadeAnimation' => isset($fadeAnimation) ? $fadeAnimation : null,
'markerZoomAnimation' => isset($markerZoomAnimation) ? $markerZoomAnimation : null,
'inertia' => isset($inertia) ? $inertia : null,
'worldCopyJump' => isset($worldCopyJump) ? $worldCopyJump : null,
'tap' => isset($tap) ? $tap : null,
'bounceAtZoomLimits' => isset($bounceAtZoomLimits) ? $bounceAtZoomLimits : null,
// defined above, but can be validated here
'zoomControl' => $atts['zoomcontrol'],
'scrollWheelZoom' => $atts['scrollwheel'],
'doubleClickZoom' => $atts['doubleclickzoom']
);
// filter out nulls
$map_options = $this->LM->filter_null($map_options);
// custom field for moving to JavaScript
$map_options['fitBounds'] = $atts['fitbounds'];
// change string booleans to booleans
$map_options = filter_var_array($map_options, FILTER_VALIDATE_BOOLEAN);
// add min/max zoom validations
$zoom_options = array(
'minZoom' => $atts['min_zoom'],
'maxZoom' => $atts['max_zoom']
);
$zoom_options = filter_var_array($zoom_options, FILTER_VALIDATE_FLOAT);
$map_options = array_merge(
$map_options,
$zoom_options
);
// update atts too
$atts['minZoom'] = $zoom_options['minZoom'];
$atts['maxZoom'] = $zoom_options['maxZoom'];
$map_options['maxBounds'] = isset($maxbounds) ? $this->LM->convert_bounds_str_to_arr($maxbounds) : null;
// custom field for moving to javascript
// filter out any unwanted HTML tags (including img)
if ($atts['attribution'] !== 0) {
$map_options['attribution'] = wp_kses_post($atts['attribution']);
}
// wrap as JSON
$atts['map_options'] = json_encode($map_options);
// get raw variables, allowing for JavaScript variables in values
$raw_map_options = array();
foreach($map_options as $key=>$val) {
$original_value = isset($atts[$key]) ? $atts[$key] : null;
$liquid = $this->LM->liquid($original_value);
if ($liquid && isset($liquid['raw']) && $liquid['raw']) {
// raw leaves original value un-quoted
$raw_map_options[$key] = $liquid['original'];
}
}
$atts['raw_map_options'] = $this->LM->rawDict($raw_map_options);
$tile_layer_options = array(
'tileSize' => empty($tilesize) ? $settings->get('tilesize') : $tilesize,
'subdomains' => empty($subdomains) ? $settings->get('map_tile_url_subdomains') : $subdomains,
'id' => empty($mapid) ? $settings->get('mapid') : $mapid,
'accessToken' => empty($accesstoken) ? $settings->get('accesstoken') : $accesstoken,
'zoomOffset' => empty($zoomoffset) ? $settings->get('zoomoffset') : $zoomoffset,
'noWrap' => filter_var(empty($nowrap) ? $settings->get('tile_no_wrap') : $nowrap, FILTER_VALIDATE_BOOLEAN),
'maxZoom' => $atts['maxZoom']
);
$tile_layer_options = $this->LM->filter_empty_string($tile_layer_options);
$tile_layer_options = $this->LM->filter_null($tile_layer_options);
if (isset($tile_layer_options['subdomains']) && strpos($tile_layer_options['subdomains'], ',') !== false) {
// subdomains can be comma-separated
$tile_layer_options['subdomains'] = explode(',', $tile_layer_options['subdomains']);
}
$atts['tile_layer_options'] = json_encode($tile_layer_options, JSON_NUMERIC_CHECK | JSON_UNESCAPED_SLASHES);
// TODO: find a better way to do this
$validations = array(
'detect_retina' => FILTER_VALIDATE_BOOLEAN,
'zoom' => FILTER_VALIDATE_FLOAT,
'fitBounds' => FILTER_VALIDATE_BOOLEAN
);
$atts = $this->LM->sanitize_inclusive($atts, $validations);
return $atts;
}
/**
* Get the div tag for the map to instantiate
*
* @param string $height
* @param string $width
*
* @return string HTML div element
*/
protected function getDiv($height, $width) {
// div does not get wrapped in script tags
ob_start();
?>
<div class="leaflet-map WPLeafletMap" style="height:<?php
echo esc_attr($height);
?>; width:<?php
echo esc_attr($width);
?>;"></div><?php
return ob_get_clean();
}
/**
* Get script for shortcode
*
* @param array $atts sometimes this is null
* @param string $content anything within a shortcode
*
* @return string HTML
*/
protected function getHTML($atts='', $content=null)
{
extract($this->getAtts($atts));
if (!empty($address)) {
/* try geocoding */
include_once LEAFLET_MAP__PLUGIN_DIR . 'class.geocoder.php';
$location = new Leaflet_Geocoder($address);
$lat = $location->lat;
$lng = $location->lng;
}
$settings = Leaflet_Map_Plugin_Settings::init();
// map uses lat/lng
$lat = isset($lat) ? $lat : $settings->get('default_lat');
$lng = isset($lng) ? $lng : $settings->get('default_lng');
// validate lat/lng
$lat = $this->LM->filter_float($lat);
$lng = $this->LM->filter_float($lng);
/*
mapquest doesn't need tile urls
*/
if (wp_script_is('leaflet_mapquest_plugin', 'registered')) {
$tileurl = '';
} else {
$tileurl = empty($tileurl) ? $settings->get('map_tile_url') : $tileurl;
}
$detect_retina = empty($detect_retina) ? $settings->get('detect_retina') : $detect_retina;
$detect_retina = filter_var($detect_retina, FILTER_VALIDATE_BOOLEAN);
/* should be iterated for multiple maps */
ob_start();
?>/*<script>*/
var baseUrl = atob('<?php echo base64_encode(filter_var($tileurl, FILTER_SANITIZE_URL)); ?>');
var base = (!baseUrl && window.MQ) ?
window.MQ.mapLayer() : L.tileLayer(baseUrl,
Object.assign({}, {
detectRetina: <?php echo $detect_retina ? '1' : '0'; ?>,
},
<?php echo $tile_layer_options; ?>
)
);
var options = Object.assign({}, {
layers: [base],
attributionControl: false
},
<?php echo $map_options; ?>,
<?php echo $raw_map_options; ?>
);
window.WPLeafletMapPlugin.createMap(options).setView(<?php
echo '[' . $lat . ',' . $lng . '],' . $zoom;
?>);<?php
$show_scale = isset($show_scale) ? $show_scale : $settings->get('show_scale');
if ($show_scale) {
echo do_shortcode('[leaflet-scale noScriptWrap]');
}
$script = ob_get_clean();
return $this->getDiv($height, $width) . $this->wrap_script($script, 'WPLeafletMapShortcode');
}
}