| 1 |
<?php |
| 2 |
/** |
| 3 |
* Marker Shortcode |
| 4 |
* |
| 5 |
* Use with [leaflet-marker ...] |
| 6 |
* |
| 7 |
* PHP Version 5.5 |
| 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 Marker Shortcode Class |
| 22 |
*/ |
| 23 |
class Leaflet_Marker_Shortcode extends Leaflet_Shortcode |
| 24 |
{ |
| 25 |
/** |
| 26 |
* Get Script for Shortcode |
| 27 |
* |
| 28 |
* @param string $atts could be an array |
| 29 |
* @param string $content optional |
| 30 |
* |
| 31 |
* @return null |
| 32 |
*/ |
| 33 |
protected function getHTML($atts='', $content=null) |
| 34 |
{ |
| 35 |
if (!empty($atts)) { |
| 36 |
extract($atts); |
| 37 |
} |
| 38 |
|
| 39 |
if (!empty($address)) { |
| 40 |
include_once LEAFLET_MAP__PLUGIN_DIR . 'class.geocoder.php'; |
| 41 |
$location = new Leaflet_Geocoder( $address ); |
| 42 |
$lat = $location->lat; |
| 43 |
$lng = $location->lng; |
| 44 |
} |
| 45 |
|
| 46 |
/* add to user contributed lat lng */ |
| 47 |
$lat = empty($lat) ? ( empty($y) ? '0' : $y ) : $lat; |
| 48 |
$lng = empty($lng) ? ( empty($x) ? '0' : $x ) : $lng; |
| 49 |
|
| 50 |
$default_marker = 'L.marker'; |
| 51 |
|
| 52 |
if (isset($svg)) { |
| 53 |
$svg = filter_var($svg, FILTER_VALIDATE_BOOLEAN); |
| 54 |
if ($svg) { |
| 55 |
wp_enqueue_script('leaflet_svg_icon_js'); |
| 56 |
$default_marker = 'new L.SVGMarker'; |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
// optional pluggable marker |
| 61 |
$default_marker = apply_filters('leaflet_map_marker', $default_marker); |
| 62 |
|
| 63 |
$options = array( |
| 64 |
'draggable' => isset($draggable) ? $draggable : null, |
| 65 |
'title' => isset($title) ? $title : null, |
| 66 |
'alt' => isset($alt) ? $alt : null, |
| 67 |
'zIndexOffset' => isset($zindexoffset) ? $zindexoffset : null, |
| 68 |
'opacity' => isset($opacity) ? $opacity : null, |
| 69 |
'iconUrl' => isset($iconurl) ? $iconurl : null, |
| 70 |
'iconSize' => isset($iconsize) ? $iconsize : null, |
| 71 |
'iconAnchor' => isset($iconanchor) ? $iconanchor : null, |
| 72 |
'shadowUrl' => isset($shadowurl) ? $shadowurl : null, |
| 73 |
'shadowSize' => isset($shadowsize) ? $shadowsize : null, |
| 74 |
'shadowAnchor' => isset($shadowanchor) ? $shadowanchor : null, |
| 75 |
'popupAnchor' => isset($popupanchor) ? $popupanchor : null, |
| 76 |
'svg' => isset($svg) ? $svg : null, |
| 77 |
'background' => isset($background) ? $background : null, |
| 78 |
'iconClass' => isset($iconclass) ? $iconclass : null, |
| 79 |
'color' => isset($color) ? $color : null |
| 80 |
); |
| 81 |
|
| 82 |
$args = array( |
| 83 |
'draggable' => FILTER_VALIDATE_BOOLEAN, |
| 84 |
'title' => FILTER_SANITIZE_STRING, |
| 85 |
'alt' => FILTER_SANITIZE_STRING, |
| 86 |
'zIndexOffset' => FILTER_VALIDATE_INT, |
| 87 |
'opacity' => FILTER_VALIDATE_FLOAT, |
| 88 |
'iconUrl' => FILTER_SANITIZE_URL, |
| 89 |
'shadowUrl' => FILTER_SANITIZE_URL, |
| 90 |
'iconSize' => array( |
| 91 |
'filter' => FILTER_SANITIZE_STRING, |
| 92 |
'flags' => FILTER_FORCE_ARRAY |
| 93 |
), |
| 94 |
'iconAnchor' => array( |
| 95 |
'filter' => FILTER_SANITIZE_STRING, |
| 96 |
'flags' => FILTER_FORCE_ARRAY |
| 97 |
), |
| 98 |
'shadowSize' => array( |
| 99 |
'filter' => FILTER_SANITIZE_STRING, |
| 100 |
'flags' => FILTER_FORCE_ARRAY |
| 101 |
), |
| 102 |
'shadowAnchor' => array( |
| 103 |
'filter' => FILTER_SANITIZE_STRING, |
| 104 |
'flags' => FILTER_FORCE_ARRAY |
| 105 |
), |
| 106 |
'popupAnchor' => array( |
| 107 |
'filter' => FILTER_SANITIZE_STRING, |
| 108 |
'flags' => FILTER_FORCE_ARRAY |
| 109 |
), |
| 110 |
'svg' => FILTER_VALIDATE_BOOLEAN, |
| 111 |
'background' => FILTER_SANITIZE_STRING, |
| 112 |
'iconClass' => FILTER_SANITIZE_STRING, |
| 113 |
'color' => FILTER_SANITIZE_STRING |
| 114 |
); |
| 115 |
|
| 116 |
$options = $this->LM->json_sanitize($options, $args); |
| 117 |
|
| 118 |
if ($options === '[]') { |
| 119 |
$options = '{}'; |
| 120 |
} |
| 121 |
ob_start(); |
| 122 |
?> |
| 123 |
<script> |
| 124 |
window.WPLeafletMapPlugin = window.WPLeafletMapPlugin || []; |
| 125 |
window.WPLeafletMapPlugin.push(function () { |
| 126 |
var marker_options = (function () { |
| 127 |
var _options = <?php echo $options; ?>; |
| 128 |
var iconArrays = [ |
| 129 |
'iconSize', |
| 130 |
'iconAnchor', |
| 131 |
'shadowSize', |
| 132 |
'shadowAnchor', |
| 133 |
'popupAnchor' |
| 134 |
]; |
| 135 |
var default_icon = L.Icon.Default.prototype.options; |
| 136 |
if (_options.iconUrl) { |
| 137 |
// arrays are strings, unfortunately... |
| 138 |
for (var i = 0, len = iconArrays.length; i < len; i++) { |
| 139 |
var option_name = iconArrays[i], |
| 140 |
option = _options[ option_name ]; |
| 141 |
// convert "1,2" to [1, 2]; |
| 142 |
if (option) { |
| 143 |
var arr = option.join('').split(','); |
| 144 |
// array.map for ie<9 |
| 145 |
for (var j = 0, lenJ = arr.length; j < lenJ; j++) { |
| 146 |
arr[j] = Number(arr[j]); |
| 147 |
} |
| 148 |
_options[ option_name ] = arr; |
| 149 |
} |
| 150 |
} |
| 151 |
// default popupAnchor |
| 152 |
if (!_options.popupAnchor) { |
| 153 |
// set (roughly) to size of icon |
| 154 |
_options.popupAnchor = (function (i_size) { |
| 155 |
// copy array |
| 156 |
i_size = i_size.slice(); |
| 157 |
|
| 158 |
// inverse coordinates |
| 159 |
i_size[0] = 0; |
| 160 |
i_size[1] *= -1; |
| 161 |
// bottom position on popup is 7px |
| 162 |
i_size[1] -= 3; |
| 163 |
return i_size; |
| 164 |
})(_options.iconSize || default_icon.iconSize); |
| 165 |
} |
| 166 |
|
| 167 |
_options.icon = new L.Icon( _options ); |
| 168 |
} |
| 169 |
return _options; |
| 170 |
})(); |
| 171 |
var draggable = marker_options.draggable; |
| 172 |
var marker = <?php echo $default_marker; ?>( |
| 173 |
[<?php echo $lat . ',' . $lng; ?>], |
| 174 |
marker_options |
| 175 |
); |
| 176 |
var map = window.WPLeafletMapPlugin.getCurrentMap(); |
| 177 |
var is_image = map.is_image_map; |
| 178 |
var group = window.WPLeafletMapPlugin.getCurrentGroup(); |
| 179 |
<?php |
| 180 |
if (empty($lat) && empty($lng)) { |
| 181 |
/* update lat lng to previous map's center */ |
| 182 |
?> |
| 183 |
if (!is_image) { |
| 184 |
marker.setLatLng( map.getCenter() ); |
| 185 |
} else { |
| 186 |
marker.setLatLng( [0, 0] ); |
| 187 |
} |
| 188 |
<?php |
| 189 |
} |
| 190 |
?> |
| 191 |
if (draggable) { |
| 192 |
marker.on('dragend', function () { |
| 193 |
var latlng = this.getLatLng(), |
| 194 |
lat = latlng.lat, |
| 195 |
lng = latlng.lng; |
| 196 |
if (is_image) { |
| 197 |
console.log('leaflet-marker y=' + lat + ' x=' + lng); |
| 198 |
} else { |
| 199 |
console.log('leaflet-marker lat=' + lat + ' lng=' + lng); |
| 200 |
} |
| 201 |
}); |
| 202 |
} |
| 203 |
marker.addTo( group ); |
| 204 |
<?php |
| 205 |
$this->LM->add_popup_to_shape($atts, $content, 'marker'); |
| 206 |
?> |
| 207 |
window.WPLeafletMapPlugin.markers.push( marker ); |
| 208 |
}); // end add function |
| 209 |
</script> |
| 210 |
<?php |
| 211 |
return ob_get_clean(); |
| 212 |
} |
| 213 |
} |