| @@ -1,14 +1,12 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | /** |
| 3 | 3 | * Map Shortcode |
| 4 | 4 | * |
| 5 | - * Displays map with [leaflet-map ...atts] | |
| 5 | + * Displays map with [leaflet-map ...atts] | |
| 6 | 6 | * |
| 7 | 7 | * JavaScript equivalent : L.map("id"); |
| 8 | - * | |
| 9 | - * PHP Version 5.5 | |
| 10 | - * | |
| 8 | + * | |
| 11 | 9 | * @category Shortcode |
| 12 | 10 | * @author Benjamin J DeLong <ben@bozdoz.com> |
| 13 | 11 | */ |
| 14 | 12 | |
| @@ -14,9 +12,9 @@ | ||
| 14 | 12 | |
| 15 | 13 | // Exit if accessed directly |
| 16 | 14 | if (!defined('ABSPATH')) { |
| 17 | 15 | exit; |
| 18 | -} | |
| 16 | +} | |
| 19 | 17 | |
| 20 | 18 | require_once LEAFLET_MAP__PLUGIN_DIR . 'shortcodes/class.shortcode.php'; |
| 21 | 19 | |
| 22 | 20 | /** |
| @@ -29,14 +27,15 @@ | ||
| 29 | 27 | */ |
| 30 | 28 | public function __construct() |
| 31 | 29 | { |
| 32 | 30 | parent::__construct(); |
| 31 | + | |
| 33 | 32 | $this->enqueue(); |
| 34 | 33 | } |
| 35 | 34 | |
| 36 | 35 | /** |
| 37 | - * Enqueue Scripts and Styles for Leaflet | |
| 38 | - * | |
| 36 | + * Enqueue Scripts and Styles for Leaflet | |
| 37 | + * | |
| 39 | 38 | * @return null |
| 40 | 39 | */ |
| 41 | 40 | protected function enqueue() |
| 42 | 41 | { |
| @@ -48,8 +47,9 @@ | ||
| 48 | 47 | wp_enqueue_script('leaflet_mapquest_plugin'); |
| 49 | 48 | } |
| 50 | 49 | |
| 51 | 50 | // enqueue user-defined scripts |
| 51 | + // ! will fire for each map | |
| 52 | 52 | do_action('leaflet_map_enqueue'); |
| 53 | 53 | } |
| 54 | 54 | |
| 55 | 55 | /** |
| @@ -54,119 +54,207 @@ | ||
| 54 | 54 | |
| 55 | 55 | /** |
| 56 | 56 | * Merge shortcode options with default options |
| 57 | 57 | * |
| 58 | - * @param array|string $atts key value pairs from shortcode | |
| 59 | - * | |
| 58 | + * @param array|string $atts key value pairs from shortcode | |
| 59 | + * | |
| 60 | 60 | * @return array new atts, which is actually an array |
| 61 | 61 | */ |
| 62 | 62 | protected function getAtts($atts='') |
| 63 | 63 | { |
| 64 | 64 | $atts = (array) $atts; |
| 65 | - extract($atts); | |
| 65 | + extract($atts, EXTR_SKIP); | |
| 66 | 66 | |
| 67 | 67 | $settings = Leaflet_Map_Plugin_Settings::init(); |
| 68 | 68 | |
| 69 | - $atts['zoom'] = array_key_exists('zoom', $atts) ? | |
| 69 | + $atts['zoom'] = array_key_exists('zoom', $atts) ? | |
| 70 | 70 | $zoom : $settings->get('default_zoom'); |
| 71 | - $atts['height'] = empty($height) ? | |
| 71 | + $atts['height'] = empty($height) ? | |
| 72 | 72 | $settings->get('default_height') : $height; |
| 73 | 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) ? | |
| 74 | + $atts['zoomcontrol'] = isset($zoomControl) | |
| 75 | + ? $zoomControl | |
| 76 | + : (array_key_exists('zoomcontrol', $atts) | |
| 77 | + ? $zoomcontrol | |
| 78 | + : $settings->get('show_zoom_controls')); | |
| 79 | + $atts['min_zoom'] = array_key_exists('min_zoom', $atts) ? | |
| 77 | 80 | $min_zoom : $settings->get('default_min_zoom'); |
| 78 | - $atts['max_zoom'] = empty($max_zoom) ? | |
| 81 | + $atts['max_zoom'] = empty($max_zoom) ? | |
| 79 | 82 | $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 | - | |
| 83 | + $atts['scrollwheel'] = isset($scrollWheelZoom) | |
| 84 | + ? $scrollWheelZoom | |
| 85 | + : (array_key_exists('scrollwheel', $atts) | |
| 86 | + ? $scrollwheel | |
| 87 | + : $settings->get('scroll_wheel_zoom')); | |
| 88 | + $atts['doubleclickzoom'] = isset($doubleClickZoom) | |
| 89 | + ? $doubleClickZoom | |
| 90 | + : (array_key_exists('doubleclickzoom', $atts) | |
| 91 | + ? $doubleclickzoom | |
| 92 | + : $settings->get('double_click_zoom')); | |
| 93 | + | |
| 86 | 94 | // @deprecated backwards-compatible fit_markers |
| 87 | - $atts['fit_markers'] = array_key_exists('fit_markers', $atts) ? | |
| 95 | + $atts['fit_markers'] = array_key_exists('fit_markers', $atts) ? | |
| 88 | 96 | $fit_markers : $settings->get('fit_markers'); |
| 89 | 97 | |
| 90 | 98 | // fitbounds is what it should be called @since v2.12.0 |
| 91 | - $atts['fitbounds'] = array_key_exists('fitbounds', $atts) ? | |
| 99 | + $atts['fitbounds'] = array_key_exists('fitbounds', $atts) ? | |
| 92 | 100 | $fitbounds : $atts['fit_markers']; |
| 93 | 101 | |
| 94 | 102 | /* allow percent, but add px for ints */ |
| 95 | 103 | $atts['height'] .= is_numeric($atts['height']) ? 'px' : ''; |
| 96 | - $atts['width'] .= is_numeric($atts['width']) ? 'px' : ''; | |
| 104 | + $atts['width'] .= is_numeric($atts['width']) ? 'px' : ''; | |
| 97 | 105 | |
| 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 | |
| 106 | + /* | |
| 107 | + need to allow 0 or empty for removal of attribution | |
| 120 | 108 | */ |
| 121 | 109 | if (!array_key_exists('attribution', $atts)) { |
| 122 | 110 | $atts['attribution'] = $settings->get('default_attribution'); |
| 123 | 111 | } |
| 124 | 112 | |
| 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) | |
| 113 | + /* allow a bunch of other (boolean) options */ | |
| 114 | + // http://leafletjs.com/reference.html#map | |
| 115 | + $map_options = array( | |
| 116 | + 'closePopupOnClick' => isset($closePopupOnClick) | |
| 117 | + ? $closePopupOnClick | |
| 118 | + : (isset($closepopuponclick) | |
| 119 | + ? $closepopuponclick | |
| 120 | + : null), | |
| 121 | + 'trackResize' => isset($trackResize) | |
| 122 | + ? $trackResize | |
| 123 | + : (isset($trackresize) | |
| 124 | + ? $trackresize | |
| 125 | + : null), | |
| 126 | + 'boxZoom' => isset($boxzoom) | |
| 127 | + ? $boxzoom | |
| 128 | + : (isset($boxZoom) | |
| 134 | 129 | ? $boxZoom |
| 135 | - : null, | |
| 130 | + : null), | |
| 136 | 131 | 'touchZoom' => isset($touchZoom) ? $touchZoom : null, |
| 137 | 132 | 'dragging' => isset($dragging) ? $dragging : null, |
| 138 | 133 | 'keyboard' => isset($keyboard) ? $keyboard : null, |
| 134 | + 'zoomAnimation' => isset($zoomAnimation) ? $zoomAnimation : null, | |
| 135 | + 'fadeAnimation' => isset($fadeAnimation) ? $fadeAnimation : null, | |
| 136 | + 'markerZoomAnimation' => isset($markerZoomAnimation) ? $markerZoomAnimation : null, | |
| 137 | + 'inertia' => isset($inertia) ? $inertia : null, | |
| 138 | + 'worldCopyJump' => isset($worldCopyJump) ? $worldCopyJump : null, | |
| 139 | + 'tap' => isset($tap) ? $tap : null, | |
| 140 | + 'bounceAtZoomLimits' => isset($bounceAtZoomLimits) ? $bounceAtZoomLimits : null, | |
| 141 | + // defined above, but can be validated here | |
| 142 | + 'zoomControl' => $atts['zoomcontrol'], | |
| 143 | + 'scrollWheelZoom' => $atts['scrollwheel'], | |
| 144 | + 'doubleClickZoom' => $atts['doubleclickzoom'] | |
| 139 | 145 | ); |
| 140 | 146 | |
| 141 | 147 | // filter out nulls |
| 142 | - $more_options = $this->LM->filter_null($more_options); | |
| 143 | - | |
| 148 | + $map_options = $this->LM->filter_null($map_options); | |
| 149 | + | |
| 150 | + // custom field for moving to JavaScript | |
| 151 | + $map_options['fitBounds'] = $atts['fitbounds']; | |
| 152 | + | |
| 144 | 153 | // change string booleans to booleans |
| 145 | - $more_options = filter_var_array($more_options, FILTER_VALIDATE_BOOLEAN); | |
| 154 | + $map_options = filter_var_array($map_options, FILTER_VALIDATE_BOOLEAN); | |
| 146 | 155 | |
| 147 | - if ($maxBounds) { | |
| 148 | - $more_options['maxBounds'] = $maxBounds; | |
| 156 | + // add min/max zoom validations | |
| 157 | + $zoom_options = array( | |
| 158 | + 'minZoom' => $atts['min_zoom'], | |
| 159 | + 'maxZoom' => $atts['max_zoom'] | |
| 160 | + ); | |
| 161 | + | |
| 162 | + $zoom_options = filter_var_array($zoom_options, FILTER_VALIDATE_FLOAT); | |
| 163 | + | |
| 164 | + $map_options = array_merge( | |
| 165 | + $map_options, | |
| 166 | + $zoom_options | |
| 167 | + ); | |
| 168 | + | |
| 169 | + // update atts too | |
| 170 | + $atts['minZoom'] = $zoom_options['minZoom']; | |
| 171 | + $atts['maxZoom'] = $zoom_options['maxZoom']; | |
| 172 | + $map_options['maxBounds'] = isset($maxbounds) ? $this->LM->convert_bounds_str_to_arr($maxbounds) : null; | |
| 173 | + | |
| 174 | + // custom field for moving to javascript | |
| 175 | + // filter out any unwanted HTML tags (including img) | |
| 176 | + if ($atts['attribution'] !== 0) { | |
| 177 | + $map_options['attribution'] = wp_kses_post($atts['attribution']); | |
| 149 | 178 | } |
| 150 | 179 | |
| 151 | 180 | // wrap as JSON |
| 152 | - if ($more_options) { | |
| 153 | - $more_options = json_encode($more_options); | |
| 154 | - } else { | |
| 155 | - $more_options = '{}'; | |
| 181 | + $atts['map_options'] = json_encode($map_options); | |
| 182 | + | |
| 183 | + // get raw variables, allowing for JavaScript variables in values | |
| 184 | + $raw_map_options = array(); | |
| 185 | + foreach($map_options as $key=>$val) { | |
| 186 | + $original_value = isset($atts[$key]) ? $atts[$key] : null; | |
| 187 | + | |
| 188 | + $liquid = $this->LM->liquid($original_value); | |
| 189 | + | |
| 190 | + if ($liquid && isset($liquid['raw']) && $liquid['raw']) { | |
| 191 | + // raw leaves original value un-quoted | |
| 192 | + $raw_map_options[$key] = $liquid['original']; | |
| 193 | + } | |
| 156 | 194 | } |
| 157 | 195 | |
| 158 | - $atts['more_options'] = $more_options; | |
| 196 | + $atts['raw_map_options'] = $this->LM->rawDict($raw_map_options); | |
| 159 | 197 | |
| 198 | + $tile_layer_options = array( | |
| 199 | + 'tileSize' => empty($tilesize) ? $settings->get('tilesize') : $tilesize, | |
| 200 | + 'subdomains' => empty($subdomains) ? $settings->get('map_tile_url_subdomains') : $subdomains, | |
| 201 | + 'id' => empty($mapid) ? $settings->get('mapid') : $mapid, | |
| 202 | + 'accessToken' => empty($accesstoken) ? $settings->get('accesstoken') : $accesstoken, | |
| 203 | + 'zoomOffset' => empty($zoomoffset) ? $settings->get('zoomoffset') : $zoomoffset, | |
| 204 | + 'noWrap' => filter_var(empty($nowrap) ? $settings->get('tile_no_wrap') : $nowrap, FILTER_VALIDATE_BOOLEAN), | |
| 205 | + 'maxZoom' => $atts['maxZoom'] | |
| 206 | + ); | |
| 207 | + | |
| 208 | + | |
| 209 | + $tile_layer_options = $this->LM->filter_empty_string($tile_layer_options); | |
| 210 | + $tile_layer_options = $this->LM->filter_null($tile_layer_options); | |
| 211 | + | |
| 212 | + if (isset($tile_layer_options['subdomains']) && strpos($tile_layer_options['subdomains'], ',') !== false) { | |
| 213 | + // subdomains can be comma-separated | |
| 214 | + $tile_layer_options['subdomains'] = explode(',', $tile_layer_options['subdomains']); | |
| 215 | + } | |
| 216 | + | |
| 217 | + $atts['tile_layer_options'] = json_encode($tile_layer_options, JSON_NUMERIC_CHECK | JSON_UNESCAPED_SLASHES); | |
| 218 | + | |
| 219 | + // TODO: find a better way to do this | |
| 220 | + $validations = array( | |
| 221 | + 'detect_retina' => FILTER_VALIDATE_BOOLEAN, | |
| 222 | + 'zoom' => FILTER_VALIDATE_FLOAT, | |
| 223 | + 'fitBounds' => FILTER_VALIDATE_BOOLEAN | |
| 224 | + ); | |
| 225 | + | |
| 226 | + $atts = $this->LM->sanitize_inclusive($atts, $validations); | |
| 227 | + | |
| 160 | 228 | return $atts; |
| 161 | 229 | } |
| 162 | 230 | |
| 163 | 231 | /** |
| 232 | + * Get the div tag for the map to instantiate | |
| 233 | + * | |
| 234 | + * @param string $height | |
| 235 | + * @param string $width | |
| 236 | + * | |
| 237 | + * @return string HTML div element | |
| 238 | + */ | |
| 239 | + protected function getDiv($height, $width) { | |
| 240 | + // div does not get wrapped in script tags | |
| 241 | + ob_start(); | |
| 242 | + ?> | |
| 243 | +<div class="leaflet-map WPLeafletMap" style="height:<?php | |
| 244 | + echo esc_attr($height); | |
| 245 | +?>; width:<?php | |
| 246 | + echo esc_attr($width); | |
| 247 | +?>;"></div><?php | |
| 248 | + return ob_get_clean(); | |
| 249 | + } | |
| 250 | + | |
| 251 | + /** | |
| 164 | 252 | * Get script for shortcode |
| 165 | - * | |
| 253 | + * | |
| 166 | 254 | * @param array $atts sometimes this is null |
| 167 | 255 | * @param string $content anything within a shortcode |
| 168 | - * | |
| 256 | + * | |
| 169 | 257 | * @return string HTML |
| 170 | 258 | */ |
| 171 | 259 | protected function getHTML($atts='', $content=null) |
| 172 | 260 | { |
| @@ -181,82 +269,59 @@ | ||
| 181 | 269 | } |
| 182 | 270 | |
| 183 | 271 | $settings = Leaflet_Map_Plugin_Settings::init(); |
| 184 | 272 | |
| 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; | |
| 273 | + // map uses lat/lng | |
| 274 | + $lat = isset($lat) ? $lat : $settings->get('default_lat'); | |
| 275 | + $lng = isset($lng) ? $lng : $settings->get('default_lng'); | |
| 190 | 276 | |
| 277 | + // validate lat/lng | |
| 278 | + $lat = $this->LM->filter_float($lat); | |
| 279 | + $lng = $this->LM->filter_float($lng); | |
| 280 | + | |
| 191 | 281 | /* |
| 192 | 282 | mapquest doesn't need tile urls |
| 193 | 283 | */ |
| 194 | 284 | if (wp_script_is('leaflet_mapquest_plugin', 'registered')) { |
| 195 | 285 | $tileurl = ''; |
| 196 | - $subdomains = ''; | |
| 197 | 286 | } else { |
| 198 | 287 | $tileurl = empty($tileurl) ? $settings->get('map_tile_url') : $tileurl; |
| 199 | - $subdomains = empty($subdomains) ? | |
| 200 | - $settings->get('map_tile_url_subdomains') : $subdomains; | |
| 201 | 288 | } |
| 202 | 289 | |
| 290 | + $detect_retina = empty($detect_retina) ? $settings->get('detect_retina') : $detect_retina; | |
| 291 | + | |
| 292 | + $detect_retina = filter_var($detect_retina, FILTER_VALIDATE_BOOLEAN); | |
| 293 | + | |
| 203 | 294 | /* 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 | |
| 295 | + ob_start(); | |
| 296 | + ?>/*<script>*/ | |
| 297 | +var baseUrl = atob('<?php echo base64_encode(filter_var($tileurl, FILTER_SANITIZE_URL)); ?>'); | |
| 298 | +var base = (!baseUrl && window.MQ) ? | |
| 299 | + window.MQ.mapLayer() : L.tileLayer(baseUrl, | |
| 300 | + L.Util.extend({}, { | |
| 301 | + detectRetina: <?php echo $detect_retina ? '1' : '0'; ?>, | |
| 302 | + }, | |
| 303 | + <?php echo $tile_layer_options; ?> | |
| 304 | + ) | |
| 305 | + ); | |
| 306 | + var options = L.Util.extend({}, { | |
| 307 | + layers: [base], | |
| 308 | + attributionControl: false | |
| 309 | + }, | |
| 310 | + <?php echo $map_options; ?>, | |
| 311 | + <?php echo $raw_map_options; ?> | |
| 312 | +); | |
| 313 | +window.WPLeafletMapPlugin.createMap(options).setView(<?php | |
| 314 | + echo '[' . $lat . ',' . $lng . '],' . $zoom; | |
| 315 | +?>);<?php | |
| 259 | 316 | |
| 260 | - return ob_get_clean(); | |
| 317 | + $show_scale = isset($show_scale) ? $show_scale : $settings->get('show_scale'); | |
| 318 | + | |
| 319 | + if ($show_scale) { | |
| 320 | + echo do_shortcode('[leaflet-scale noScriptWrap]'); | |
| 321 | + } | |
| 322 | + | |
| 323 | + $script = ob_get_clean(); | |
| 324 | + | |
| 325 | + return $this->getDiv($height, $width) . $this->wrap_script($script, 'WPLeafletMapShortcode'); | |
| 261 | 326 | } |
| 262 | -} | |
| 327 | +} | |