| 1 |
<?php |
| 2 |
/** |
| 3 |
* Marker Shortcode |
| 4 |
* |
| 5 |
* Use with [leaflet-marker ...] |
| 6 |
* |
| 7 |
* @param array $atts user-input array |
| 8 |
* @param string $content user-input content (allows HTML) |
| 9 |
* @return string content for post/page |
| 10 |
*/ |
| 11 |
|
| 12 |
// Exit if accessed directly |
| 13 |
if ( !defined( 'ABSPATH' ) ) exit; |
| 14 |
|
| 15 |
include_once(LEAFLET_MAP__PLUGIN_DIR . 'shortcodes/class.shortcode.php'); |
| 16 |
|
| 17 |
class Leaflet_Marker_Shortcode extends Leaflet_Shortcode { |
| 18 |
protected function getHTML ($atts='', $content=null) { |
| 19 |
if (!empty($atts)) extract($atts); |
| 20 |
|
| 21 |
if (!empty($address)) { |
| 22 |
include_once(LEAFLET_MAP__PLUGIN_DIR . 'class.geocoder.php'); |
| 23 |
$location = new Leaflet_Geocoder( $address ); |
| 24 |
$lat = $location->lat; |
| 25 |
$lng = $location->lng; |
| 26 |
} |
| 27 |
|
| 28 |
/* add to user contributed lat lng */ |
| 29 |
$lat = empty($lat) ? ( empty($y) ? '0' : $y ) : $lat; |
| 30 |
$lng = empty($lng) ? ( empty($x) ? '0' : $x ) : $lng; |
| 31 |
|
| 32 |
$options = array( |
| 33 |
'draggable' => isset($draggable) ? $draggable : NULL, |
| 34 |
'title' => isset($title) ? $title : NULL, |
| 35 |
'alt' => isset($alt) ? $alt : NULL, |
| 36 |
'zIndexOffset' => isset($zindexoffset) ? $zindexoffset : NULL, |
| 37 |
'opacity' => isset($opacity) ? $opacity : NULL, |
| 38 |
'iconUrl' => isset($iconurl) ? $iconurl : NULL, |
| 39 |
'iconSize' => isset($iconsize) ? $iconsize : NULL, |
| 40 |
'iconAnchor' => isset($iconanchor) ? $iconanchor : NULL, |
| 41 |
'shadowUrl' => isset($shadowurl) ? $shadowurl : NULL, |
| 42 |
'shadowSize' => isset($shadowsize) ? $shadowsize : NULL, |
| 43 |
'shadowAnchor' => isset($shadowanchor) ? $shadowanchor : NULL |
| 44 |
); |
| 45 |
|
| 46 |
$args = array( |
| 47 |
'draggable' => FILTER_VALIDATE_BOOLEAN, |
| 48 |
'title' => FILTER_SANITIZE_STRING, |
| 49 |
'alt' => FILTER_SANITIZE_STRING, |
| 50 |
'zIndexOffset' => FILTER_VALIDATE_INT, |
| 51 |
'opacity' => FILTER_VALIDATE_FLOAT, |
| 52 |
'iconUrl' => FILTER_SANITIZE_URL, |
| 53 |
'shadowUrl' => FILTER_SANITIZE_URL, |
| 54 |
'iconSize' => array( |
| 55 |
'filter' => FILTER_SANITIZE_STRING, |
| 56 |
'flags' => FILTER_FORCE_ARRAY |
| 57 |
), |
| 58 |
'iconAnchor' => array( |
| 59 |
'filter' => FILTER_SANITIZE_STRING, |
| 60 |
'flags' => FILTER_FORCE_ARRAY |
| 61 |
), |
| 62 |
'shadowSize' => array( |
| 63 |
'filter' => FILTER_SANITIZE_STRING, |
| 64 |
'flags' => FILTER_FORCE_ARRAY |
| 65 |
), |
| 66 |
'shadowAnchor' => array( |
| 67 |
'filter' => FILTER_SANITIZE_STRING, |
| 68 |
'flags' => FILTER_FORCE_ARRAY |
| 69 |
) |
| 70 |
); |
| 71 |
|
| 72 |
$options = $this->LM->json_sanitize($options, $args); |
| 73 |
|
| 74 |
if ($options === '[]') { |
| 75 |
$options = '{}'; |
| 76 |
} |
| 77 |
ob_start(); |
| 78 |
?> |
| 79 |
<script> |
| 80 |
WPLeafletMapPlugin.add(function () { |
| 81 |
var marker_options = (function () { |
| 82 |
var _options = <?php echo $options; ?>, |
| 83 |
iconArrays = ['iconSize', 'iconAnchor', |
| 84 |
'shadowSize', 'shadowAnchor']; |
| 85 |
if (_options.iconUrl) { |
| 86 |
// arrays are strings, unfortunately... |
| 87 |
for (var i = 0, len = iconArrays.length; i < len; i++) { |
| 88 |
var option_name = iconArrays[i], |
| 89 |
option = _options[ option_name ]; |
| 90 |
if (option) { |
| 91 |
_options[ option_name ] = option.join('').split(','); |
| 92 |
} |
| 93 |
} |
| 94 |
_options.icon = new L.Icon( _options ); |
| 95 |
} |
| 96 |
return _options; |
| 97 |
})(), |
| 98 |
draggable = marker_options.draggable, |
| 99 |
marker = L.marker([<?php echo $lat . ',' . $lng; ?>], marker_options), |
| 100 |
previous_map = WPLeafletMapPlugin.getCurrentMap(), |
| 101 |
is_image = previous_map.is_image_map, |
| 102 |
previous_map_onload, |
| 103 |
markergroup = WPLeafletMapPlugin.getCurrentMarkerGroup(); |
| 104 |
<?php |
| 105 |
if (empty($lat) && empty($lng)) { |
| 106 |
/* update lat lng to previous map's center */ |
| 107 |
?> |
| 108 |
if (!is_image) { |
| 109 |
marker.setLatLng( previous_map.getCenter() ); |
| 110 |
} else { |
| 111 |
marker.setLatLng( [0, 0] ); |
| 112 |
} |
| 113 |
<?php |
| 114 |
} |
| 115 |
?> |
| 116 |
if (draggable) { |
| 117 |
marker.on('dragend', function () { |
| 118 |
var latlng = this.getLatLng(), |
| 119 |
lat = latlng.lat, |
| 120 |
lng = latlng.lng; |
| 121 |
if (is_image) { |
| 122 |
console.log('leaflet-marker y=' + lat + ' x=' + lng); |
| 123 |
} else { |
| 124 |
console.log('leaflet-marker lat=' + lat + ' lng=' + lng); |
| 125 |
} |
| 126 |
}); |
| 127 |
} |
| 128 |
|
| 129 |
marker.addTo( markergroup ); |
| 130 |
<?php |
| 131 |
$this->LM->add_popup_to_shape($atts, $content, 'marker'); |
| 132 |
?> |
| 133 |
WPLeafletMapPlugin.markers.push( marker ); |
| 134 |
}); // end add function |
| 135 |
</script> |
| 136 |
<?php |
| 137 |
|
| 138 |
return ob_get_clean(); |
| 139 |
} |
| 140 |
} |