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