This repository has been archived by the owner on Mar 12, 2020. It is now read-only.
forked from bozdoz/wp-plugin-leaflet-map
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathleaflet-map.php
591 lines (458 loc) · 24.4 KB
/
leaflet-map.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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
<?php
/*
Author: bozdoz
Author URI: http://twitter.com/bozdoz/
Plugin URI: http://wordpress.org/plugins/leaflet-map/
Plugin Name: Leaflet Map
Description: A plugin for creating a Leaflet JS map with a shortcode.
Version: 1.15
License: GPL2
*/
if (!class_exists('Leaflet_Map_Plugin')) {
class Leaflet_Map_Plugin {
public static $geocoders = array(
'google' => 'Google Maps',
'osm' => 'OpenStreetMap Nominatim',
'dawa' => 'Danmarks Adressers'
);
public static $defaults = array (
'text' => array(
'leaflet_map_tile_url' => '//otile{s}-s.mqcdn.com/tiles/1.0.0/map/{z}/{x}/{y}.jpg',
'leaflet_map_tile_url_subdomains' => '1234',
'leaflet_js_url' => '//cdnjs.cloudflare.com/ajax/libs/leaflet/0.7.3/leaflet.js',
'leaflet_css_url' => '//cdnjs.cloudflare.com/ajax/libs/leaflet/0.7.3/leaflet.css',
'leaflet_default_zoom' => '16',
'leaflet_default_height' => '250',
'leaflet_default_width' => '100%',
),
'textarea' => array(
'leaflet_default_attribution' => 'Tiles Courtesy of <a href="http://www.mapquest.com/" target="_blank">MapQuest</a> <img src="http://developer.mapquest.com/content/osm/mq_logo.png" />; © <a href="http://www.openstreetmap.org/">OpenStreetMap</a> contributors',
),
'select' => array(
'leaflet_geocoder' => 'google',
),
'checkbox' => array(
'leaflet_show_attribution' => '1',
'leaflet_show_zoom_controls' => '0',
'leaflet_scroll_wheel_zoom' => '0',
),
'serialized' => array(
'leaflet_geocoded_locations' => array()
)
);
public static $helptext = array(
'leaflet_map_tile_url' => 'See some example tile URLs at <a href="http://developer.mapquest.com/web/products/open/map" target="_blank">MapQuest</a>. Can be set per map with shortcode attribute <br/> <code>[leaflet-map tileurl="http://otile{s}.mqcdn.com/tiles/1.0.0/map/{z}/{x}/{y}.jpg"]</code>',
'leaflet_map_tile_url_subdomains' => 'Some maps get tiles from multiple servers with subdomains such as a,b,c,d or 1,2,3,4; can be set per map with the shortcode <br/> <code>[leaflet-map subdomains="1234"]</code>',
'leaflet_js_url' => 'If you host your own Leaflet files, specify the URL here.',
'leaflet_css_url' => 'Same as above.',
'leaflet_default_zoom' => 'Can set per map in shortcode or adjust for all maps here; e.g. <br /> <code>[leaflet-map zoom="5"]</code>',
'leaflet_default_height' => 'Can set per map in shortcode or adjust for all maps here. Values can include "px" but it is not necessary. Can also be %; e.g. <br/> <code>[leaflet-map height="250"]</code>',
'leaflet_default_width' => 'Can set per map in shortcode or adjust for all maps here. Values can include "px" but it is not necessary. Can also be %; e.g. <br/> <code>[leaflet-map height="250"]</code>',
'leaflet_show_attribution' => 'The default URL requires attribution by its terms of use. If you want to change the URL, you may remove the attribution. Also, you can set this per map in the shortcode (1 for enabled and 0 for disabled): <br/> <code>[leaflet-map show_attr="1"]</code>',
'leaflet_show_zoom_controls' => 'The zoom buttons can be large and annoying. Enabled or disable per map in shortcode: <br/> <code>[leaflet-map zoomcontrol="0"]</code>',
'leaflet_scroll_wheel_zoom' => 'Disable zoom with mouse scroll wheel. Sometimes someone wants to scroll down the page, and not zoom the map. Enabled or disable per map in shortcode: <br/> <code>[leaflet-map scrollwheel="0"]</code>',
'leaflet_default_attribution' => 'Attribution to a custom tile url. Use semi-colons (;) to separate multiple.',
'leaflet_geocoder' => 'Select the Geocoding provider to use to retrieve addresses defined in shortcode.',
);
public function __construct() {
add_action('admin_init', array(&$this, 'admin_init'));
add_action('admin_menu', array(&$this, 'admin_menu'));
add_action( 'wp_enqueue_scripts', array(&$this, 'enqueue_and_register') );
add_action( 'admin_enqueue_scripts', array(&$this, 'enqueue_and_register') );
add_shortcode('leaflet-map', array(&$this, 'map_shortcode'));
add_shortcode('leaflet-marker', array(&$this, 'marker_shortcode'));
add_shortcode('leaflet-line', array(&$this, 'line_shortcode'));
add_shortcode('leaflet-image', array(&$this, 'image_shortcode'));
/* allow maps on excerpts */
/* should be optional? */
add_filter('the_excerpt', 'do_shortcode');
}
public static function activate () {
/* set default values to db */
foreach(self::$defaults as $arrs) {
foreach($arrs as $k=>$v) {
add_option($k, $v);
}
}
}
public static function uninstall () {
/* remove geocoded locations */
$locations = get_option('leaflet_geocoded_locations', array());
foreach ($locations as $address => $latlng) {
delete_option('leaflet_' + $address);
}
/* remove values from db */
foreach (self::$defaults as $arrs) {
foreach($arrs as $k=>$v) {
delete_option($k);
}
}
}
public function enqueue_and_register () {
$defaults = $this::$defaults['text'];
/* backwards compatible */
$version = get_option('leaflet_js_version', '');
/* defaults from db */
$js_url = get_option('leaflet_js_url', $defaults['leaflet_js_url']);
$css_url = get_option('leaflet_css_url', $defaults['leaflet_css_url']);
$js_url = sprintf($js_url, $version);
$css_url = sprintf($css_url, $version);
wp_register_style('leaflet_stylesheet', $css_url, Array(), $version, false);
wp_register_script('leaflet_js', $js_url, Array(), $version, true);
/* run an init function because other wordpress plugins don't play well with their window.onload functions */
wp_register_script('leaflet_map_init', plugins_url('scripts/init-leaflet-map.js', __FILE__), Array('leaflet_js'), '1.0', true);
/* run a construct function in the document head for the init function to use */
wp_enqueue_script('leaflet_map_construct', plugins_url('scripts/construct-leaflet-map.js', __FILE__), Array(), '1.0', false);
}
public function admin_init () {
wp_register_style('leaflet_admin_stylesheet', plugins_url('style.css', __FILE__));
}
public function admin_menu () {
add_menu_page("Leaflet Map", "Leaflet Map", 'manage_options', "leaflet-map", array(&$this, "settings_page"), plugins_url('images/leaf.png', __FILE__));
add_submenu_page("leaflet-map", "Default Values", "Default Values", 'manage_options', "leaflet-map", array(&$this, "settings_page"));
add_submenu_page("leaflet-map", "Shortcodes", "Shortcodes", 'manage_options', "leaflet-get-shortcode", array(&$this, "shortcode_page"));
}
public function settings_page () {
wp_enqueue_style( 'leaflet_admin_stylesheet' );
include 'templates/admin.php';
}
public function shortcode_page () {
wp_enqueue_style( 'leaflet_admin_stylesheet' );
wp_enqueue_script('custom_plugin_js', plugins_url('scripts/get-shortcode.js', __FILE__), Array('leaflet_js'), false);
include 'templates/find-on-map.php';
}
public function geocoder ( $address ) {
$address = urlencode( $address );
$default = $this::$defaults['select']['leaflet_geocoder'];
$geocoder = get_option('leaflet_geocoder', $default);
$cached_address = 'leaflet_' . $geocoder . '_' . $address;
/* retrieve cached geocoded location */
if (get_option( $cached_address )) {
return get_option($cached_address);
}
$geocoding_method = $geocoder . '_geocode';
$location = (Object) $this::$geocoding_method( $address );
/* add location */
add_option($cached_address, $location);
/* add option key to locations for clean up purposes */
$locations = get_option('leaflet_geocoded_locations', array());
array_push($locations, $cached_address);
update_option('leaflet_geocoded_locations', $locations);
return $location;
}
public function google_geocode ( $address ) {
/* Google */
$geocode_url = 'https://maps.googleapis.com/maps/api/geocode/json?address=';
$geocode_url .= $address;
$json = file_get_contents($geocode_url);
$json = json_decode($json);
/* found location */
if ($json->{'status'} == 'OK') {
$location = $json->{'results'}[0]->{'geometry'}->{'location'};
return $location;
}
/* else */
return array('lat' => 0, 'lng' => 0);
}
public function osm_geocode ( $address ) {
/* OpenStreetMap Nominatim */
$osm_geocode = 'https://nominatim.openstreetmap.org/?format=json&limit=1&q=';
$geocode_url = $osm_geocode . $address;
$json = file_get_contents($geocode_url);
$json = json_decode($json);
/* found location */
if (count($json) > 0) {
$location = (Object) array(
'lat' => $json[0]->{'lat'},
'lng' => $json[0]->{'lon'},
);
return $location;
}
/* else */
return (Object) array('lat' => 0, 'lng' => 0);
}
public function dawa_geocode ( $address ) {
/* Danish Addresses Web Application */
$dawa_geocode = 'https://dawa.aws.dk/adresser?format=json&q=';
$geocode_url = $dawa_geocode . $address;
$json = file_get_contents($geocode_url);
$json = json_decode($json);
/* found location */
if ($json && $json[0]->{'status'}==1) {
$location = (Object) array(
'lat'=>$json[0]->{'adgangsadresse'}->{'adgangspunkt'}->{'koordinater'}[1],
'lng'=>$json[0]->{'adgangsadresse'}->{'adgangspunkt'}->{'koordinater'}[0]
);
return $location;
}
/* else */
return (Object) array('lat' => 0, 'lng' => 0);
}
/* count map shortcodes to allow for multiple */
public static $leaflet_map_count;
public function map_shortcode ( $atts ) {
if (!$this::$leaflet_map_count) {
$this::$leaflet_map_count = 0;
}
$this::$leaflet_map_count++;
$leaflet_map_count = $this::$leaflet_map_count;
$defaults = array_merge($this::$defaults['text'], $this::$defaults['textarea'], $this::$defaults['checkbox']);
/* defaults from db */
$default_zoom = get_option('leaflet_default_zoom', $defaults['leaflet_default_zoom']);
$default_zoom_control = get_option('leaflet_show_zoom_controls', $defaults['leaflet_show_zoom_controls']);
$default_height = get_option('leaflet_default_height', $defaults['leaflet_default_height']);
$default_width = get_option('leaflet_default_width', $defaults['leaflet_default_width']);
$default_show_attr = get_option('leaflet_show_attribution', $defaults['leaflet_show_attribution']);
$default_tileurl = get_option('leaflet_map_tile_url', $defaults['leaflet_map_tile_url']);
$default_subdomains = get_option('leaflet_map_tile_url_subdomains', $defaults['leaflet_map_tile_url_subdomains']);
$default_scrollwheel = get_option('leaflet_scroll_wheel_zoom', $defaults['leaflet_scroll_wheel_zoom']);
$default_attribution = get_option('leaflet_default_attribution', $defaults['leaflet_default_attribution']);
/* leaflet script */
wp_enqueue_style('leaflet_stylesheet');
wp_enqueue_script('leaflet_js');
wp_enqueue_script('leaflet_map_init');
if ($atts) {
extract($atts);
}
/* only really necessary $atts are the location variables */
if (!empty($address)) {
/* try geocoding */
$location = $this::geocoder( $address );
$lat = $location->{'lat'};
$lng = $location->{'lng'};
}
$lat = empty($lat) ? '44.67' : $lat;
$lng = empty($lng) ? '-63.61' : $lng;
/* check more user defined $atts against defaults */
$tileurl = empty($tileurl) ? $default_tileurl : $tileurl;
$show_attr = empty($show_attr) ? $default_show_attr : $show_attr;
$subdomains = empty($subdomains) ? $default_subdomains : $subdomains;
$zoomcontrol = empty($zoomcontrol) ? $default_zoom_control : $zoomcontrol;
$zoom = empty($zoom) ? $default_zoom : $zoom;
$scrollwheel = empty($scrollwheel) ? $default_scrollwheel : $scrollwheel;
$height = empty($height) ? $default_height : $height;
$width = empty($width) ? $default_width : $width;
$attribution = empty($attribution) ? $default_attribution : $attribution;
/* allow percent, but add px for ints */
$height .= is_numeric($height) ? 'px' : '';
$width .= is_numeric($width) ? 'px' : '';
/* should be iterated for multiple maps */
$content = '<div id="leaflet-wordpress-map-'.$leaflet_map_count.'" class="leaflet-wordpress-map" style="height:'.$height.'; width:'.$width.';"></div>';
$content .= "<script>
WPLeafletMapPlugin.add(function () {
var map,
baseURL = '{$tileurl}',
base = L.tileLayer(baseURL, {
subdomains: '{$subdomains}'
});
map = L.map('leaflet-wordpress-map-{$leaflet_map_count}',
{
layers: [base],
zoomControl: {$zoomcontrol},
scrollWheelZoom: {$scrollwheel}
}).setView([{$lat}, {$lng}], {$zoom});";
if ($show_attr) {
/* add attribution to MapQuest and OSM */
$attributions = explode(';', $attribution);
foreach ($attributions as $a) {
$content .= "
map.attributionControl.addAttribution('{$a}');";
}
}
$content .= '
WPLeafletMapPlugin.maps.push(map);
}); // end add
</script>
';
return $content;
}
public function image_shortcode ( $atts ) {
/* get map count for unique id */
if (!$this::$leaflet_map_count) {
$this::$leaflet_map_count = 0;
}
$this::$leaflet_map_count++;
$leaflet_map_count = $this::$leaflet_map_count;
$defaults = array_merge($this::$defaults['text'], $this::$defaults['checkbox']);
/* defaults from db */
$default_zoom_control = get_option('leaflet_show_zoom_controls', $defaults['leaflet_show_zoom_controls']);
$default_height = get_option('leaflet_default_height', $defaults['leaflet_default_height']);
$default_width = get_option('leaflet_default_width', $defaults['leaflet_default_width']);
$default_scrollwheel = get_option('leaflet_scroll_wheel_zoom', $defaults['leaflet_scroll_wheel_zoom']);
/* leaflet script */
wp_enqueue_style('leaflet_stylesheet');
wp_enqueue_script('leaflet_js');
wp_enqueue_script('leaflet_map_init');
if ($atts) {
extract($atts);
}
/* only required field for image map */
$source = empty($source) ? 'http://lorempixel.com/1000/1000/' : $source;
/* check more user defined $atts against defaults */
$height = empty($height) ? $default_height : $height;
$width = empty($width) ? $default_width : $width;
$zoomcontrol = empty($zoomcontrol) ? $default_zoom_control : $zoomcontrol;
$zoom = empty($zoom) ? 1 : $zoom;
$scrollwheel = empty($scrollwheel) ? $default_scrollwheel : $scrollwheel;
/* allow percent, but add px for ints */
$height .= is_numeric($height) ? 'px' : '';
$width .= is_numeric($width) ? 'px' : '';
$content = '<div id="leaflet-wordpress-image-'.$leaflet_map_count.'" class="leaflet-wordpress-map" style="height:'.$height.'; width:'.$width.';"></div>';
$content .= "<script>
WPLeafletMapPlugin.add(function () {
var map,
image_src = '$source',
img = new Image(),
zoom = $zoom;
img.onload = function() {
var center_h = img.height / (zoom * 4),
center_w = img.width / (zoom * 4);
map.setView([center_h, center_w], zoom);
L.imageOverlay( image_src, [[ center_h * 2, 0 ], [ 0, center_w * 2]] ).addTo( map );
img.is_loaded = true;
};
img.src = image_src;
map = L.map('leaflet-wordpress-image-{$leaflet_map_count}', {
maxZoom: 10,
minZoom: 1,
crs: L.CRS.Simple,
zoomControl: {$zoomcontrol},
scrollWheelZoom: {$scrollwheel}
}).setView([0, 0], zoom);
// make it known that it is an image map
map.is_image_map = true;
WPLeafletMapPlugin.maps.push( map );
WPLeafletMapPlugin.images.push( img );
}); // end add
</script>";
return $content;
}
public function marker_shortcode ( $atts, $content = null ) {
/* add to previous map */
if (!$this::$leaflet_map_count) {
return '';
}
$leaflet_map_count = $this::$leaflet_map_count;
if (!empty($atts)) extract($atts);
$draggable = empty($draggable) ? 'false' : $draggable;
$visible = empty($visible) ? false : ($visible == 'true');
if (!empty($address)) {
$location = $this::geocoder( $address );
$lat = $location->{'lat'};
$lng = $location->{'lng'};
}
/* add to user contributed lat lng */
$lat = empty($lat) ? ( empty($y) ? '0' : $y ) : $lat;
$lng = empty($lng) ? ( empty($x) ? '0' : $x ) : $lng;
$marker_script = "<script>
WPLeafletMapPlugin.add(function () {
var map_count = {$leaflet_map_count},
draggable = {$draggable},
marker = L.marker([{$lat}, {$lng}], { draggable : draggable }),
previous_map = WPLeafletMapPlugin.maps[ map_count - 1 ],
is_image = previous_map.is_image_map,
image_len = WPLeafletMapPlugin.images.length,
previous_image = WPLeafletMapPlugin.images[ image_len - 1 ],
previous_image_onload;
";
if (empty($lat) && empty($lng)) {
/* update lat lng to previous map's center */
$marker_script .= "
if ( is_image &&
!previous_image.is_loaded) {
previous_image_onload = previous_image.onload;
previous_image.onload = function () {
previous_image_onload();
marker.setLatLng( previous_map.getCenter() );
};
} else {
marker.setLatLng( previous_map.getCenter() );
}
";
}
$marker_script .= "
if (draggable) {
marker.on('dragend', function () {
var latlng = this.getLatLng(),
lat = latlng.lat,
lng = latlng.lng;
if (is_image) {
console.log('leaflet-marker y=' + lat + ' x=' + lng);
} else {
console.log('leaflet-marker lat=' + lat + ' lng=' + lng);
}
});
}
marker.addTo( previous_map );
";
$message = empty($message) ? (empty($content) ? '' : $content) : $message;
if (!empty($message)) {
$message = str_replace("\n", '', $message);
$marker_script .= "marker.bindPopup('$message')";
if ($visible) {
$marker_script .= ".openPopup()";
}
$marker_script .= ";
";
}
$marker_script .= "
WPLeafletMapPlugin.markers.push( marker );
}); // end add function
</script>";
return $marker_script;
}
public function line_shortcode ( $atts, $content = null ) {
/* add to previous map */
if (!$this::$leaflet_map_count) {
return '';
}
$leaflet_map_count = $this::$leaflet_map_count;
if (!empty($atts)) extract($atts);
$color = empty($color) ? "black" : $color;
$fitline = empty($fitline) ? 0 : $fitline;
$locations = Array();
if (!empty($addresses)) {
$addresses = preg_split('/\s?[;|\/]\s?/', $addresses);
foreach ($addresses as $address) {
if (trim($address)) {
$geocoded = $this::geocoder($address);
$locations[] = Array($geocoded->{'lat'}, $geocoded->{'lng'});
}
}
} else if (!empty($latlngs)) {
$latlngs = preg_split('/\s?[;|\/]\s?/', $latlngs);
foreach ($latlngs as $latlng) {
if (trim($latlng)) {
$locations[] = array_map('floatval', preg_split('/\s?,\s?/', $latlng));
}
}
} else if (!empty($coordinates)) {
$coordinates = preg_split('/\s?[;|\/]\s?/', $coordinates);
foreach ($coordinates as $xy) {
if (trim($xy)) {
$locations[] = array_map('floatval', preg_split('/\s?,\s?/', $xy));
}
}
}
$location_json = json_encode($locations);
$marker_script = "<script>
WPLeafletMapPlugin.add(function () {
var previous_map = WPLeafletMapPlugin.maps[ {$leaflet_map_count} - 1 ],
line = L.polyline($location_json, { color : '$color'}),
fitline = $fitline;
line.addTo( previous_map );
if (fitline) {
// zoom the map to the polyline
previous_map.fitBounds( line.getBounds() );
}
WPLeafletMapPlugin.lines.push( line );
});
</script>";
return $marker_script;
}
} /* end class */
register_activation_hook( __FILE__, array('Leaflet_Map_Plugin', 'activate'));
register_uninstall_hook( __FILE__, array('Leaflet_Map_Plugin', 'uninstall') );
$leaflet_map_plugin = new Leaflet_Map_Plugin();
}
?>