PluginProbe
Leaflet Map / 2.16.2
Leaflet Map v2.16.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 / class.leaflet-map.php

class.leaflet-map.php in Leaflet Map 2.16.2, at class.leaflet-map.php

340 lines 10.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Leaflet Map Class File
4 *
5 * PHP Version 5.5
6 *
7 * @category Admin
8 * @author Benjamin J DeLong <ben@bozdoz.com>
9 */
10
11 // Exit if accessed directly
12 if (!defined('ABSPATH')) {
13 exit;
14 }
15
16 /**
17 * Leaflet Map Class
18 */
19 class Leaflet_Map
20 {
21
22 /**
23 * Leaflet version
24 *
25 * @var string major minor patch version
26 */
27 public static $leaflet_version = '1.6.0';
28
29 /**
30 * Files to include upon init
31 *
32 * @var array $_shortcodes
33 */
34 private $_shortcodes = array(
35 'leaflet-geojson' => array(
36 'file' => 'class.geojson-shortcode.php',
37 'class' => 'Leaflet_Geojson_Shortcode'
38 ),
39 'leaflet-image' => array(
40 'file' => 'class.image-shortcode.php',
41 'class' => 'Leaflet_Image_Shortcode'
42 ),
43 'leaflet-kml' => array(
44 'file' => 'class.kml-shortcode.php',
45 'class' => 'Leaflet_Kml_Shortcode'
46 ),
47 'leaflet-gpx' => array(
48 'file' => 'class.gpx-shortcode.php',
49 'class' => 'Leaflet_Gpx_Shortcode'
50 ),
51 'leaflet-line' => array(
52 'file' => 'class.line-shortcode.php',
53 'class' => 'Leaflet_Line_Shortcode'
54 ),
55 'leaflet-polygon' => array(
56 'file' => 'class.polygon-shortcode.php',
57 'class' => 'Leaflet_Polygon_Shortcode'
58 ),
59 'leaflet-circle' => array(
60 'file' => 'class.circle-shortcode.php',
61 'class' => 'Leaflet_Circle_Shortcode'
62 ),
63 'leaflet-map' => array(
64 'file' => 'class.map-shortcode.php',
65 'class' => 'Leaflet_Map_Shortcode'
66 ),
67 'leaflet-marker' => array(
68 'file' => 'class.marker-shortcode.php',
69 'class' => 'Leaflet_Marker_Shortcode'
70 )
71 );
72
73 /**
74 * Singleton Instance of Leaflet Map
75 *
76 * @var Leaflet_Map
77 **/
78 private static $instance = null;
79
80 /**
81 * Singleton init Function
82 *
83 * @static
84 */
85 public static function init() {
86 if ( !self::$instance ) {
87 self::$instance = new self;
88 }
89
90 return self::$instance;
91 }
92
93 /**
94 * Leaflet_Map Constructor
95 */
96 private function __construct() {
97 $this->init_hooks();
98 $this->add_shortcodes();
99
100 // loaded
101 do_action('leaflet_map_loaded');
102 }
103
104 /**
105 * Add actions and filters
106 */
107 private function init_hooks()
108 {
109
110 // Leaflet_Map_Plugin_Settings
111 include_once LEAFLET_MAP__PLUGIN_DIR . 'class.plugin-settings.php';
112
113 // Leaflet_Map_Admin
114 include_once LEAFLET_MAP__PLUGIN_DIR . 'class.admin.php';
115
116 // init admin
117 Leaflet_Map_Admin::init();
118
119 add_action( 'plugins_loaded', array('Leaflet_Map', 'load_text_domain' ));
120
121 add_action( 'wp_enqueue_scripts', array('Leaflet_Map', 'enqueue_and_register') );
122
123 /*
124 allows maps on excerpts
125 todo? should be optional somehow (admin setting?)
126 */
127 add_filter('the_excerpt', 'do_shortcode');
128 }
129
130 /**
131 * Includes and adds shortcodes
132 */
133 private function add_shortcodes()
134 {
135 // shortcodes
136 $shortcode_dir = LEAFLET_MAP__PLUGIN_DIR . 'shortcodes/';
137
138 foreach ($this->_shortcodes as $shortcode => $details) {
139 include_once $shortcode_dir . $details['file'];
140 add_shortcode($shortcode, array($details['class'], 'shortcode'));
141 }
142 }
143
144 /**
145 * Triggered when user uninstalls/removes plugin
146 */
147 public static function uninstall()
148 {
149 // remove settings in db
150 // think it needs to be included again (because __construct
151 // won't need to execute)
152 include_once LEAFLET_MAP__PLUGIN_DIR . 'class.plugin-settings.php';
153 $settings = Leaflet_Map_Plugin_Settings::init();
154 $settings->reset();
155
156 // remove geocoder locations in db
157 include_once LEAFLET_MAP__PLUGIN_DIR . 'class.geocoder.php';
158 Leaflet_Geocoder::remove_caches();
159 }
160
161 /**
162 * Loads Translations
163 */
164 public static function load_text_domain()
165 {
166 load_plugin_textdomain( 'leaflet-map', false, dirname( plugin_basename( LEAFLET_MAP__PLUGIN_FILE ) ) . '/languages/' );
167 }
168
169 /**
170 * Enqueue and register styles and scripts (called in __construct)
171 */
172 public static function enqueue_and_register()
173 {
174 /* defaults from db */
175 // Leaflet_Map_Plugin_Settings
176 include_once LEAFLET_MAP__PLUGIN_DIR . 'class.plugin-settings.php';
177 $settings = Leaflet_Map_Plugin_Settings::init();
178
179 $js_url = $settings->get('js_url');
180 $css_url = $settings->get('css_url');
181
182 wp_register_style('leaflet_stylesheet', $css_url, Array(), null, false);
183 wp_register_script('leaflet_js', $js_url, Array(), null, true);
184
185 // new required MapQuest javascript file
186 $tiling_service = $settings->get('default_tiling_service');
187
188 if ($tiling_service == 'mapquest') {
189 $mapquest_js_url = 'https://www.mapquestapi.com/sdk/leaflet/v2.2/mq-map.js?key=%s';
190 $mq_appkey = $settings->get('mapquest_appkey');
191 $mapquest_js_url = sprintf($mapquest_js_url, $mq_appkey);
192
193 wp_register_script('leaflet_mapquest_plugin', $mapquest_js_url, Array('leaflet_js'), '2.0', true);
194 }
195
196 // optional ajax geojson plugin
197 wp_register_script('tmcw_togeojson', $settings->get('togeojson_url'), Array('jquery'), LEAFLET_MAP__PLUGIN_VERSION, false);
198
199 if (defined('WP_DEBUG') && WP_DEBUG) {
200 $minified = '';
201 } else {
202 $minified = '.min';
203 }
204
205 wp_register_script('leaflet_ajax_geojson_js', plugins_url(sprintf('scripts/leaflet-ajax-geojson%s.js', $minified), __FILE__), Array('tmcw_togeojson', 'leaflet_js'), LEAFLET_MAP__PLUGIN_VERSION, false);
206
207 wp_register_script('leaflet_svg_icon_js', plugins_url(sprintf('scripts/leaflet-svg-icon%s.js', $minified), __FILE__), Array('leaflet_js'), LEAFLET_MAP__PLUGIN_VERSION, false);
208
209 /* run a construct function in the document head for subsequent functions to use (it is lightweight) */
210 wp_register_script('wp_leaflet_map', plugins_url(sprintf('scripts/construct-leaflet-map%s.js', $minified), __FILE__), Array('leaflet_js'), LEAFLET_MAP__PLUGIN_VERSION, false);
211 }
212
213 /**
214 * Filter for removing nulls from array
215 *
216 * @param array $arr
217 *
218 * @return array with nulls removed
219 */
220 public function filter_null($arr)
221 {
222 if (!function_exists('remove_null')) {
223 function remove_null ($var) {
224 return $var !== null;
225 }
226 }
227
228 return array_filter($arr, 'remove_null');
229 }
230
231 /**
232 * Sanitize JSON
233 *
234 * Takes options for filtering/correcting inputs for use in JavaScript
235 *
236 * @param array $arr user-input array
237 * @param array $args array with key-value definitions on how to convert values
238 * @return array corrected for JavaScript
239 */
240 public function json_sanitize($arr, $args)
241 {
242 // remove nulls
243 $arr = self::filter_null($arr);
244
245 // sanitize output
246 $args = array_intersect_key($args, $arr);
247 $arr = filter_var_array($arr, $args);
248
249 return json_encode($arr);
250 }
251
252 /**
253 * Get Style JSON for map shapes/geojson (svg or canvas)
254 *
255 * Takes atts for creating shapes on the map
256 *
257 * @param array $atts user-input array
258 *
259 * @return array corrected for JavaScript
260 */
261 public function get_style_json($atts)
262 {
263 if ($atts) {
264 extract($atts);
265 }
266
267 // from http://leafletjs.com/reference-1.0.3.html#path
268 $style = array(
269 'stroke' => isset($stroke) ? $stroke : null,
270 'color' => isset($color) ? $color : null,
271 'weight' => isset($weight) ? $weight : null,
272 'opacity' => isset($opacity) ? $opacity : null,
273 'lineCap' => isset($linecap) ? $linecap : null,
274 'lineJoin' => isset($linejoin) ? $linejoin : null,
275 'dashArray' => isset($dasharray) ? $dasharray : null,
276 'dashOffset' => isset($dashoffset) ? $dashoffset : null,
277 'fill' => isset($fill) ? $fill : null,
278 'fillColor' => isset($fillcolor) ? $fillcolor : null,
279 'fillOpacity' => isset($fillopacity) ? $fillopacity : null,
280 'fillRule' => isset($fillrule) ? $fillrule : null,
281 'className' => isset($classname) ? $classname : null,
282 'radius' => isset($radius) ? $radius : null
283 );
284
285 $args = array(
286 'stroke' => FILTER_VALIDATE_BOOLEAN,
287 'color' => FILTER_SANITIZE_STRING,
288 'weight' => FILTER_VALIDATE_FLOAT,
289 'opacity' => FILTER_VALIDATE_FLOAT,
290 'lineCap' => FILTER_SANITIZE_STRING,
291 'lineJoin' => FILTER_SANITIZE_STRING,
292 'dashArray' => FILTER_SANITIZE_STRING,
293 'dashOffset' => FILTER_SANITIZE_STRING,
294 'fill' => FILTER_VALIDATE_BOOLEAN,
295 'fillColor' => FILTER_SANITIZE_STRING,
296 'fillOpacity' => FILTER_VALIDATE_FLOAT,
297 'fillRule' => FILTER_SANITIZE_STRING,
298 'className' => FILTER_SANITIZE_STRING,
299 'radius' => FILTER_VALIDATE_FLOAT
300 );
301
302 return $this->json_sanitize($style, $args);
303 }
304
305 /**
306 * Add Popups to Shapes
307 *
308 * used by leaflet-marker, leaflet-line and leaflet-circle
309 *
310 * @param array $atts user-input array
311 * @param string $content text to display
312 * @param string $shape JavaScript variable for shape
313 *
314 * @return null
315 */
316 public function add_popup_to_shape($atts, $content, $shape)
317 {
318 if (!empty($atts)) {
319 extract($atts);
320 }
321
322 $message = empty($message) ?
323 (empty($content) ? '' : $content) : $message;
324 $message = str_replace(array("\r\n", "\n", "\r"), '<br>', $message);
325 $message = addslashes($message);
326 $message = htmlspecialchars($message);
327 $visible = empty($visible)
328 ? false
329 : filter_var($visible, FILTER_VALIDATE_BOOLEAN);
330
331 if (!empty($message)) {
332 echo "{$shape}.bindPopup(window.WPLeafletMapPlugin.unescape('{$message}'))";
333 if ($visible) {
334 echo ".openPopup()";
335 }
336 echo ";";
337 }
338 }
339 }
340