| 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 |
$detect_retina = empty($detect_retina) ? $settings->get('detect_retina') : $detect_retina; |
| 203 |
} |
| 204 |
|
| 205 |
/* should be iterated for multiple maps */ |
| 206 |
ob_start(); ?> |
| 207 |
<script> |
| 208 |
(function () { |
| 209 |
// TODO: This could/should be abstracted so we don't duplicate code in image-shortcode |
| 210 |
var scriptsSoFar = document.getElementsByTagName('script'); |
| 211 |
var thisScript = scriptsSoFar[scriptsSoFar.length - 1]; |
| 212 |
// create map HTMLElement |
| 213 |
var mapElement = document.createElement('div'); |
| 214 |
mapElement.className = 'leaflet-map'; |
| 215 |
mapElement.style.height = "<?php echo $height; ?>"; |
| 216 |
mapElement.style.width = "<?php echo $width; ?>"; |
| 217 |
// insert just before this script |
| 218 |
thisScript.parentNode.insertBefore(mapElement, thisScript); |
| 219 |
// push deferred map creation function |
| 220 |
window.WPLeafletMapPlugin = window.WPLeafletMapPlugin || []; |
| 221 |
window.WPLeafletMapPlugin.push(function () { |
| 222 |
var baseUrl = '<?php echo $tileurl; ?>', |
| 223 |
base = (!baseUrl && window.MQ) ? |
| 224 |
MQ.mapLayer() : L.tileLayer(baseUrl, { |
| 225 |
subdomains: '<?php echo $subdomains; ?>', |
| 226 |
detectRetina: <?php echo $detect_retina; ?>, |
| 227 |
}), |
| 228 |
options = L.Util.extend({}, { |
| 229 |
maxZoom: <?php echo $max_zoom; ?>, |
| 230 |
minZoom: <?php echo $min_zoom; ?>, |
| 231 |
layers: [base], |
| 232 |
zoomControl: <?php echo $zoomcontrol; ?>, |
| 233 |
scrollWheelZoom: <?php echo $scrollwheel; ?>, |
| 234 |
doubleClickZoom: <?php echo $doubleclickzoom; ?>, |
| 235 |
attributionControl: false |
| 236 |
}, <?php echo $more_options; ?>), |
| 237 |
map = L.map(mapElement, options) |
| 238 |
.setView([<?php |
| 239 |
echo $lat . ',' . $lng . '],' . $zoom; |
| 240 |
?>); |
| 241 |
if (<?php echo $fitbounds; ?>) { |
| 242 |
map._shouldFitBounds = true; |
| 243 |
} |
| 244 |
<?php |
| 245 |
if ($attribution) : |
| 246 |
/* add any attributions, semi-colon-separated */ |
| 247 |
$attributions = explode(';', $attribution); |
| 248 |
?> |
| 249 |
var attControl = L.control.attribution({prefix:false}).addTo(map); |
| 250 |
<?php |
| 251 |
foreach ($attributions as $a): |
| 252 |
?> |
| 253 |
attControl.addAttribution('<?php echo trim($a); ?>'); |
| 254 |
<?php |
| 255 |
endforeach; |
| 256 |
endif; |
| 257 |
?> |
| 258 |
window.WPLeafletMapPlugin.maps.push(map); |
| 259 |
}); // end add |
| 260 |
})(); // end IIFE |
| 261 |
</script><?php |
| 262 |
|
| 263 |
return ob_get_clean(); |
| 264 |
} |
| 265 |
} |