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