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 / class.leaflet-map.php

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

347 lines 10.4 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 $settings = self::settings();
124
125 if ($settings->get('shortcode_in_excerpt')) {
126 // allows maps in excerpts
127 add_filter('the_excerpt', 'do_shortcode');
128 }
129 }
130
131 /**
132 * Includes and adds shortcodes
133 */
134 private function add_shortcodes()
135 {
136 // shortcodes
137 $shortcode_dir = LEAFLET_MAP__PLUGIN_DIR . 'shortcodes/';
138
139 foreach ($this->_shortcodes as $shortcode => $details) {
140 include_once $shortcode_dir . $details['file'];
141 add_shortcode($shortcode, array($details['class'], 'shortcode'));
142 }
143 }
144
145 /**
146 * Triggered when user uninstalls/removes plugin
147 */
148 public static function uninstall()
149 {
150 // remove settings in db
151 // it needs to be included again because __construct
152 // won't need to execute
153 $settings = self::settings();
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 $settings = self::settings();
176
177 $js_url = $settings->get('js_url');
178 $css_url = $settings->get('css_url');
179
180 wp_register_style('leaflet_stylesheet', $css_url, Array(), null, false);
181 wp_register_script('leaflet_js', $js_url, Array(), null, true);
182
183 // new required MapQuest javascript file
184 $tiling_service = $settings->get('default_tiling_service');
185
186 if ($tiling_service == 'mapquest') {
187 $mapquest_js_url = 'https://www.mapquestapi.com/sdk/leaflet/v2.2/mq-map.js?key=%s';
188 $mq_appkey = $settings->get('mapquest_appkey');
189 $mapquest_js_url = sprintf($mapquest_js_url, $mq_appkey);
190
191 wp_register_script('leaflet_mapquest_plugin', $mapquest_js_url, Array('leaflet_js'), '2.0', true);
192 }
193
194 // optional ajax geojson plugin
195 wp_register_script('tmcw_togeojson', $settings->get('togeojson_url'), Array('jquery'), LEAFLET_MAP__PLUGIN_VERSION, false);
196
197 if (defined('WP_DEBUG') && WP_DEBUG) {
198 $minified = '';
199 } else {
200 $minified = '.min';
201 }
202
203 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);
204
205 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);
206
207 /* run a construct function in the document head for subsequent functions to use (it is lightweight) */
208 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);
209 }
210
211 /**
212 * Filter for removing nulls from array
213 *
214 * @param array $arr
215 *
216 * @return array with nulls removed
217 */
218 public function filter_null($arr)
219 {
220 if (!function_exists('remove_null')) {
221 function remove_null ($var) {
222 return $var !== null;
223 }
224 }
225
226 return array_filter($arr, 'remove_null');
227 }
228
229 /**
230 * Sanitize JSON
231 *
232 * Takes options for filtering/correcting inputs for use in JavaScript
233 *
234 * @param array $arr user-input array
235 * @param array $args array with key-value definitions on how to convert values
236 * @return array corrected for JavaScript
237 */
238 public function json_sanitize($arr, $args)
239 {
240 // remove nulls
241 $arr = $this->filter_null($arr);
242
243 // sanitize output
244 $args = array_intersect_key($args, $arr);
245 $arr = filter_var_array($arr, $args);
246
247 return json_encode($arr);
248 }
249
250 /**
251 * Get Style JSON for map shapes/geojson (svg or canvas)
252 *
253 * Takes atts for creating shapes on the map
254 *
255 * @param array $atts user-input array
256 *
257 * @return array corrected for JavaScript
258 */
259 public function get_style_json($atts)
260 {
261 if ($atts) {
262 extract($atts);
263 }
264
265 // from http://leafletjs.com/reference-1.0.3.html#path
266 $style = array(
267 'stroke' => isset($stroke) ? $stroke : null,
268 'color' => isset($color) ? $color : null,
269 'weight' => isset($weight) ? $weight : null,
270 'opacity' => isset($opacity) ? $opacity : null,
271 'lineCap' => isset($linecap) ? $linecap : null,
272 'lineJoin' => isset($linejoin) ? $linejoin : null,
273 'dashArray' => isset($dasharray) ? $dasharray : null,
274 'dashOffset' => isset($dashoffset) ? $dashoffset : null,
275 'fill' => isset($fill) ? $fill : null,
276 'fillColor' => isset($fillcolor) ? $fillcolor : null,
277 'fillOpacity' => isset($fillopacity) ? $fillopacity : null,
278 'fillRule' => isset($fillrule) ? $fillrule : null,
279 'className' => isset($classname) ? $classname : null,
280 'radius' => isset($radius) ? $radius : null
281 );
282
283 $args = array(
284 'stroke' => FILTER_VALIDATE_BOOLEAN,
285 'color' => FILTER_SANITIZE_STRING,
286 'weight' => FILTER_VALIDATE_FLOAT,
287 'opacity' => FILTER_VALIDATE_FLOAT,
288 'lineCap' => FILTER_SANITIZE_STRING,
289 'lineJoin' => FILTER_SANITIZE_STRING,
290 'dashArray' => FILTER_SANITIZE_STRING,
291 'dashOffset' => FILTER_SANITIZE_STRING,
292 'fill' => FILTER_VALIDATE_BOOLEAN,
293 'fillColor' => FILTER_SANITIZE_STRING,
294 'fillOpacity' => FILTER_VALIDATE_FLOAT,
295 'fillRule' => FILTER_SANITIZE_STRING,
296 'className' => FILTER_SANITIZE_STRING,
297 'radius' => FILTER_VALIDATE_FLOAT
298 );
299
300 return $this->json_sanitize($style, $args);
301 }
302
303 /**
304 * Add Popups to Shapes
305 *
306 * used by leaflet-marker, leaflet-line and leaflet-circle
307 *
308 * @param array $atts user-input array
309 * @param string $content text to display
310 * @param string $shape JavaScript variable for shape
311 *
312 * @return null
313 */
314 public function add_popup_to_shape($atts, $content, $shape)
315 {
316 if (!empty($atts)) {
317 extract($atts);
318 }
319
320 $message = empty($message) ?
321 (empty($content) ? '' : $content) : $message;
322 $message = str_replace(array("\r\n", "\n", "\r"), '<br>', $message);
323 $message = addslashes($message);
324 $message = htmlspecialchars($message);
325 $visible = empty($visible)
326 ? false
327 : filter_var($visible, FILTER_VALIDATE_BOOLEAN);
328
329 if (!empty($message)) {
330 echo "{$shape}.bindPopup(window.WPLeafletMapPlugin.unescape('{$message}'))";
331 if ($visible) {
332 echo ".openPopup()";
333 }
334 echo ";";
335 }
336 }
337
338 /**
339 * Get settings from Leaflet_Map_Plugin_Settings
340 * @return Leaflet_Map_Plugin_Settings
341 */
342 public static function settings () {
343 include_once LEAFLET_MAP__PLUGIN_DIR . 'class.plugin-settings.php';
344 return Leaflet_Map_Plugin_Settings::init();
345 }
346 }
347