| 1 |
<?php |
| 2 |
/** |
| 3 |
* GeoJSON Shortcode |
| 4 |
* |
| 5 |
* Use with [leaflet-geojson src="..."] |
| 6 |
* |
| 7 |
* @param array $atts user-input array |
| 8 |
* @return string JavaScript |
| 9 |
*/ |
| 10 |
|
| 11 |
// Exit if accessed directly |
| 12 |
if ( !defined( 'ABSPATH' ) ) exit; |
| 13 |
|
| 14 |
include_once(LEAFLET_MAP__PLUGIN_DIR . 'shortcodes/class.shortcode.php'); |
| 15 |
|
| 16 |
class Leaflet_Geojson_Shortcode extends Leaflet_Shortcode { |
| 17 |
/** |
| 18 |
* @var string $default_src default src |
| 19 |
*/ |
| 20 |
public static $default_src = 'https://rawgit.com/bozdoz/567817310f102d169510d94306e4f464/raw/2fdb48dafafd4c8304ff051f49d9de03afb1718b/map.geojson'; |
| 21 |
|
| 22 |
/** |
| 23 |
* @var string $type how leaflet renders the src |
| 24 |
*/ |
| 25 |
public static $type = 'json'; |
| 26 |
|
| 27 |
protected function getHTML ($atts='', $content=null) { |
| 28 |
|
| 29 |
// need to get the called class to extend above variables |
| 30 |
$class = self::getClass(); |
| 31 |
|
| 32 |
if ($atts) extract($atts); |
| 33 |
|
| 34 |
wp_enqueue_script( 'leaflet_ajax_geojson_js' ); |
| 35 |
|
| 36 |
if ($content) { |
| 37 |
$content = str_replace(array("\r\n", "\n", "\r"), '<br>', $content); |
| 38 |
$content = htmlspecialchars($content); |
| 39 |
} |
| 40 |
|
| 41 |
/* only required field for geojson; accept either src or source */ |
| 42 |
$source = empty($source) ? '' : $source; |
| 43 |
$src = empty($src) ? $class::$default_src : $src; |
| 44 |
$src = empty($source) ? $src : $source; |
| 45 |
|
| 46 |
$style_json = $this->LM->get_style_json( $atts ); |
| 47 |
|
| 48 |
$fitbounds = empty($fitbounds) ? 0 : $fitbounds; |
| 49 |
|
| 50 |
// shortcode content becomes popup text |
| 51 |
$content_text = empty($content) ? '' : $content; |
| 52 |
// alternatively, the popup_text attribute works as popup text |
| 53 |
$popup_text = empty($popup_text) ? '' : $popup_text; |
| 54 |
// choose which one takes priority (content_text) |
| 55 |
$popup_text = empty($content_text) ? $popup_text : $content_text; |
| 56 |
|
| 57 |
$popup_property = empty($popup_property) ? '' : $popup_property; |
| 58 |
|
| 59 |
$popup_text = trim($popup_text); |
| 60 |
|
| 61 |
ob_start(); |
| 62 |
?> |
| 63 |
<script> |
| 64 |
WPLeafletMapPlugin.add(function () { |
| 65 |
var previous_map = WPLeafletMapPlugin.getCurrentMap(), |
| 66 |
src = '<?php echo $src; ?>', |
| 67 |
default_style = <?php echo $style_json; ?>, |
| 68 |
rewrite_keys = { |
| 69 |
fill : 'fillColor', |
| 70 |
'fill-opacity' : 'fillOpacity', |
| 71 |
stroke : 'color', |
| 72 |
'stroke-opacity' : 'opacity', |
| 73 |
'stroke-width' : 'width', |
| 74 |
}, |
| 75 |
layer = L.ajaxGeoJson(src, { |
| 76 |
type: '<?php echo $class::$type; ?>', |
| 77 |
style : layerStyle, |
| 78 |
onEachFeature : onEachFeature |
| 79 |
}), |
| 80 |
fitbounds = <?php echo $fitbounds; ?>, |
| 81 |
popup_text = WPLeafletMapPlugin.unescape('<?php echo $popup_text; ?>'), |
| 82 |
popup_property = '<?php echo $popup_property; ?>'; |
| 83 |
if (fitbounds) { |
| 84 |
layer.on('ready', function () { |
| 85 |
this.map.fitBounds( this.getBounds() ); |
| 86 |
}); |
| 87 |
} |
| 88 |
layer.addTo( previous_map ); |
| 89 |
function layerStyle (feature) { |
| 90 |
var props = feature.properties || {}, |
| 91 |
style = {}; |
| 92 |
for (var key in props) { |
| 93 |
if (key.match('-')) { |
| 94 |
var camelcase = key.replace(/-(\w)/, function (_, first_letter) { |
| 95 |
return first_letter.toUpperCase(); |
| 96 |
}); |
| 97 |
style[ camelcase ] = props[ key ]; |
| 98 |
} |
| 99 |
// rewrite style keys from geojson.io |
| 100 |
if (rewrite_keys[ key ]) { |
| 101 |
style[ rewrite_keys[ key ] ] = props[ key ]; |
| 102 |
} |
| 103 |
} |
| 104 |
style = L.Util.extend(style, default_style); |
| 105 |
return style; |
| 106 |
} |
| 107 |
function onEachFeature (feature, layer) { |
| 108 |
var props = feature.properties || {}, |
| 109 |
text = popup_property && props[ popup_property ] || template(popup_text, feature.properties); |
| 110 |
if (text) { |
| 111 |
layer.bindPopup( text ); |
| 112 |
} |
| 113 |
} |
| 114 |
var templateRe = /\{ *([\w_-]+) *\}/g; |
| 115 |
function template(str, data) { |
| 116 |
return str.replace(templateRe, function (match, key) { |
| 117 |
var value = data[key]; |
| 118 |
if (value === undefined) { |
| 119 |
return match; |
| 120 |
} |
| 121 |
return value; |
| 122 |
}); |
| 123 |
} |
| 124 |
}); |
| 125 |
</script> |
| 126 |
<?php |
| 127 |
return ob_get_clean(); |
| 128 |
} |
| 129 |
} |