| 1 |
<?php |
| 2 |
/** |
| 3 |
* GeoJSON Shortcode |
| 4 |
* |
| 5 |
* Use with [leaflet-geojson src="..."] |
| 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 |
* GeoJSON Shortcode Class |
| 22 |
*/ |
| 23 |
class Leaflet_Geojson_Shortcode extends Leaflet_Shortcode |
| 24 |
{ |
| 25 |
/** |
| 26 |
* Default src for geoJSON |
| 27 |
* |
| 28 |
* @var string $default_src |
| 29 |
*/ |
| 30 |
public static $default_src = 'https://gist.githubusercontent.com/bozdoz/064a7101b95a324e8852fe9381ab9a18/raw/03f4f54b13a3a7e256732760a8b679818d9d36fc/map.geojson'; |
| 31 |
|
| 32 |
/** |
| 33 |
* How leaflet renders the src |
| 34 |
* |
| 35 |
* @var string $type |
| 36 |
*/ |
| 37 |
public static $type = 'json'; |
| 38 |
|
| 39 |
/** |
| 40 |
* Get Script for Shortcode |
| 41 |
* |
| 42 |
* @param string $atts could be an array |
| 43 |
* @param string $content could be an array |
| 44 |
* |
| 45 |
* @return string HTML |
| 46 |
*/ |
| 47 |
protected function getHTML($atts='', $content=null) |
| 48 |
{ |
| 49 |
|
| 50 |
// need to get the called class to extend above variables |
| 51 |
$class = self::getClass(); |
| 52 |
|
| 53 |
if ($atts) { |
| 54 |
extract($atts); |
| 55 |
} |
| 56 |
|
| 57 |
wp_enqueue_script('leaflet_ajax_geojson_js'); |
| 58 |
|
| 59 |
if ($content) { |
| 60 |
$content = str_replace(array("\r\n", "\n", "\r"), '<br>', $content); |
| 61 |
$content = htmlspecialchars($content); |
| 62 |
} |
| 63 |
|
| 64 |
/* only required field for geojson; accept either src or source */ |
| 65 |
$source = empty($source) ? '' : $source; |
| 66 |
$src = empty($src) ? $class::$default_src : $src; |
| 67 |
$src = empty($source) ? $src : $source; |
| 68 |
|
| 69 |
$style_json = $this->LM->get_style_json($atts); |
| 70 |
|
| 71 |
$fitbounds = empty($fitbounds) ? 0 : $fitbounds; |
| 72 |
$circleMarker = empty($circleMarker) ? 0 : $circleMarker; |
| 73 |
|
| 74 |
// shortcode content becomes popup text |
| 75 |
$content_text = empty($content) ? '' : $content; |
| 76 |
// alternatively, the popup_text attribute works as popup text |
| 77 |
$popup_text = empty($popup_text) ? '' : $popup_text; |
| 78 |
// choose which one takes priority (content_text) |
| 79 |
$popup_text = empty($content_text) ? $popup_text : $content_text; |
| 80 |
|
| 81 |
$popup_property = empty($popup_property) ? '' : $popup_property; |
| 82 |
|
| 83 |
$popup_text = trim($popup_text); |
| 84 |
|
| 85 |
ob_start(); |
| 86 |
?> |
| 87 |
<script> |
| 88 |
window.WPLeafletMapPlugin = window.WPLeafletMapPlugin || []; |
| 89 |
window.WPLeafletMapPlugin.push(function () { |
| 90 |
var src = '<?php echo $src; ?>', |
| 91 |
default_style = <?php echo $style_json; ?>, |
| 92 |
rewrite_keys = { |
| 93 |
stroke : 'color', |
| 94 |
'stroke-width' : 'weight', |
| 95 |
'stroke-opacity' : 'opacity', |
| 96 |
fill : 'fillColor', |
| 97 |
'fill-opacity' : 'fillOpacity', |
| 98 |
}, |
| 99 |
layer = L.ajaxGeoJson(src, { |
| 100 |
type: '<?php echo $class::$type; ?>', |
| 101 |
style : layerStyle, |
| 102 |
onEachFeature : onEachFeature, |
| 103 |
pointToLayer: pointToLayer |
| 104 |
}), |
| 105 |
fitbounds = <?php echo $fitbounds; ?>, |
| 106 |
circleMarker = <?php echo $circleMarker; ?>, |
| 107 |
popup_text = window.WPLeafletMapPlugin.unescape('<?php echo $popup_text; ?>'), |
| 108 |
popup_property = '<?php echo $popup_property; ?>', |
| 109 |
group = window.WPLeafletMapPlugin.getCurrentGroup(); |
| 110 |
layer.addTo( group ); |
| 111 |
window.WPLeafletMapPlugin.geojsons.push( layer ); |
| 112 |
if (fitbounds) { |
| 113 |
layer.on('ready', function () { |
| 114 |
this.map.fitBounds( this.getBounds() ); |
| 115 |
}); |
| 116 |
} |
| 117 |
function layerStyle (feature) { |
| 118 |
var props = feature.properties || {}; |
| 119 |
var style = {}; |
| 120 |
var camelFun = function camelFun (_, first_letter) { |
| 121 |
return first_letter.toUpperCase(); |
| 122 |
}; |
| 123 |
for (var key in props) { |
| 124 |
if (key.match('-')) { |
| 125 |
var camelcase = key.replace(/-(\w)/, camelFun); |
| 126 |
style[ camelcase ] = props[ key ]; |
| 127 |
} |
| 128 |
// rewrite style keys from geojson.io |
| 129 |
if (rewrite_keys[ key ]) { |
| 130 |
style[ rewrite_keys[ key ] ] = props[ key ]; |
| 131 |
} |
| 132 |
} |
| 133 |
style = L.Util.extend(style, default_style); |
| 134 |
return style; |
| 135 |
} |
| 136 |
function onEachFeature (feature, layer) { |
| 137 |
var props = feature.properties || {}; |
| 138 |
var text = popup_property |
| 139 |
? props[ popup_property ] |
| 140 |
: window.WPLeafletMapPlugin.template( |
| 141 |
popup_text, |
| 142 |
feature.properties |
| 143 |
); |
| 144 |
if (text) { |
| 145 |
layer.bindPopup( text ); |
| 146 |
} |
| 147 |
} |
| 148 |
function pointToLayer (feature, latlng) { |
| 149 |
if (circleMarker) { |
| 150 |
return L.circleMarker(latlng); |
| 151 |
} else { |
| 152 |
return L.marker(latlng); |
| 153 |
} |
| 154 |
} |
| 155 |
}); |
| 156 |
</script> |
| 157 |
<?php |
| 158 |
return ob_get_clean(); |
| 159 |
} |
| 160 |
} |
| 161 |
|