-
-
Notifications
You must be signed in to change notification settings - Fork 74
Expand file tree
/
Copy pathclass.image-shortcode.php
More file actions
71 lines (63 loc) · 2.06 KB
/
Copy pathclass.image-shortcode.php
File metadata and controls
71 lines (63 loc) · 2.06 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
<?php
/**
* Image Shortcode
*
* Displays map with [leaflet-image src="path/to/image.jpg"]
*
* JavaScript equivalent : L.imageOverlay('path/to/image.jpg');
*
* @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.map-shortcode.php';
/**
* Leaflet Image Shortcode Class
*/
class Leaflet_Image_Shortcode extends Leaflet_Map_Shortcode
{
/**
* Get HTML for shortcode
*
* @param string $atts or Array
* @param string $content produced by adding atts to JavaScript
*
* @return string HTML script
*/
protected function getHTML($atts='', $content=null)
{
extract($this->getAtts($atts));
$x = empty($x) ? 0 : $this->LM->filter_float($x);
$y = empty($y) ? 0 : $this->LM->filter_float($y);
/* only required field for image map (src/source) */
$src = empty($src) ? '' : $src;
$source = empty($source) ? 'https://picsum.photos/1000/1000/' : $source;
$source = empty($src) ? $source : $src;
ob_start(); ?>/*<script>*/
var options = Object.assign({}, {
attributionControl: false
}, <?php echo $map_options; ?>, {
crs: L.CRS.Simple
});
var image_src = '<?php echo htmlspecialchars($source, ENT_QUOTES); ?>';
var img = new Image();
var zoom = <?php echo $zoom; ?>;
var map = window.WPLeafletMapPlugin.createImageMap(options).setView([<?php echo $x; ?>, <?php echo $y; ?>], zoom);
img.onload = function() {
var h = img.height,
w = img.width,
projected_zoom = zoom + 1,
southWest = map.unproject([-w, h], projected_zoom),
northEast = map.unproject([w, -h], projected_zoom),
bounds = new L.LatLngBounds(southWest, northEast);
L.imageOverlay( image_src, bounds ).addTo( map );
map.setMaxBounds(bounds);
};
img.src = image_src;<?php
$script = ob_get_clean();
return $this->getDiv($height, $width) . $this->wrap_script($script, 'WPLeafletImageShortcode');
}
}