-
Notifications
You must be signed in to change notification settings - Fork 4
/
leaflet-php.php
452 lines (380 loc) · 14.4 KB
/
leaflet-php.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
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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
<?php
/**
* This file provides the LeafletPHP class
*
* @package LeafletPHP
*/
/**
* A PHP class for building custom Leaflet.js initialization functions.
*/
class LeafletPHP {
/**
* The version of LeafletPHP.
*
* @var $version
*/
public static $version;
/**
* Debug or not.
*
* @var $debug
*/
var $debug;
/**
* An additional classname to add to the wrapper div.
*
* @var $classname
*/
var $classname;
/**
* A global JS ID for the map wrapped object.
*
* @var $jsid
*/
var $jsid;
/**
* Default settings for our known plugins.
*
* @var $settings
*/
var $settings = array(
'leaflet' => array( 'center' => array( 0, 0 ), 'zoom' => 1 ),
'locatecontrol' => array(),
'draw' => array(),
);
/**
* The basemaps we're going to show.
*
* @var $basemaps
*/
var $basemaps = array(
array(
'type' => 'L.tileLayer',
'args' => array(
'//{s}.tile.openstreetmap.org/{z}/{x}/{y}.png',
array(
'maxZoom' => 19,
'attribution' => '© <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>',
),
),
'name' => 'default_basemap',
),
);
/**
* The regular layers we're going to show.
*
* @var $layers
*/
var $layers = array();
/**
* The map controls we're going to show.
*
* @var $controls
*/
var $controls = array();
/**
* Any additional scripts we're going to include within the IIFE
*
* @var $scripts
*/
var $scripts = array();
/**
* The constructor.
*
* @param array $args An array of Leaflet.js constructor arguments.
* @param string $jsid The ID that this IIFE will have.
* @param string $classname Additional classnames to put in the wrapper div.
*/
public function __construct( $args = array(), $jsid = '', $classname = '' ) {
if ( !defined( 'JSON_PRETTY_PRINT') ) {
define( 'JSON_PRETTY_PRINT', 128 );
}
$this->debug = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) || !empty($_GET['LEAFLETPHP_DEBUG']);
$this->pretty_print_json = ( $this->debug ? JSON_PRETTY_PRINT : 0 );
$this->newline = ( $this->debug ? "\n" : '' );
$this->tab = ( $this->debug ? " " : '' );
$this->newline_tab = $this->newline . $this->tab;
$this->add_settings( 'leaflet', $args );
$this->jsid = ( !empty( $jsid ) ? $jsid : 'leafletphp_' . rand() );
$this->classname = $classname;
}
/**
* Add settings for known objects.
*
* @param string $target What are these settings for.
* @param array $settings The array of settings.
*/
public function add_settings( $target, $settings ) {
$this->settings[ $target ] = array_merge( $this->settings[ $target ], $settings );
}
/**
* Get the output HTML, including the JS which will initialize the map.
*/
public function get_html() {
global $wp_scripts, $wp_styles;
$this->enqueue_scripts();
if ( ! empty( $this->jsid ) ) {
$this->jsid = $this->jsid;
}
$idtag = 'id="' . $this->jsid . '" ';
$classnames = array('leafletphp');
if ( ! empty( $this->classname ) ) {
$classnames[] = $this->classname;
}
/**
* Verify that needed css/js is available.
*
* If a handle is still in the queue, then it wasn't printed yet.
*/
$maybe_missing_css = array();
foreach( $wp_styles->queue as $handle ){
if ( strpos( $handle, 'leafletphp' ) === 0 ) {
$maybe_missing_css[] = $handle;
}
}
$missing_css = array();
if ( !empty( $maybe_missing_css ) ) {
$wp_styles->all_deps( $maybe_missing_css, true );
foreach ( $wp_styles->to_do as $handle ) {
$src = $wp_styles->registered[$handle]->src;
if ( empty( $src ) ) {
continue;
}
if ( ! preg_match( '|^(https?:)?//|', $src ) && ! ( $wp_styles->content_url && 0 === strpos( $src, $wp_styles->content_url ) ) ) {
$src = $wp_styles->base_url . $src;
}
$missing_css[] = add_query_arg( 'ver', $wp_styles->registered[$handle]->ver, $src);
}
}
$maybe_missing_js = array();
foreach( $wp_scripts->queue as $handle ){
if ( strpos( $handle, 'leafletphp' ) === 0 ) {
$maybe_missing_js[] = $handle;
}
}
$missing_js = array();
if ( !empty( $maybe_missing_js ) ) {
$wp_scripts->all_deps( $maybe_missing_js, true );
foreach( $wp_scripts->to_do as $handle ) {
$src = $wp_scripts->registered[$handle]->src;
if ( empty( $src ) ) {
continue;
}
if ( ! preg_match( '|^(https?:)?//|', $src ) && ! ( $wp_scripts->content_url && 0 === strpos( $src, $wp_scripts->content_url ) ) ) {
$src = $wp_scripts->base_url . $src;
}
$missing_js[$handle] = add_query_arg( 'ver', $wp_scripts->registered[$handle]->ver, $src );
}
}
$html = array();
// Set up the div wrapper.
$html[] = '<div class="leafletphpwrap">';
$html[] = '<div ' . $idtag . ' class="' . implode( ' ', $classnames ) . '" data-leafletphp="' . $this->jsid . '">';
$html[] = '<script data-leafletphp="' . $this->jsid . '">';
$html[] = 'window.leafletphp = window.leafletphp || {' . $this->newline_tab . 'js_deferreds:[],' . $this->newline_tab . 'maps:{}' . $this->newline . '};';
$html[] = 'jQuery(document).ready(function(){';
// Load needed css and JS
if ( !empty( $missing_css ) ) {
$html[] = 'var maybe_missing_css = ' . $this->json_encode( $missing_css ) . ";";
$html[] = 'jQuery(maybe_missing_css).each(function(i,css){';
$html[] = $this->tab . 'if ( jQuery(\'link[href="\'+css+\'"]\').length === 0 ) {';
$html[] = $this->tab . 'jQuery(\'head\').append(\'<link rel="stylesheet" href="\'+css+\'">\');';
$html[] = '}});';
}
if ( !empty( $missing_js ) ) {
$html[] = 'var load_missing_js = function(js){';
$html[] = $this->tab . 'if ( jQuery(\'script[src="\'+js+\'"]\').length === 0 ) {';
$html[] = $this->tab . $this->tab .'jQuery(\'head\').append(\'<script src="\'+js+\'">\');';
$html[] = $this->tab . $this->tab .'window.leafletphp.js_deferreds.push(jQuery.getScript(js));';
$html[] = '}};';
/**
* Do smart loading here so we don't wipe out any plugins that have been loaded for JQuery or Leaflet.
*/
if ( !empty( $missing_js['leafletphp-leaflet-js'] ) ) {
$html[] = "if ( typeof L === 'undefined' ) { " . $this->newline_tab . "load_missing_js('" . $missing_js['leafletphp-leaflet-js'] . "'); " . $this->newline . "};";
}
if ( !empty( $missing_js['leafletphp-draw-js'] ) ) {
$html[] = "if ( typeof L === 'undefined' || typeof L.Draw === 'undefined' ) { " . $this->newline_tab . "load_missing_js('" . $missing_js['leafletphp-draw-js'] . "'); " . $this->newline . "};";
}
if ( !empty( $missing_js['leafletphp-locate-js'] ) ) {
$html[] = "if ( typeof L === 'undefined' || typeof L.Control.Locate=== 'undefined' ) { " . $this->newline_tab . "load_missing_js('" . $missing_js['leafletphp-locate-js'] . "'); " . $this->newline . "};";
}
}
$html[] = 'jQuery.when.apply(jQuery,window.leafletphp.js_deferreds).then( function() { ' . $this->newline_tab . 'new function(){';
// Verify that the map isn't already initialized. If it is, we've done this already so return.
$html[] = $this->tab . 'if ( ' . $this->jsid . ' !== undefined && ' . $this->jsid . '.map instanceof L.Map ) {';
$html[] = $this->tab . $this->tab . 'return;';
$html[] = $this->tab . '}';
// Short icons for draw toolbar.
$html[] = $this->tab . "if ( L.drawLocal !== undefined ) {";
$html[] = $this->tab . $this->tab . "L.drawLocal.draw.toolbar.actions.text = 'X';"; // Cancel
$html[] = $this->tab . $this->tab . "L.drawLocal.draw.toolbar.finish.text = '💾';"; // Save
$html[] = $this->tab . $this->tab . "L.drawLocal.draw.toolbar.undo.text = '↩';"; // Undo
$html[] = $this->tab . $this->tab . "L.drawLocal.edit.toolbar.actions.save.text = '💾';"; // Save
$html[] = $this->tab . $this->tab . "L.drawLocal.edit.toolbar.actions.cancel.text = 'X';"; // Cancel
$html[] = $this->tab . $this->tab . "L.drawLocal.edit.toolbar.actions.clearAll.text = '💥';"; // Cancel
$html[] = $this->tab . "}";
// Set a script ID
$html[] = $this->tab . 'this.scriptid = "' . $this->jsid . '";';
// Initialize Leaflet.
$html[] = $this->tab . 'var map = this.map = L.map("' . $this->jsid . '", ' . $this->json_encode( $this->settings['leaflet'] ) . ');';
// Initialize basemap(s).
$html[] = 'this.basemaps = {};';
foreach ( $this->basemaps as $basemap ) {
if ( empty( $basemap['name'] ) ) {
$basemap['name'] = 'basemap_' . rand();
}
$html[] = $this->tab . 'var ' . $basemap['name'] . ' = this.basemaps.' . $basemap['name'] . ' = ' . $basemap['type'] . $this->newline_tab . '.apply(this,' . $this->json_encode( $basemap['args'] ) . ')' . $this->newline_tab . '.addTo(this.map);';
}
// Initialize layers.
$html[] = 'this.layers = {};';
foreach ( $this->layers as $layer ) {
if ( empty( $layer['name'] ) ) {
$layer['name'] = 'layer_' . rand();
}
$html[] = $this->tab . 'var ' . $layer['name'] . ' = this.layers.' . $layer['name'] . '= ' . $layer['type'] . $this->newline_tab . '.apply(this,' . $this->json_encode( $layer['args'] ) . ')' . $this->newline_tab . '.addTo(this.map);';
}
// Initialize controls.
$html[] = 'this.controls = {};';
foreach ( $this->controls as $control ) {
if ( empty( $control['name'] ) ) {
$control['name'] = 'control_' . rand();
}
$html[] = $this->tab . 'var ' . $control['name'] . ' = this.controls.' . $control['name'] . ' = new ' . $control['type'] . '(' . $this->json_encode( $control['args'] ) . ');';
$html[] = $this->tab . 'this.map.addControl(' . $control['name'] . ');';
}
// Set up reference to inside the container.
$html[] = $this->tab . 'window.' . $this->jsid . ' = window.leafletphp.maps.' . $this->jsid . ' = this;';
// Add user scripts here at the bottom.
foreach ( $this->scripts as $script ) {
$html[] = $this->tab . $script;
}
$html[] = $this->tab . 'jQuery("#' . $this->jsid . '").trigger("leafletphp/loaded",this);';
$html[] = '};});});' . "\n" . '</script>';
$html[] = '</div></div>';
$html[] = '<div class="leafletphpspacer" data-leafletphp="' . $this->jsid . '"></div>';
if ( $this->debug ) {
$output = "\n" . implode( "\n",$html ) . "\n";
} else {
$output = implode( '',$html );
}
return $output;
}
/**
* Add a basemap.
*
* @param string $type The type of basemap to add.
* @param array $args The arguments for the basemap.
* @param string $layer_name A name for the layer.
*/
public function add_basemap( $type, $args, $layer_name = '' ) {
$this->layers[] = array(
'type' => $type,
'args' => $args,
'name' => $layer_name,
);
}
/**
* Add a regular layer.
*
* @param string $type The type of layer to add.
* @param array $args The arguments for the layer.
* @param string $layer_name A name for the layer.
*/
public function add_layer( $type, $args, $layer_name = '' ) {
$this->layers[] = array(
'type' => $type,
'args' => $args,
'name' => $layer_name,
);
}
/**
* Add a control.
*
* @param string $type The type of control to add.
* @param array $args The arguments for the control.
* @param string $control_name A name for the control.
*/
public function add_control( $type, $args, $control_name = '' ) {
$this->controls[] = array(
'type' => $type,
'args' => $args,
'name' => $control_name,
);
}
/**
* Add user scripts which will run inside the IIFE.
*
* @param string $script The JS script to run (no <script> tags needed).
*/
public function add_script( $script ) {
$this->scripts[] = $script;
}
/**
* Callback handler to enqueue scripts.
*
* NOTE: Don't forget to add conditional browser-side auto-loading down in get_html().
*/
public function enqueue_scripts() {
$baseurl = plugins_url( dirname( plugin_basename( __FILE__ ) ) );
$leafletphp_needs_js = array('leafletphp-leaflet-js');
$leafletphp_needs_css = array('leafletphp-leaflet-css');
// Always enqueue these.
if ( $this->debug ) {
wp_enqueue_script( 'leafletphp-leaflet-js', $baseurl . '/assets/leaflet/leaflet-src.js', array( 'jquery' ), LeafletPHP::$version );
} else {
wp_enqueue_script( 'leafletphp-leaflet-js', $baseurl . '/assets/leaflet/leaflet.js', array( 'jquery' ), LeafletPHP::$version );
}
wp_enqueue_style( 'leafletphp-leaflet-css', $baseurl . '/assets/leaflet/leaflet.css', array(), LeafletPHP::$version );
// Enqueue needed control scripts.
foreach ( $this->controls as $control ) {
switch ( $control['type'] ) {
case 'L.Control.Draw':
$leafletphp_needs_js[] = 'leafletphp-draw-js';
if ( $this->debug ) {
wp_enqueue_script( 'leafletphp-draw-js', $baseurl . '/assets/Leaflet.draw/dist/leaflet.draw-src.js', array( 'leafletphp-leaflet-js' ), LeafletPHP::$version );
} else {
wp_enqueue_script( 'leafletphp-draw-js', $baseurl . '/assets/Leaflet.draw/dist/leaflet.draw.js', array( 'leafletphp-leaflet-js' ), LeafletPHP::$version );
}
$leafletphp_needs_css[] = 'leafletphp-draw-css';
wp_enqueue_style( 'leafletphp-draw-css', $baseurl . '/assets/Leaflet.draw/dist/leaflet.draw.css', array( 'leafletphp-leaflet-css' ), LeafletPHP::$version );
break;
case 'L.Control.Locate':
$leafletphp_needs_js[] = 'leafletphp-locate-js';
if ( $this->debug ) {
wp_enqueue_script( 'leafletphp-locate-js', $baseurl . '/assets/leaflet-locatecontrol/dist/L.Control.Locate.js', array( 'leafletphp-leaflet-js' ), LeafletPHP::$version );
} else {
wp_enqueue_script( 'leafletphp-locate-js', $baseurl . '/assets/leaflet-locatecontrol/dist/L.Control.Locate.min.js', array( 'leafletphp-leaflet-js' ), LeafletPHP::$version );
}
$leafletphp_needs_css[] = 'leafletphp-locate-css';
wp_enqueue_style( 'leafletphp-locate-css', $baseurl . '/assets/leaflet-locatecontrol/dist/L.Control.Locate.min.css', array( 'leafletphp-leaflet-css' ), LeafletPHP::$version );
break;
}
}
// Finally, enqueue leafletphp, so it's last.
wp_enqueue_script( 'leafletphp-leafletphp-js', $baseurl . '/assets/leafletphp.js', $leafletphp_needs_js, LeafletPHP::$version );
wp_enqueue_style( 'leafletphp-css', $baseurl . '/assets/leafletphp.css', $leafletphp_needs_css, LeafletPHP::$version );
}
/**
* Get the map ID.
*/
public function get_id() {
return $this->jsid;
}
/**
* Encode JSON, but strip quotes from strings wrapped in "@@@this@@@" so they'll be variables.
*
* @param array $array Something to json_encode.
*/
public function json_encode( $array ) {
$ret = wp_json_encode( $array, $this->pretty_print_json );
$ret = preg_replace( '/:\s*"@@@(.*?)@@@"([,}])?/s',':\1\2',$ret );
return $ret;
}
/**
* Magic method to print the html.
*/
public function __toString() {
return $this->get_html();
}
}