PluginProbe
Leaflet Map / 2.11.5
Leaflet Map v2.11.5
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.marker-shortcode.php

class.marker-shortcode.php in Leaflet Map 2.11.5, at shortcodes/class.marker-shortcode.php

209 lines 7.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Marker Shortcode
4 *
5 * Use with [leaflet-marker ...]
6 *
7 * PHP Version 5.5
8 *
9 * @category Shortcode
10 * @author Benjamin J DeLong <ben@bozdoz.com>
11 */
12
13 // Exit if accessed directly
14 if (!defined('ABSPATH')) {
15 exit;
16 }
17
18 require_once LEAFLET_MAP__PLUGIN_DIR . 'shortcodes/class.shortcode.php';
19
20 /**
21 * Leaflet Marker Shortcode Class
22 */
23 class Leaflet_Marker_Shortcode extends Leaflet_Shortcode
24 {
25 /**
26 * Get Script for Shortcode
27 *
28 * @param string $atts could be an array
29 * @param string $content optional
30 *
31 * @return null
32 */
33 protected function getHTML($atts='', $content=null)
34 {
35 if (!empty($atts)) {
36 extract($atts);
37 }
38
39 if (!empty($address)) {
40 include_once LEAFLET_MAP__PLUGIN_DIR . 'class.geocoder.php';
41 $location = new Leaflet_Geocoder( $address );
42 $lat = $location->lat;
43 $lng = $location->lng;
44 }
45
46 /* add to user contributed lat lng */
47 $lat = empty($lat) ? ( empty($y) ? '0' : $y ) : $lat;
48 $lng = empty($lng) ? ( empty($x) ? '0' : $x ) : $lng;
49
50 $default_marker = 'L.marker';
51
52 if (isset($svg)) {
53 $svg = filter_var($svg, FILTER_VALIDATE_BOOLEAN);
54 if ($svg) {
55 wp_enqueue_script('leaflet_svg_icon_js');
56 $default_marker = 'new L.SVGMarker';
57 }
58 }
59
60 // optional pluggable marker
61 $default_marker = apply_filters('leaflet_map_marker', $default_marker);
62
63 $options = array(
64 'draggable' => isset($draggable) ? $draggable : null,
65 'title' => isset($title) ? $title : null,
66 'alt' => isset($alt) ? $alt : null,
67 'zIndexOffset' => isset($zindexoffset) ? $zindexoffset : null,
68 'opacity' => isset($opacity) ? $opacity : null,
69 'iconUrl' => isset($iconurl) ? $iconurl : null,
70 'iconSize' => isset($iconsize) ? $iconsize : null,
71 'iconAnchor' => isset($iconanchor) ? $iconanchor : null,
72 'shadowUrl' => isset($shadowurl) ? $shadowurl : null,
73 'shadowSize' => isset($shadowsize) ? $shadowsize : null,
74 'shadowAnchor' => isset($shadowanchor) ? $shadowanchor : null,
75 'popupAnchor' => isset($popupanchor) ? $popupanchor : null,
76 'svg' => isset($svg) ? $svg : null,
77 'background' => isset($background) ? $background : null,
78 'iconClass' => isset($iconclass) ? $iconclass : null,
79 'color' => isset($color) ? $color : null
80 );
81
82 $args = array(
83 'draggable' => FILTER_VALIDATE_BOOLEAN,
84 'title' => FILTER_SANITIZE_STRING,
85 'alt' => FILTER_SANITIZE_STRING,
86 'zIndexOffset' => FILTER_VALIDATE_INT,
87 'opacity' => FILTER_VALIDATE_FLOAT,
88 'iconUrl' => FILTER_SANITIZE_URL,
89 'shadowUrl' => FILTER_SANITIZE_URL,
90 'iconSize' => array(
91 'filter' => FILTER_SANITIZE_STRING,
92 'flags' => FILTER_FORCE_ARRAY
93 ),
94 'iconAnchor' => array(
95 'filter' => FILTER_SANITIZE_STRING,
96 'flags' => FILTER_FORCE_ARRAY
97 ),
98 'shadowSize' => array(
99 'filter' => FILTER_SANITIZE_STRING,
100 'flags' => FILTER_FORCE_ARRAY
101 ),
102 'shadowAnchor' => array(
103 'filter' => FILTER_SANITIZE_STRING,
104 'flags' => FILTER_FORCE_ARRAY
105 ),
106 'popupAnchor' => array(
107 'filter' => FILTER_SANITIZE_STRING,
108 'flags' => FILTER_FORCE_ARRAY
109 ),
110 'svg' => FILTER_VALIDATE_BOOLEAN,
111 'background' => FILTER_SANITIZE_STRING,
112 'iconClass' => FILTER_SANITIZE_STRING,
113 'color' => FILTER_SANITIZE_STRING
114 );
115
116 $options = $this->LM->json_sanitize($options, $args);
117
118 if ($options === '[]') {
119 $options = '{}';
120 }
121 ob_start();
122 ?>
123 <script>
124 window.WPLeafletMapPlugin = window.WPLeafletMapPlugin || [];
125 window.WPLeafletMapPlugin.push(function () {
126 var marker_options = (function () {
127 var _options = <?php echo $options; ?>;
128 var iconArrays = [
129 'iconSize',
130 'iconAnchor',
131 'shadowSize',
132 'shadowAnchor',
133 'popupAnchor'
134 ];
135 var default_icon = L.Icon.Default.prototype.options;
136 if (_options.iconUrl) {
137 // arrays are strings, unfortunately...
138 for (var i = 0, len = iconArrays.length; i < len; i++) {
139 var option_name = iconArrays[i],
140 option = _options[ option_name ];
141 // convert "1,2" to [1, 2];
142 if (option) {
143 _options[ option_name ] = option.join('').split(',');
144 }
145 }
146 // default popupAnchor
147 if (!_options.popupAnchor) {
148 // set (roughly) to size of icon
149 _options.popupAnchor = (function (i_size) {
150 // copy array
151 i_size = i_size.slice();
152
153 // inverse coordinates
154 i_size[0] = 0;
155 i_size[1] *= -1;
156 // bottom position on popup is 7px
157 i_size[1] -= 3;
158 return i_size;
159 })(_options.iconSize || default_icon.iconSize);
160 }
161
162 _options.icon = new L.Icon( _options );
163 }
164 return _options;
165 })(),
166 draggable = marker_options.draggable,
167 marker = <?php echo $default_marker; ?>(
168 [<?php echo $lat . ',' . $lng; ?>],
169 marker_options
170 ),
171 map = window.WPLeafletMapPlugin.getCurrentMap(),
172 is_image = map.is_image_map,
173 markergroup = window.WPLeafletMapPlugin.getCurrentMarkerGroup();
174 <?php
175 if (empty($lat) && empty($lng)) {
176 /* update lat lng to previous map's center */
177 ?>
178 if (!is_image) {
179 marker.setLatLng( map.getCenter() );
180 } else {
181 marker.setLatLng( [0, 0] );
182 }
183 <?php
184 }
185 ?>
186 if (draggable) {
187 marker.on('dragend', function () {
188 var latlng = this.getLatLng(),
189 lat = latlng.lat,
190 lng = latlng.lng;
191 if (is_image) {
192 console.log('leaflet-marker y=' + lat + ' x=' + lng);
193 } else {
194 console.log('leaflet-marker lat=' + lat + ' lng=' + lng);
195 }
196 });
197 }
198
199 marker.addTo( markergroup );
200 <?php
201 $this->LM->add_popup_to_shape($atts, $content, 'marker');
202 ?>
203 window.WPLeafletMapPlugin.markers.push( marker );
204 }); // end add function
205 </script>
206 <?php
207 return ob_get_clean();
208 }
209 }