PluginProbe
Leaflet Map / 2.11.2
Leaflet Map v2.11.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.11.2, at shortcodes/class.map-shortcode.php

240 lines 7.7 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 * Unique ID for a map
29 *
30 * @var int $map_id
31 */
32 protected $map_id = 0;
33
34 /**
35 * Instantiate class
36 */
37 public function __construct()
38 {
39 parent::__construct();
40 $this->enqueue();
41 $this->incrementMap();
42 }
43
44 /**
45 * Increment the map count
46 */
47 protected function incrementMap()
48 {
49 $this->LM->map_count++;
50 $this->map_id = $this->LM->map_count;
51 }
52
53 /**
54 * Merge shortcode options with default options
55 *
56 * @param array|string $atts key value pairs from shortcode
57 * @param string $content inner text in shortcode
58 *
59 * @return array new atts, which is actually an array
60 */
61 protected function getAtts($atts='', $content=null)
62 {
63 $atts = (array) $atts;
64 extract($atts);
65
66 $settings = Leaflet_Map_Plugin_Settings::init();
67
68 $atts['zoom'] = array_key_exists('zoom', $atts) ?
69 $zoom : $settings->get('default_zoom');
70 $atts['height'] = empty($height) ?
71 $settings->get('default_height') : $height;
72 $atts['width'] = empty($width) ? $settings->get('default_width') : $width;
73 $atts['zoomcontrol'] = array_key_exists('zoomcontrol', $atts) ?
74 $zoomcontrol : $settings->get('show_zoom_controls');
75 $atts['min_zoom'] = array_key_exists('min_zoom', $atts) ?
76 $min_zoom : $settings->get('default_min_zoom');
77 $atts['max_zoom'] = empty($max_zoom) ?
78 $settings->get('default_max_zoom') : $max_zoom;
79 $atts['scrollwheel'] = array_key_exists('scrollwheel', $atts) ?
80 $scrollwheel : $settings->get('scroll_wheel_zoom');
81 $atts['doubleclickzoom'] = array_key_exists('doubleclickzoom', $atts) ?
82 $doubleclickzoom : $settings->get('double_click_zoom');
83 $atts['fit_markers'] = array_key_exists('fit_markers', $atts) ?
84 $fit_markers : $settings->get('fit_markers');
85
86 /* allow percent, but add px for ints */
87 $atts['height'] .= is_numeric($atts['height']) ? 'px' : '';
88 $atts['width'] .= is_numeric($atts['width']) ? 'px' : '';
89
90 /*
91 need to allow 0 or empty for removal of attribution
92 */
93 if (!array_key_exists('attribution', $atts)) {
94 $atts['attribution'] = $settings->get('default_attribution');
95 }
96
97 /* allow a bunch of other options */
98 // http://leafletjs.com/reference-1.0.3.html#map-closepopuponclick
99 $more_options = array(
100 'closePopupOnClick' => isset($closepopuponclick) ?
101 $closepopuponclick : null,
102 'trackResize' => isset($trackresize) ? $trackresize : null,
103 'boxZoom' => isset($boxzoom) ? $boxzoom : null,
104 'dragging' => isset($dragging) ? $dragging : null,
105 'keyboard' => isset($keyboard) ? $keyboard : null
106 );
107
108 // filter out nulls
109 $more_options = $this->LM->filter_null($more_options);
110
111 // change string booleans to booleans
112 $more_options = filter_var_array($more_options, FILTER_VALIDATE_BOOLEAN);
113
114 // wrap as JSON
115 if ($more_options) {
116 $more_options = json_encode($more_options);
117 } else {
118 $more_options = '{}';
119 }
120
121 $atts['more_options'] = $more_options;
122
123 return $atts;
124 }
125
126 /**
127 * Enqueue Scripts and Styles for Leaflet
128 *
129 * @return null
130 */
131 protected function enqueue()
132 {
133 wp_enqueue_style('leaflet_stylesheet');
134 wp_enqueue_script('leaflet_js');
135 wp_enqueue_script('leaflet_map_init');
136
137 if (wp_script_is('leaflet_mapquest_plugin', 'registered')) {
138 // mapquest doesn't accept direct tile access as of July 11, 2016
139 wp_enqueue_script('leaflet_mapquest_plugin');
140 }
141
142 // enqueue user-defined scripts
143 do_action('leaflet_map_enqueue');
144 }
145
146 /**
147 * Get script for shortcode
148 *
149 * @param array $atts sometimes this is null
150 * @param string $content anything within a shortcode
151 *
152 * @return string HTML
153 */
154 protected function getHTML($atts='', $content=null)
155 {
156 extract($this->getAtts($atts));
157
158 if (!empty($address)) {
159 /* try geocoding */
160 include_once LEAFLET_MAP__PLUGIN_DIR . 'class.geocoder.php';
161 $location = new Leaflet_Geocoder($address);
162 $lat = $location->lat;
163 $lng = $location->lng;
164 }
165
166 $settings = Leaflet_Map_Plugin_Settings::init();
167
168 /*
169 map uses lat/lng
170 */
171 $lat = empty($lat) ? $settings->get('default_lat') : $lat;
172 $lng = empty($lng) ? $settings->get('default_lng') : $lng;
173
174 /*
175 mapquest doesn't need tile urls
176 */
177 if (wp_script_is('leaflet_mapquest_plugin', 'registered')) {
178 $tileurl = '';
179 $subdomains = '';
180 } else {
181 $tileurl = empty($tileurl) ? $settings->get('map_tile_url') : $tileurl;
182 $subdomains = empty($subdomains) ?
183 $settings->get('map_tile_url_subdomains') : $subdomains;
184 }
185
186 /* should be iterated for multiple maps */
187 ob_start(); ?>
188 <div
189 id="leaflet-map-<?php echo $this->map_id; ?>"
190 class="leaflet-map"
191 style="height:<?php
192 echo $height;
193 ?>; width:<?php
194 echo $width;
195 ?>;"></div>
196 <script>
197 window.WPLeafletMapPlugin = window.WPLeafletMapPlugin || [];
198 window.WPLeafletMapPlugin.push(function () {
199 var baseUrl = '<?php echo $tileurl; ?>',
200 base = (!baseUrl && window.MQ) ?
201 MQ.mapLayer() : L.tileLayer(baseUrl, {
202 subdomains: '<?php echo $subdomains; ?>'
203 }),
204 options = L.Util.extend({}, {
205 maxZoom: <?php echo $max_zoom; ?>,
206 minZoom: <?php echo $min_zoom; ?>,
207 layers: [base],
208 zoomControl: <?php echo $zoomcontrol; ?>,
209 scrollWheelZoom: <?php echo $scrollwheel; ?>,
210 doubleClickZoom: <?php echo $doubleclickzoom; ?>,
211 attributionControl: false
212 }, <?php echo $more_options; ?>),
213 map = L.map('leaflet-map-<?php echo $this->map_id; ?>', options)
214 .setView([<?php
215 echo $lat . ',' . $lng . '],' . $zoom;
216 ?>);
217 if (<?php echo $fit_markers; ?>) {
218 map.fit_markers = true;
219 }
220 <?php
221 if ($attribution) :
222 /* add any attributions, semi-colon-separated */
223 $attributions = explode(';', $attribution);
224 ?>
225 var attControl = L.control.attribution({prefix:false}).addTo(map);
226 <?php
227 foreach ($attributions as $a):
228 ?>
229 attControl.addAttribution('<?php echo trim($a); ?>');
230 <?php
231 endforeach;
232 endif;
233 ?>
234 window.WPLeafletMapPlugin.maps.push(map);
235 }); // end add
236 </script><?php
237
238 return ob_get_clean();
239 }
240 }