PluginProbe
Leaflet Map / 2.11.5
Leaflet Map v2.11.5
3.4.7 3.4.6 trunk 2.10.0 2.10.1 2.11.0 2.11.1 2.11.2 2.11.3 2.11.4 2.11.5 2.12.0 2.13.0 2.14.0 2.15.0 2.16.0 2.16.1 2.16.2 2.17.0 2.17.1 2.17.2 2.17.3 2.18.0 2.19.0 2.19.1 All 49 releases
leaflet-map / shortcodes / class.geojson-shortcode.php

class.geojson-shortcode.php in Leaflet Map 2.11.5, at shortcodes/class.geojson-shortcode.php

156 lines 5.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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://rawgit.com/bozdoz/567817310f102d169510d94306e4f464/raw/2fdb48dafafd4c8304ff051f49d9de03afb1718b/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
73 // shortcode content becomes popup text
74 $content_text = empty($content) ? '' : $content;
75 // alternatively, the popup_text attribute works as popup text
76 $popup_text = empty($popup_text) ? '' : $popup_text;
77 // choose which one takes priority (content_text)
78 $popup_text = empty($content_text) ? $popup_text : $content_text;
79
80 $popup_property = empty($popup_property) ? '' : $popup_property;
81
82 $popup_text = trim($popup_text);
83
84 ob_start();
85 ?>
86 <script>
87 window.WPLeafletMapPlugin = window.WPLeafletMapPlugin || [];
88 window.WPLeafletMapPlugin.push(function () {
89 var previous_map = window.WPLeafletMapPlugin.getCurrentMap(),
90 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 }),
104 fitbounds = <?php echo $fitbounds; ?>,
105 popup_text = window.WPLeafletMapPlugin.unescape('<?php echo $popup_text; ?>'),
106 popup_property = '<?php echo $popup_property; ?>';
107 if (fitbounds) {
108 layer.on('ready', function () {
109 this.map.fitBounds( this.getBounds() );
110 });
111 }
112 layer.addTo( previous_map );
113 function layerStyle (feature) {
114 var props = feature.properties || {};
115 var style = {};
116 var camelFun = function camelFun (_, first_letter) {
117 return first_letter.toUpperCase();
118 };
119 for (var key in props) {
120 if (key.match('-')) {
121 var camelcase = key.replace(/-(\w)/, camelFun);
122 style[ camelcase ] = props[ key ];
123 }
124 // rewrite style keys from geojson.io
125 if (rewrite_keys[ key ]) {
126 style[ rewrite_keys[ key ] ] = props[ key ];
127 }
128 }
129 style = L.Util.extend(style, default_style);
130 return style;
131 }
132 function onEachFeature (feature, layer) {
133 var props = feature.properties || {};
134 var text = popup_property && props[ popup_property ] ||
135 template(popup_text, feature.properties);
136 if (text) {
137 layer.bindPopup( text );
138 }
139 }
140 var templateRe = /\{ *([\w_-]+) *\}/g;
141 function template(str, data) {
142 return str.replace(templateRe, function (match, key) {
143 var value = data[key];
144 if (value === undefined) {
145 return match;
146 }
147 return value;
148 });
149 }
150 });
151 </script>
152 <?php
153 return ob_get_clean();
154 }
155 }
156