| 1 |
<?php |
| 2 |
/** |
| 3 |
* Map Shortcode |
| 4 |
* |
| 5 |
* Displays map with [leaflet-map ...atts] |
| 6 |
* |
| 7 |
* JavaScript equivalent : L.map("id"); |
| 8 |
* |
| 9 |
* @param array $atts |
| 10 |
* @return string $content produced by adding atts to JavaScript |
| 11 |
*/ |
| 12 |
|
| 13 |
// Exit if accessed directly |
| 14 |
if ( !defined( 'ABSPATH' ) ) exit; |
| 15 |
|
| 16 |
include_once(LEAFLET_MAP__PLUGIN_DIR . 'shortcodes/class.shortcode.php'); |
| 17 |
|
| 18 |
class Leaflet_Map_Shortcode extends Leaflet_Shortcode { |
| 19 |
protected $map_id = 0; |
| 20 |
|
| 21 |
public function __construct ($atts) { |
| 22 |
parent::__construct(); |
| 23 |
$this->enqueue(); |
| 24 |
$this->incrementMap(); |
| 25 |
} |
| 26 |
|
| 27 |
protected function incrementMap () { |
| 28 |
$this->LM->map_count++; |
| 29 |
$this->map_id = $this->LM->map_count; |
| 30 |
} |
| 31 |
|
| 32 |
/** |
| 33 |
* Merge shortcode options with default options |
| 34 |
* |
| 35 |
* @param array|string $atts key value pairs from shortcode |
| 36 |
* @param string $content inner text in shortcode |
| 37 |
* @return array new atts, which is actually an array |
| 38 |
*/ |
| 39 |
|
| 40 |
protected function getAtts ($atts='', $content=null) { |
| 41 |
$atts = (array) $atts; |
| 42 |
extract($atts); |
| 43 |
|
| 44 |
$settings = Leaflet_Map_Plugin_Settings::init(); |
| 45 |
|
| 46 |
$atts['zoom'] = array_key_exists('zoom', $atts) ? $zoom : $settings->get('default_zoom'); |
| 47 |
$atts['height'] = empty($height) ? $settings->get('default_height') : $height; |
| 48 |
$atts['width'] = empty($width) ? $settings->get('default_width') : $width; |
| 49 |
$atts['zoomcontrol'] = array_key_exists('zoomcontrol', $atts) ? $zoomcontrol : $settings->get('show_zoom_controls'); |
| 50 |
$atts['min_zoom'] = array_key_exists('min_zoom', $atts) ? $min_zoom : $settings->get('default_min_zoom'); |
| 51 |
$atts['max_zoom'] = empty($max_zoom) ? $settings->get('default_max_zoom') : $max_zoom; |
| 52 |
$atts['scrollwheel'] = array_key_exists('scrollwheel', $atts) ? $scrollwheel : $settings->get('scroll_wheel_zoom'); |
| 53 |
$atts['doubleclickzoom'] = array_key_exists('doubleclickzoom', $atts) ? $doubleclickzoom : $settings->get('double_click_zoom'); |
| 54 |
$atts['fit_markers'] = array_key_exists('fit_markers', $atts) ? $fit_markers : $settings->get('fit_markers'); |
| 55 |
|
| 56 |
/* allow percent, but add px for ints */ |
| 57 |
$atts['height'] .= is_numeric($atts['height']) ? 'px' : ''; |
| 58 |
$atts['width'] .= is_numeric($atts['width']) ? 'px' : ''; |
| 59 |
|
| 60 |
/* |
| 61 |
need to allow 0 or empty for removal of attribution |
| 62 |
*/ |
| 63 |
if (!array_key_exists('attribution', $atts)) { |
| 64 |
$atts['attribution'] = $settings->get('default_attribution'); |
| 65 |
} |
| 66 |
|
| 67 |
/* allow a bunch of other options */ |
| 68 |
// http://leafletjs.com/reference-1.0.3.html#map-closepopuponclick |
| 69 |
$more_options = array( |
| 70 |
'closePopupOnClick' => isset($closepopuponclick) ? $closepopuponclick : NULL, |
| 71 |
'trackResize' => isset($trackresize) ? $trackresize : NULL, |
| 72 |
'boxZoom' => isset($boxzoom) ? $boxzoom : NULL, |
| 73 |
'doubleClickZoom' => isset($doubleclickzoom) ? $doubleclickzoom : NULL, |
| 74 |
'dragging' => isset($dragging) ? $dragging : NULL, |
| 75 |
'keyboard' => isset($keyboard) ? $keyboard : NULL |
| 76 |
); |
| 77 |
|
| 78 |
// filter out nulls |
| 79 |
$more_options = $this->LM->filter_null( $more_options ); |
| 80 |
|
| 81 |
// change string booleans to booleans |
| 82 |
$more_options = filter_var_array($more_options, FILTER_VALIDATE_BOOLEAN); |
| 83 |
|
| 84 |
// wrap as JSON |
| 85 |
if ($more_options) { |
| 86 |
$more_options = json_encode( $more_options ); |
| 87 |
} else { |
| 88 |
$more_options = '{}'; |
| 89 |
} |
| 90 |
|
| 91 |
$atts['more_options'] = $more_options; |
| 92 |
|
| 93 |
return $atts; |
| 94 |
} |
| 95 |
|
| 96 |
protected function enqueue () { |
| 97 |
wp_enqueue_style('leaflet_stylesheet'); |
| 98 |
wp_enqueue_script('leaflet_js'); |
| 99 |
wp_enqueue_script('leaflet_map_init'); |
| 100 |
|
| 101 |
if (wp_script_is('leaflet_mapquest_plugin', 'registered')) { |
| 102 |
// mapquest doesn't accept direct tile access as of July 11, 2016 |
| 103 |
wp_enqueue_script('leaflet_mapquest_plugin'); |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
protected function getHTML ($atts='', $content=null) { |
| 108 |
extract($this->getAtts($atts)); |
| 109 |
|
| 110 |
if (!empty($address)) { |
| 111 |
/* try geocoding */ |
| 112 |
include_once(LEAFLET_MAP__PLUGIN_DIR . 'class.geocoder.php'); |
| 113 |
$location = new Leaflet_Geocoder( $address ); |
| 114 |
$lat = $location->lat; |
| 115 |
$lng = $location->lng; |
| 116 |
} |
| 117 |
|
| 118 |
$settings = Leaflet_Map_Plugin_Settings::init(); |
| 119 |
|
| 120 |
/* |
| 121 |
map uses lat/lng |
| 122 |
*/ |
| 123 |
$lat = empty($lat) ? $settings->get('default_lat') : $lat; |
| 124 |
$lng = empty($lng) ? $settings->get('default_lng') : $lng; |
| 125 |
|
| 126 |
/* |
| 127 |
mapquest doesn't need tile urls |
| 128 |
*/ |
| 129 |
if (wp_script_is('leaflet_mapquest_plugin', 'registered')) { |
| 130 |
$tileurl = ''; |
| 131 |
$subdomains = ''; |
| 132 |
} else { |
| 133 |
$tileurl = empty($tileurl) ? $settings->get('map_tile_url') : $tileurl; |
| 134 |
$subdomains = empty($subdomains) ? $settings->get('map_tile_url_subdomains') : $subdomains; |
| 135 |
} |
| 136 |
|
| 137 |
/* should be iterated for multiple maps */ |
| 138 |
ob_start(); ?> |
| 139 |
<div |
| 140 |
id="leaflet-map-<?php echo $this->map_id; ?>" |
| 141 |
class="leaflet-map" |
| 142 |
style="height:<?php echo $height; ?>; width:<?php echo $width; ?>;"></div> |
| 143 |
<script> |
| 144 |
WPLeafletMapPlugin.add(function () { |
| 145 |
var baseUrl = '<?php echo $tileurl; ?>', |
| 146 |
base = (!baseUrl && window.MQ) ? MQ.mapLayer() : L.tileLayer(baseUrl, { |
| 147 |
subdomains: '<?php echo $subdomains; ?>' |
| 148 |
}), |
| 149 |
options = L.Util.extend({}, { |
| 150 |
maxZoom: <?php echo $max_zoom; ?>, |
| 151 |
minZoom: <?php echo $min_zoom; ?>, |
| 152 |
layers: [base], |
| 153 |
zoomControl: <?php echo $zoomcontrol; ?>, |
| 154 |
scrollWheelZoom: <?php echo $scrollwheel; ?>, |
| 155 |
doubleClickZoom: <?php echo $doubleclickzoom; ?>, |
| 156 |
attributionControl: false |
| 157 |
}, <?php echo $more_options; ?>), |
| 158 |
map = L.map('leaflet-map-<?php echo $this->map_id; ?>', options).setView([<?php echo $lat . ',' . $lng . '],' . $zoom; ?>); |
| 159 |
if (<?php echo $fit_markers; ?>) { |
| 160 |
map.fit_markers = true; |
| 161 |
} |
| 162 |
<?php |
| 163 |
if ($attribution) { |
| 164 |
/* add any attributions, semi-colon-separated */ |
| 165 |
$attributions = explode(';', $attribution); |
| 166 |
|
| 167 |
?> |
| 168 |
var attControl = L.control.attribution({prefix:false}).addTo(map); |
| 169 |
<?php |
| 170 |
|
| 171 |
foreach ($attributions as $a) { |
| 172 |
?> |
| 173 |
attControl.addAttribution('<?php echo trim($a); ?>'); |
| 174 |
<?php |
| 175 |
} |
| 176 |
} |
| 177 |
?> |
| 178 |
WPLeafletMapPlugin.maps.push(map); |
| 179 |
}); // end add |
| 180 |
</script> <?php |
| 181 |
|
| 182 |
return ob_get_clean(); |
| 183 |
} |
| 184 |
} |