PluginProbe
Leaflet Map / 2.16.2
Leaflet Map v2.16.2
3.4.7 3.4.6 trunk 2.10.0 2.10.1 2.11.0 2.11.1 2.11.2 2.11.3 2.11.4 2.11.5 2.12.0 2.13.0 2.14.0 2.15.0 2.16.0 2.16.1 2.16.2 2.17.0 2.17.1 2.17.2 2.17.3 2.18.0 2.19.0 2.19.1 All 49 releases
leaflet-map / shortcodes / class.image-shortcode.php

class.image-shortcode.php in Leaflet Map 2.16.2, at shortcodes/class.image-shortcode.php

115 lines 4.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Image Shortcode
4 *
5 * Displays map with [leaflet-image src="path/to/image.jpg"]
6 *
7 * JavaScript equivalent : L.imageOverlay('path/to/image.jpg');
8 *
9 * PHP Version 5.5
10 *
11 * @category Shortcode
12 * @author Benjamin J DeLong <ben@bozdoz.com>
13 */
14
15 // Exit if accessed directly
16 if (!defined('ABSPATH')) {
17 exit;
18 }
19
20 require_once LEAFLET_MAP__PLUGIN_DIR . 'shortcodes/class.map-shortcode.php';
21
22 /**
23 * Leaflet Image Shortcode Class
24 */
25 class Leaflet_Image_Shortcode extends Leaflet_Map_Shortcode
26 {
27 /**
28 * Get HTML for shortcode
29 *
30 * @param string $atts or Array
31 * @param string $content produced by adding atts to JavaScript
32 *
33 * @return string HTML script
34 */
35 protected function getHTML($atts='', $content=null)
36 {
37 extract($this->getAtts($atts));
38
39 /* only required field for image map (src/source) */
40 $src = empty($src) ? '' : $src;
41 $source = empty($source) ? 'https://picsum.photos/1000/1000/' : $source;
42 $source = empty($src) ? $source : $src;
43
44 ob_start();
45 ?>
46 <script>
47 (function () {
48 // TODO: This could/should be abstracted so we don't duplicate code in map-shortcode
49 var scriptsSoFar = document.getElementsByTagName('script');
50 var thisScript = scriptsSoFar[scriptsSoFar.length - 1];
51 // create map HTMLElement
52 var mapElement = document.createElement('div');
53 mapElement.className = 'leaflet-map';
54 mapElement.style.height = "<?php echo $height; ?>";
55 mapElement.style.width = "<?php echo $width; ?>";
56 // insert just before this script
57 thisScript.parentNode.insertBefore(mapElement, thisScript);
58 // push deferred map creation function
59 window.WPLeafletMapPlugin = window.WPLeafletMapPlugin || [];
60 window.WPLeafletMapPlugin.push(function () {
61 var map,
62 options = L.Util.extend({}, {
63 maxZoom: <?php echo $max_zoom; ?>,
64 minZoom: <?php echo $min_zoom; ?>,
65 zoomControl: <?php echo $zoomcontrol; ?>,
66 scrollWheelZoom: <?php echo $scrollwheel; ?>,
67 doubleClickZoom: <?php echo $doubleclickzoom; ?>,
68 attributionControl: false
69 }, <?php echo $more_options; ?>, {
70 crs: L.CRS.Simple
71 }),
72 image_src = '<?php echo $source; ?>',
73 img = new Image(),
74 zoom = <?php echo $zoom; ?>;
75 img.onload = function() {
76 var h = img.height,
77 w = img.width,
78 projected_zoom = zoom + 1,
79 southWest = map.unproject([-w, h], projected_zoom),
80 northEast = map.unproject([w, -h], projected_zoom),
81 bounds = new L.LatLngBounds(southWest, northEast);
82 L.imageOverlay( image_src, bounds ).addTo( map );
83 map.setMaxBounds(bounds);
84 };
85 img.src = image_src;
86 map = L.map(mapElement, options).setView([0, 0], zoom);
87 // make it known that it is an image map
88 map.is_image_map = true;
89 if (<?php echo $fitbounds; ?>) {
90 map._shouldFitBounds = true;
91 }
92 <?php
93 if ($attribution) {
94 /* add any attributions, semi-colon-separated */
95 $attributions = explode(';', $attribution);
96 ?>
97 var attControl = L.control.attribution({prefix:false}).addTo(map);
98 <?php
99 foreach ($attributions as $a) {
100 ?>
101 attControl.addAttribution('<?php echo trim($a); ?>');
102 <?php
103 }
104 }
105 ?>
106 window.WPLeafletMapPlugin.maps.push( map );
107 window.WPLeafletMapPlugin.images.push( img );
108 }); // end add
109 })(); // end IIFE
110 </script>
111 <?php
112 return ob_get_clean();
113 }
114 }
115