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.map-shortcode.php

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

262 lines 9.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Map Shortcode
4 *
5 * Displays map with [leaflet-map ...atts]
6 *
7 * JavaScript equivalent : L.map("id");
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.shortcode.php';
21
22 /**
23 * Leaflet_Map_Shortcode Class
24 */
25 class Leaflet_Map_Shortcode extends Leaflet_Shortcode
26 {
27 /**
28 * Instantiate class
29 */
30 public function __construct()
31 {
32 parent::__construct();
33 $this->enqueue();
34 }
35
36 /**
37 * Enqueue Scripts and Styles for Leaflet
38 *
39 * @return null
40 */
41 protected function enqueue()
42 {
43 wp_enqueue_style('leaflet_stylesheet');
44 wp_enqueue_script('wp_leaflet_map');
45
46 if (wp_script_is('leaflet_mapquest_plugin', 'registered')) {
47 // mapquest doesn't accept direct tile access as of July 11, 2016
48 wp_enqueue_script('leaflet_mapquest_plugin');
49 }
50
51 // enqueue user-defined scripts
52 do_action('leaflet_map_enqueue');
53 }
54
55 /**
56 * Merge shortcode options with default options
57 *
58 * @param array|string $atts key value pairs from shortcode
59 *
60 * @return array new atts, which is actually an array
61 */
62 protected function getAtts($atts='')
63 {
64 $atts = (array) $atts;
65 extract($atts);
66
67 $settings = Leaflet_Map_Plugin_Settings::init();
68
69 $atts['zoom'] = array_key_exists('zoom', $atts) ?
70 $zoom : $settings->get('default_zoom');
71 $atts['height'] = empty($height) ?
72 $settings->get('default_height') : $height;
73 $atts['width'] = empty($width) ? $settings->get('default_width') : $width;
74 $atts['zoomcontrol'] = array_key_exists('zoomcontrol', $atts) ?
75 $zoomcontrol : $settings->get('show_zoom_controls');
76 $atts['min_zoom'] = array_key_exists('min_zoom', $atts) ?
77 $min_zoom : $settings->get('default_min_zoom');
78 $atts['max_zoom'] = empty($max_zoom) ?
79 $settings->get('default_max_zoom') : $max_zoom;
80 $atts['scrollwheel'] = array_key_exists('scrollwheel', $atts)
81 ? $scrollwheel
82 : $settings->get('scroll_wheel_zoom');
83 $atts['doubleclickzoom'] = array_key_exists('doubleclickzoom', $atts) ?
84 $doubleclickzoom : $settings->get('double_click_zoom');
85
86 // @deprecated backwards-compatible fit_markers
87 $atts['fit_markers'] = array_key_exists('fit_markers', $atts) ?
88 $fit_markers : $settings->get('fit_markers');
89
90 // fitbounds is what it should be called @since v2.12.0
91 $atts['fitbounds'] = array_key_exists('fitbounds', $atts) ?
92 $fitbounds : $atts['fit_markers'];
93
94 /* allow percent, but add px for ints */
95 $atts['height'] .= is_numeric($atts['height']) ? 'px' : '';
96 $atts['width'] .= is_numeric($atts['width']) ? 'px' : '';
97
98 // maxbounds as string: maxbounds="50, -114; 52, -112"
99 $maxBounds = isset($maxbounds) ? $maxbounds : null;
100
101 if ($maxBounds) {
102 try {
103 // explode by semi-colons and commas
104 $maxBounds = preg_split("[;|,]", $maxBounds);
105 $maxBounds = array(
106 array(
107 $maxBounds[0], $maxBounds[1]
108 ),
109 array(
110 $maxBounds[2], $maxBounds[3]
111 )
112 );
113 } catch (Exception $e) {
114 $maxBounds = null;
115 }
116 }
117
118 /*
119 need to allow 0 or empty for removal of attribution
120 */
121 if (!array_key_exists('attribution', $atts)) {
122 $atts['attribution'] = $settings->get('default_attribution');
123 }
124
125 /* allow a bunch of other options */
126 // http://leafletjs.com/reference-1.0.3.html#map-closepopuponclick
127 $more_options = array(
128 'closePopupOnClick' => isset($closepopuponclick) ?
129 $closepopuponclick : null,
130 'trackResize' => isset($trackresize) ? $trackresize : null,
131 'boxZoom' => isset($boxzoom)
132 ? $boxzoom
133 : isset($boxZoom)
134 ? $boxZoom
135 : null,
136 'touchZoom' => isset($touchZoom) ? $touchZoom : null,
137 'dragging' => isset($dragging) ? $dragging : null,
138 'keyboard' => isset($keyboard) ? $keyboard : null,
139 );
140
141 // filter out nulls
142 $more_options = $this->LM->filter_null($more_options);
143
144 // change string booleans to booleans
145 $more_options = filter_var_array($more_options, FILTER_VALIDATE_BOOLEAN);
146
147 if ($maxBounds) {
148 $more_options['maxBounds'] = $maxBounds;
149 }
150
151 // wrap as JSON
152 if ($more_options) {
153 $more_options = json_encode($more_options);
154 } else {
155 $more_options = '{}';
156 }
157
158 $atts['more_options'] = $more_options;
159
160 return $atts;
161 }
162
163 /**
164 * Get script for shortcode
165 *
166 * @param array $atts sometimes this is null
167 * @param string $content anything within a shortcode
168 *
169 * @return string HTML
170 */
171 protected function getHTML($atts='', $content=null)
172 {
173 extract($this->getAtts($atts));
174
175 if (!empty($address)) {
176 /* try geocoding */
177 include_once LEAFLET_MAP__PLUGIN_DIR . 'class.geocoder.php';
178 $location = new Leaflet_Geocoder($address);
179 $lat = $location->lat;
180 $lng = $location->lng;
181 }
182
183 $settings = Leaflet_Map_Plugin_Settings::init();
184
185 /*
186 map uses lat/lng
187 */
188 $lat = empty($lat) ? $settings->get('default_lat') : $lat;
189 $lng = empty($lng) ? $settings->get('default_lng') : $lng;
190
191 /*
192 mapquest doesn't need tile urls
193 */
194 if (wp_script_is('leaflet_mapquest_plugin', 'registered')) {
195 $tileurl = '';
196 $subdomains = '';
197 } else {
198 $tileurl = empty($tileurl) ? $settings->get('map_tile_url') : $tileurl;
199 $subdomains = empty($subdomains) ?
200 $settings->get('map_tile_url_subdomains') : $subdomains;
201 }
202
203 /* should be iterated for multiple maps */
204 ob_start(); ?>
205 <script>
206 (function () {
207 // TODO: This could/should be abstracted so we don't duplicate code in image-shortcode
208 var scriptsSoFar = document.getElementsByTagName('script');
209 var thisScript = scriptsSoFar[scriptsSoFar.length - 1];
210 // create map HTMLElement
211 var mapElement = document.createElement('div');
212 mapElement.className = 'leaflet-map';
213 mapElement.style.height = "<?php echo $height; ?>";
214 mapElement.style.width = "<?php echo $width; ?>";
215 // insert just before this script
216 thisScript.parentNode.insertBefore(mapElement, thisScript);
217 // push deferred map creation function
218 window.WPLeafletMapPlugin = window.WPLeafletMapPlugin || [];
219 window.WPLeafletMapPlugin.push(function () {
220 var baseUrl = '<?php echo $tileurl; ?>',
221 base = (!baseUrl && window.MQ) ?
222 MQ.mapLayer() : L.tileLayer(baseUrl, {
223 subdomains: '<?php echo $subdomains; ?>'
224 }),
225 options = L.Util.extend({}, {
226 maxZoom: <?php echo $max_zoom; ?>,
227 minZoom: <?php echo $min_zoom; ?>,
228 layers: [base],
229 zoomControl: <?php echo $zoomcontrol; ?>,
230 scrollWheelZoom: <?php echo $scrollwheel; ?>,
231 doubleClickZoom: <?php echo $doubleclickzoom; ?>,
232 attributionControl: false
233 }, <?php echo $more_options; ?>),
234 map = L.map(mapElement, options)
235 .setView([<?php
236 echo $lat . ',' . $lng . '],' . $zoom;
237 ?>);
238 if (<?php echo $fitbounds; ?>) {
239 map._shouldFitBounds = true;
240 }
241 <?php
242 if ($attribution) :
243 /* add any attributions, semi-colon-separated */
244 $attributions = explode(';', $attribution);
245 ?>
246 var attControl = L.control.attribution({prefix:false}).addTo(map);
247 <?php
248 foreach ($attributions as $a):
249 ?>
250 attControl.addAttribution('<?php echo trim($a); ?>');
251 <?php
252 endforeach;
253 endif;
254 ?>
255 window.WPLeafletMapPlugin.maps.push(map);
256 }); // end add
257 })(); // end IIFE
258 </script><?php
259
260 return ob_get_clean();
261 }
262 }