PluginProbe
Leaflet Map / 3.4.5
Leaflet Map v3.4.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.line-shortcode.php

class.line-shortcode.php in Leaflet Map 3.4.5, at shortcodes/class.line-shortcode.php

112 lines 3.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Line Shortcode
4 *
5 * Use with [leaflet-line ...]
6 *
7 * @category Shortcode
8 * @author Benjamin J DeLong <ben@bozdoz.com>
9 */
10
11 // Exit if accessed directly
12 if (!defined('ABSPATH')) {
13 exit;
14 }
15
16 require_once LEAFLET_MAP__PLUGIN_DIR . 'shortcodes/class.shortcode.php';
17
18 /**
19 * Leaflet Line Shortcode Class
20 */
21 class Leaflet_Line_Shortcode extends Leaflet_Shortcode
22 {
23 /**
24 * How leaflet renders the shape
25 *
26 * @var string $type
27 */
28 protected $type = 'line';
29
30 /**
31 * Get Script for Shortcode
32 *
33 * @param string $atts shortcode attributes
34 * @param string $content optional
35 *
36 * @return string HTML
37 */
38 protected function getHTML($atts='', $content=null)
39 {
40 if (!empty($atts)) {
41 extract($atts, EXTR_SKIP);
42 }
43
44 $style_json = $this->LM->get_style_json($atts);
45
46 $fitbounds = empty($fitbounds) ? 0 : $fitbounds;
47
48 // backwards compatible `fitline`
49 if (!empty($fitline)) {
50 $fitbounds = $fitline;
51 }
52
53 $fitbounds = filter_var($fitbounds, FILTER_VALIDATE_BOOLEAN);
54
55 $locations = Array();
56
57 if (!empty($addresses)) {
58 include_once LEAFLET_MAP__PLUGIN_DIR . 'class.geocoder.php';
59 $addresses = preg_split('/\s?[;|\/]\s?/', $addresses);
60 foreach ($addresses as $address) {
61 if (trim($address)) {
62 $location = new Leaflet_Geocoder($address);
63 $locations[] = Array($location->lat, $location->lng);
64 }
65 }
66 } else if (!empty($latlngs)) {
67 $latlngs = preg_split('/\s?[;|\/]\s?/', $latlngs);
68 foreach ($latlngs as $latlng) {
69 if (trim($latlng)) {
70 $locations[] = array_map('floatval', preg_split('/\s?,\s?/', $latlng));
71 }
72 }
73 } else if (!empty($coordinates)) {
74 $coordinates = preg_split('/\s?[;|\/]\s?/', $coordinates);
75 foreach ($coordinates as $xy) {
76 if (trim($xy)) {
77 $locations[] = array_map('floatval', preg_split('/\s?,\s?/', $xy));
78 }
79 }
80 }
81
82 $location_json = json_encode($locations);
83
84 $js_factory = 'L.polyline';
85 $collection = 'lines';
86
87 if ($this->type == 'polygon') {
88 $js_factory = 'L.polygon';
89 $locations = "[$location_json]";
90 $collection = 'polygons';
91 }
92
93 ob_start();
94 ?>/*<script>*/
95 var previous_map = window.WPLeafletMapPlugin.getCurrentMap();
96 var group = window.WPLeafletMapPlugin.getCurrentGroup();
97 var shape = <?php echo $js_factory; ?>(<?php echo $location_json; ?>, <?php echo $style_json; ?>);
98 var fitbounds = <?php echo $fitbounds ? '1' : '0'; ?>;
99 shape.addTo( group );
100 if (fitbounds) {
101 // zoom the map to the shape
102 previous_map.fitBounds( shape.getBounds() );
103 }
104 <?php
105 $this->LM->add_popup_to_shape($atts, $content, 'shape');
106 ?>
107 window.WPLeafletMapPlugin.<?php echo $collection; ?>.push( shape );<?php
108 $script = ob_get_clean();
109
110 return $this->wrap_script($script, 'WPLeafletLineShortcode');
111 }
112 }