| @@ -3,10 +3,8 @@ | ||
| 3 | 3 | * GeoJSON Shortcode |
| 4 | 4 | * |
| 5 | 5 | * Use with [leaflet-geojson src="..."] |
| 6 | 6 | * |
| 7 | - * PHP Version 5.5 | |
| 8 | - * | |
| 9 | 7 | * @category Shortcode |
| 10 | 8 | * @author Benjamin J DeLong <[email protected]> |
| 11 | 9 | */ |
| 12 | 10 | |
| @@ -26,9 +24,9 @@ | ||
| 26 | 24 | * Default src for geoJSON |
| 27 | 25 | * |
| 28 | 26 | * @var string $default_src |
| 29 | 27 | */ |
| 30 | - public static $default_src = 'https://gist.githubusercontent.com/bozdoz/064a7101b95a324e8852fe9381ab9a18/raw/03f4f54b13a3a7e256732760a8b679818d9d36fc/map.geojson'; | |
| 28 | + protected $default_src = 'https://gist.githubusercontent.com/bozdoz/064a7101b95a324e8852fe9381ab9a18/raw/03f4f54b13a3a7e256732760a8b679818d9d36fc/map.geojson'; | |
| 31 | 29 | |
| 32 | 30 | /** |
| 33 | 31 | * How leaflet renders the src |
| 34 | 32 | * |
| @@ -33,26 +31,22 @@ | ||
| 33 | 31 | * How leaflet renders the src |
| 34 | 32 | * |
| 35 | 33 | * @var string $type |
| 36 | 34 | */ |
| 37 | - public static $type = 'json'; | |
| 35 | + protected $type = 'json'; | |
| 38 | 36 | |
| 39 | 37 | /** |
| 40 | 38 | * Get Script for Shortcode |
| 41 | 39 | * |
| 42 | 40 | * @param string $atts could be an array |
| 43 | - * @param string $content could be an array | |
| 41 | + * @param string $content | |
| 44 | 42 | * |
| 45 | 43 | * @return string HTML |
| 46 | 44 | */ |
| 47 | 45 | protected function getHTML($atts='', $content=null) |
| 48 | 46 | { |
| 49 | - | |
| 50 | - // need to get the called class to extend above variables | |
| 51 | - $class = self::getClass(); | |
| 52 | - | |
| 53 | 47 | if ($atts) { |
| 54 | - extract($atts); | |
| 48 | + extract($atts, EXTR_SKIP); | |
| 55 | 49 | } |
| 56 | 50 | |
| 57 | 51 | wp_enqueue_script('leaflet_ajax_geojson_js'); |
| 58 | 52 | |
| @@ -62,15 +56,17 @@ | ||
| 62 | 56 | } |
| 63 | 57 | |
| 64 | 58 | /* only required field for geojson; accept either src or source */ |
| 65 | 59 | $source = empty($source) ? '' : $source; |
| 66 | - $src = empty($src) ? $class::$default_src : $src; | |
| 60 | + $src = empty($src) ? $this->default_src : $src; | |
| 67 | 61 | $src = empty($source) ? $src : $source; |
| 68 | 62 | |
| 69 | 63 | $style_json = $this->LM->get_style_json($atts); |
| 70 | 64 | |
| 71 | 65 | $fitbounds = empty($fitbounds) ? 0 : $fitbounds; |
| 66 | + $fitbounds = filter_var($fitbounds, FILTER_VALIDATE_BOOLEAN); | |
| 72 | 67 | $circleMarker = empty($circleMarker) ? 0 : $circleMarker; |
| 68 | + $circleMarker = filter_var($circleMarker, FILTER_VALIDATE_BOOLEAN); | |
| 73 | 69 | |
| 74 | 70 | // shortcode content becomes popup text |
| 75 | 71 | $content_text = empty($content) ? '' : $content; |
| 76 | 72 | // alternatively, the popup_text attribute works as popup text |
| @@ -81,80 +77,118 @@ | ||
| 81 | 77 | $popup_property = empty($popup_property) ? '' : $popup_property; |
| 82 | 78 | |
| 83 | 79 | $popup_text = trim($popup_text); |
| 84 | 80 | |
| 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(); | |
| 81 | + $table_view = filter_var(empty($table_view) ? 0 : $table_view, FILTER_VALIDATE_INT); | |
| 82 | + | |
| 83 | + //options of iconUrl feature | |
| 84 | + $options = array( | |
| 85 | + 'iconUrl' => isset($iconurl) ? $iconurl : null, | |
| 86 | + 'iconSize' => isset($iconsize) ? $iconsize : null, | |
| 87 | + 'iconAnchor' => isset($iconanchor) ? $iconanchor : null, | |
| 88 | + 'popupAnchor' => isset($popupanchor) ? $popupanchor : null, | |
| 89 | + 'tooltipAnchor' => isset($tooltipanchor) ? $tooltipanchor : null | |
| 90 | + ); | |
| 91 | + | |
| 92 | + $args = array( | |
| 93 | + 'iconUrl' => FILTER_SANITIZE_URL, | |
| 94 | + 'iconSize' => FILTER_SANITIZE_FULL_SPECIAL_CHARS, | |
| 95 | + 'iconAnchor' => FILTER_SANITIZE_FULL_SPECIAL_CHARS, | |
| 96 | + 'popupAnchor' => FILTER_SANITIZE_FULL_SPECIAL_CHARS, | |
| 97 | + 'tooltipAnchor' => FILTER_SANITIZE_FULL_SPECIAL_CHARS, | |
| 98 | + ); | |
| 99 | + | |
| 100 | + $options = $this->LM->json_sanitize($options, $args); | |
| 101 | + | |
| 102 | + // set the default for onEachFeature | |
| 103 | + ob_start(); | |
| 104 | + ?> | |
| 105 | +function onEachFeature (feature, layer) { | |
| 106 | + var props = feature.properties || {}; | |
| 107 | + var text; | |
| 108 | + if (table_view) { | |
| 109 | + text = window.WPLeafletMapPlugin.propsToTable(props); | |
| 110 | + } else { | |
| 111 | + text = popup_property | |
| 112 | + ? props[ popup_property ] | |
| 113 | + : window.WPLeafletMapPlugin.template( | |
| 114 | + popup_text, | |
| 115 | + feature.properties | |
| 116 | + ); | |
| 117 | + } | |
| 118 | + if (text) { | |
| 119 | + layer.bindPopup( text ); | |
| 120 | + } | |
| 121 | +} | |
| 122 | + <?php | |
| 123 | + $onEachFeature = ob_get_clean(); | |
| 124 | + | |
| 125 | + // use with: add_filter('leaflet_map_geojson_onEachFeature', 'example_callback', 10, 3); | |
| 126 | + // function takes default onEachFeature | |
| 127 | + $onEachFeature = apply_filters('leaflet_map_geojson_onEachFeature', $onEachFeature); | |
| 128 | + | |
| 129 | + // create the geojson script | |
| 130 | + ob_start(); | |
| 131 | + ?>/*<script>*/ | |
| 132 | +var src = '<?php echo esc_js(esc_url($src)); ?>'; | |
| 133 | +var default_style = <?php echo $style_json; ?>; | |
| 134 | +var rewrite_keys = { | |
| 135 | + stroke : 'color', | |
| 136 | + 'stroke-width' : 'weight', | |
| 137 | + 'stroke-opacity' : 'opacity', | |
| 138 | + fill : 'fillColor', | |
| 139 | + 'fill-opacity' : 'fillOpacity', | |
| 140 | +}; | |
| 141 | +// htmlspecialchars converts & to "&"; maybe unnecessarily, and maybe 3x | |
| 142 | +var ampersandRegex = /&(?:amp;){1,3}/g | |
| 143 | +var layer = L.ajaxGeoJson(src.replace(ampersandRegex, '&'), { | |
| 144 | + type: '<?php echo $this->type; ?>', | |
| 145 | + style : layerStyle, | |
| 146 | + onEachFeature : onEachFeature, | |
| 147 | + pointToLayer: pointToLayer | |
| 148 | +}); | |
| 149 | +var fitbounds = <?php echo $fitbounds ? '1' : '0'; ?>; | |
| 150 | +var circleMarker = <?php echo $circleMarker ? '1' : '0'; ?>; | |
| 151 | +var popup_text = window.WPLeafletMapPlugin.unescape("<?php echo esc_js( | |
| 152 | + $popup_text | |
| 153 | +); ?>"); | |
| 154 | +var popup_property = "<?php echo esc_js($popup_property); ?>"; | |
| 155 | +var table_view = <?php echo $table_view; ?>; | |
| 156 | +var group = window.WPLeafletMapPlugin.getCurrentGroup(); | |
| 157 | +var markerOptions = window.WPLeafletMapPlugin.getIconOptions(<?php echo $options; ?>); | |
| 158 | +layer.addTo( group ); | |
| 159 | +window.WPLeafletMapPlugin.geojsons.push( layer ); | |
| 160 | +if (fitbounds) { | |
| 161 | + layer.on('ready', function () { | |
| 162 | + this.map.fitBounds( this.getBounds() ); | |
| 163 | + }); | |
| 164 | +} | |
| 165 | +function layerStyle (feature) { | |
| 166 | + var props = feature.properties || {}; | |
| 167 | + var style = {}; | |
| 168 | + function camelFun (_, first_letter) { | |
| 169 | + return first_letter.toUpperCase(); | |
| 170 | + }; | |
| 171 | + for (var key in props) { | |
| 172 | + if (key.match('-')) { | |
| 173 | + var camelcase = key.replace(/-(\w)/, camelFun); | |
| 174 | + style[ camelcase ] = props[ key ]; | |
| 175 | + } | |
| 176 | + // rewrite style keys from geojson.io | |
| 177 | + if (rewrite_keys[ key ]) { | |
| 178 | + style[ rewrite_keys[ key ] ] = props[ key ]; | |
| 179 | + } | |
| 180 | + } | |
| 181 | + return L.Util.extend(style, default_style); | |
| 182 | +} | |
| 183 | +<?php echo $onEachFeature; ?> | |
| 184 | +function pointToLayer (feature, latlng) { | |
| 185 | + if (circleMarker) { | |
| 186 | + return L.circleMarker(latlng); | |
| 187 | + } | |
| 188 | + return L.marker(latlng, markerOptions); | |
| 189 | +}<?php | |
| 190 | + $script = ob_get_clean(); | |
| 191 | + | |
| 192 | + return $this->wrap_script($script, 'WPLeaflet' . $this->type .'Shortcode'); | |
| 159 | 193 | } |
| 160 | 194 | } |