PluginProbe
Leaflet Map / 2.17.2
Leaflet Map v2.17.2
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.17.2, at shortcodes/class.geojson-shortcode.php

161 lines 5.5 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://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