PluginProbe
Leaflet Map / 2.11.2
Leaflet Map v2.11.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.11.2, at class.leaflet-map.php

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