| 1 |
<?php |
| 2 |
/** |
| 3 |
* Map Shortcode |
| 4 |
* |
| 5 |
* Displays map with [leaflet-map ...atts] |
| 6 |
* |
| 7 |
* JavaScript equivalent : L.map("id"); |
| 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_Map_Shortcode Class |
| 22 |
*/ |
| 23 |
class Leaflet_Map_Shortcode extends Leaflet_Shortcode |
| 24 |
{ |
| 25 |
/** |
| 26 |
* Instantiate class |
| 27 |
*/ |
| 28 |
public function __construct() |
| 29 |
{ |
| 30 |
parent::__construct(); |
| 31 |
|
| 32 |
$this->enqueue(); |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* Enqueue Scripts and Styles for Leaflet |
| 37 |
* |
| 38 |
* @return null |
| 39 |
*/ |
| 40 |
protected function enqueue() |
| 41 |
{ |
| 42 |
wp_enqueue_style('leaflet_stylesheet'); |
| 43 |
wp_enqueue_script('wp_leaflet_map'); |
| 44 |
|
| 45 |
if (wp_script_is('leaflet_mapquest_plugin', 'registered')) { |
| 46 |
// mapquest doesn't accept direct tile access as of July 11, 2016 |
| 47 |
wp_enqueue_script('leaflet_mapquest_plugin'); |
| 48 |
} |
| 49 |
|
| 50 |
// enqueue user-defined scripts |
| 51 |
// ! will fire for each map |
| 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, EXTR_SKIP); |
| 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'] = 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) ? |
| 80 |
$min_zoom : $settings->get('default_min_zoom'); |
| 81 |
$atts['max_zoom'] = empty($max_zoom) ? |
| 82 |
$settings->get('default_max_zoom') : $max_zoom; |
| 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 |
|
| 94 |
// @deprecated backwards-compatible fit_markers |
| 95 |
$atts['fit_markers'] = array_key_exists('fit_markers', $atts) ? |
| 96 |
$fit_markers : $settings->get('fit_markers'); |
| 97 |
|
| 98 |
// fitbounds is what it should be called @since v2.12.0 |
| 99 |
$atts['fitbounds'] = array_key_exists('fitbounds', $atts) ? |
| 100 |
$fitbounds : $atts['fit_markers']; |
| 101 |
|
| 102 |
/* allow percent, but add px for ints */ |
| 103 |
$atts['height'] .= is_numeric($atts['height']) ? 'px' : ''; |
| 104 |
$atts['width'] .= is_numeric($atts['width']) ? 'px' : ''; |
| 105 |
|
| 106 |
/* |
| 107 |
need to allow 0 or empty for removal of attribution |
| 108 |
*/ |
| 109 |
if (!array_key_exists('attribution', $atts)) { |
| 110 |
$atts['attribution'] = $settings->get('default_attribution'); |
| 111 |
} |
| 112 |
|
| 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) |
| 129 |
? $boxZoom |
| 130 |
: null), |
| 131 |
'touchZoom' => isset($touchZoom) ? $touchZoom : null, |
| 132 |
'dragging' => isset($dragging) ? $dragging : null, |
| 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'] |
| 145 |
); |
| 146 |
|
| 147 |
// filter out nulls |
| 148 |
$map_options = $this->LM->filter_null($map_options); |
| 149 |
|
| 150 |
// custom field for moving to JavaScript |
| 151 |
$map_options['fitBounds'] = $atts['fitbounds']; |
| 152 |
|
| 153 |
// change string booleans to booleans |
| 154 |
$map_options = filter_var_array($map_options, FILTER_VALIDATE_BOOLEAN); |
| 155 |
|
| 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']); |
| 178 |
} |
| 179 |
|
| 180 |
// wrap as JSON |
| 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 |
} |
| 194 |
} |
| 195 |
|
| 196 |
$atts['raw_map_options'] = $this->LM->rawDict($raw_map_options); |
| 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 |
|
| 228 |
return $atts; |
| 229 |
} |
| 230 |
|
| 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 htmlspecialchars($height); |
| 245 |
?>; width:<?php |
| 246 |
echo htmlspecialchars($width); |
| 247 |
?>;"></div><?php |
| 248 |
return ob_get_clean(); |
| 249 |
} |
| 250 |
|
| 251 |
/** |
| 252 |
* Get script for shortcode |
| 253 |
* |
| 254 |
* @param array $atts sometimes this is null |
| 255 |
* @param string $content anything within a shortcode |
| 256 |
* |
| 257 |
* @return string HTML |
| 258 |
*/ |
| 259 |
protected function getHTML($atts='', $content=null) |
| 260 |
{ |
| 261 |
extract($this->getAtts($atts)); |
| 262 |
|
| 263 |
if (!empty($address)) { |
| 264 |
/* try geocoding */ |
| 265 |
include_once LEAFLET_MAP__PLUGIN_DIR . 'class.geocoder.php'; |
| 266 |
$location = new Leaflet_Geocoder($address); |
| 267 |
$lat = $location->lat; |
| 268 |
$lng = $location->lng; |
| 269 |
} |
| 270 |
|
| 271 |
$settings = Leaflet_Map_Plugin_Settings::init(); |
| 272 |
|
| 273 |
// map uses lat/lng |
| 274 |
$lat = isset($lat) ? $lat : $settings->get('default_lat'); |
| 275 |
$lng = isset($lng) ? $lng : $settings->get('default_lng'); |
| 276 |
|
| 277 |
// validate lat/lng |
| 278 |
$lat = $this->LM->filter_float($lat); |
| 279 |
$lng = $this->LM->filter_float($lng); |
| 280 |
|
| 281 |
/* |
| 282 |
mapquest doesn't need tile urls |
| 283 |
*/ |
| 284 |
if (wp_script_is('leaflet_mapquest_plugin', 'registered')) { |
| 285 |
$tileurl = ''; |
| 286 |
} else { |
| 287 |
$tileurl = empty($tileurl) ? $settings->get('map_tile_url') : $tileurl; |
| 288 |
} |
| 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 |
|
| 294 |
/* should be iterated for multiple maps */ |
| 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 |
| 316 |
|
| 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'); |
| 326 |
} |
| 327 |
} |
| 328 |
|