forked from bozdoz/wp-plugin-leaflet-map
-
Notifications
You must be signed in to change notification settings - Fork 0
/
class.plugin-settings.php
429 lines (412 loc) · 17.9 KB
/
class.plugin-settings.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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
<?php
/**
* Class for getting and setting db/default values
*
* @category Admin
* @author Benjamin J DeLong <[email protected]>
*/
// Exit if accessed directly
if (!defined('ABSPATH')) {
exit;
}
require_once LEAFLET_MAP__PLUGIN_DIR . 'class.plugin-option.php';
// TODO: add option to reset just a single field
/**
* Used to get and set values
*
* Features:
* * Add prefixes to db options
* * built-in admin settings page method
*/
class Leaflet_Map_Plugin_Settings
{
/**
* Prefix for options, for unique db entries
*
* @var string $prefix
*/
public $prefix = 'leaflet_';
/**
* Singleton instance
*
* @var Leaflet_Map_Plugin_Settings
**/
private static $_instance = null;
/**
* Default values and admin form information
* Needs to be created within __construct
* in order to use a function such as __()
*
* @var array $options
*/
public $options = array();
/**
* Singleton
*
* @static
*
* @return Leaflet_Map_Plugin_Settings
*/
public static function init() {
if ( !self::$_instance ) {
self::$_instance = new self;
}
return self::$_instance;
}
/**
* Instantiate the class
*/
private function __construct()
{
/* update leaflet version from main class */
$leaflet_version = Leaflet_Map::$leaflet_version;
$foreachmap = __('You can also change this for each map');
/*
* initiate options using internationalization!
*/
$this->options = array(
'default_lat' => array(
'display_name'=>__('Default Latitude', 'leaflet-map'),
'default'=>'44.67',
'type' => 'number',
'helptext' => sprintf(
'%1$s %2$s <br /> <code>[leaflet-map lat="44.67"]</code>',
__('Default latitude for maps.', 'leaflet-map'),
$foreachmap
)
),
'default_lng' => array(
'display_name'=>__('Default Longitude', 'leaflet-map'),
'default'=>'-63.61',
'type' => 'number',
'helptext' => sprintf(
'%1$s %2$s <br /> <code>[leaflet-map lng="-63.61"]</code>',
__('Default longitude for maps.', 'leaflet-map'),
$foreachmap
)
),
'default_zoom' => array(
'display_name'=>__('Default Zoom', 'leaflet-map'),
'default'=>'12',
'type' => 'number',
'helptext' => sprintf(
'%1$s %2$s <br /> <code>[leaflet-map zoom="5"]</code>',
__('Default zoom for maps.', 'leaflet-map'),
$foreachmap
)
),
'default_height' => array(
'display_name'=>__('Default Height', 'leaflet-map'),
'default'=>'250',
'type' => 'text',
'helptext' => sprintf(
'%1$s %2$s <br /> <code>[leaflet-map height="250"]</code>',
__('Default height for maps. Values can include "px" but it is not necessary. Can also be "%". ', 'leaflet-map'),
$foreachmap
)
),
'default_width' => array(
'display_name'=>__('Default Width', 'leaflet-map'),
'default'=>'100%',
'type' => 'text',
'helptext' => sprintf(
'%1$s %2$s <br /> <code>[leaflet-map width="100%%"]</code>',
__('Default width for maps. Values can include "px" but it is not necessary. Can also be "%".', 'leaflet-map'),
$foreachmap
)
),
'fit_markers' => array(
'display_name'=>__('Fit Bounds', 'leaflet-map'),
'default' => '0',
'type' => 'checkbox',
'helptext' => sprintf(
'%1$s %2$s <br /> <code>[leaflet-map fitbounds]</code>',
__('If enabled, all markers on each map will alter the view of the map; i.e. the map will fit to the bounds of all of the markers on the map.', 'leaflet-map'),
$foreachmap
)
),
'show_zoom_controls' => array(
'display_name'=>__('Show Zoom Controls', 'leaflet-map'),
'default' => '0',
'type' => 'checkbox',
'helptext' => sprintf(
'%1$s %2$s <br /> <code>[leaflet-map !zoomcontrol]</code>',
__('The zoom buttons can be large and annoying.', 'leaflet-map'),
$foreachmap
)
),
'scroll_wheel_zoom' => array(
'display_name'=>__('Scroll Wheel Zoom', 'leaflet-map'),
'default' => '0',
'type' => 'checkbox',
'helptext' => sprintf(
'%1$s %2$s <br /> <code>[leaflet-map !scrollwheel]</code>',
__('Disable zoom with mouse scroll wheel. Sometimes someone wants to scroll down the page, and not zoom the map.', 'leaflet-map'),
$foreachmap
)
),
'double_click_zoom' => array(
'display_name'=>__('Double Click Zoom', 'leaflet-map'),
'default' => '0',
'type' => 'checkbox',
'helptext' => sprintf(
'%1$s %2$s <br /> <code>[leaflet-map !doubleClickZoom]</code>',
__('If enabled, your maps will zoom with a double click. By default it is disabled: If we\'re going to remove zoom controls and have scroll wheel zoom off by default, we might as well stick to our guns and not zoom the map.', 'leaflet-map'),
$foreachmap
)
),
'default_min_zoom' => array(
'display_name'=>__('Default Min Zoom', 'leaflet-map'),
'default' => '0',
'type' => 'number',
'helptext' => sprintf(
'%1$s %2$s <br /> <code>[leaflet-map min_zoom="1"]</code>',
__('Restrict the viewer from zooming in past the minimum zoom. Can set per map in shortcode or adjust for all maps here.', 'leaflet-map'),
$foreachmap
)
),
'default_max_zoom' => array(
'display_name'=>__('Default Max Zoom', 'leaflet-map'),
'default' => '20',
'type' => 'number',
'helptext' => sprintf(
'%1$s %2%s <br /> <code>%3$s</code>',
__('Restrict the viewer from zooming out past the maximum zoom. Can set per map in shortcode or adjust for all maps here', 'leaflet-map'),
$foreachmap,
'[leaflet-map max_zoom="10"]'
)
),
'default_tiling_service' => array(
'display_name'=>__('Default Tiling Service', 'leaflet-map'),
'default' => 'other',
'type' => 'select',
'options' => array(
'other' => __('I will provide my own map tile URL', 'leaflet-map'),
'mapquest' => __('MapQuest (I have an API key)', 'leaflet-map'),
),
'helptext' => __('Choose a tiling service or provide your own.', 'leaflet-map')
),
'mapquest_appkey' => array(
'display_name'=>__('MapQuest API Key (optional)', 'leaflet-map'),
'default' => __('Supply an API key if you choose MapQuest', 'leaflet-map'),
'type' => 'text',
'noreset' => true,
'helptext' => sprintf(
'%1$s <a href="https://developer.mapquest.com/plan_purchase/steps/business_edition/business_edition_free/register" target="_blank"> %2$s </a>, %3$s <a href="https://developer.mapquest.com/user/me/apps" target="_blank"> %4$s </a> %5$s',
__('If you choose MapQuest, you must provide an API key.', 'leaflet-map'),
__('Sign up', 'leaflet-map'),
__('then', 'leaflet-map'),
__('Create a new app', 'leaflet-map'),
__('then supply the "Consumer Key" here.', 'leaflet-map')
)
),
'map_tile_url' => array(
'display_name'=>__('Map Tile URL', 'leaflet-map'),
'default'=>'https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png',
'type' => 'text',
'helptext' => sprintf(
'%1$s: <a href="http://wiki.openstreetmap.org/wiki/Tile_servers" target="_blank"> %2$s </a>. %3$s: <a href="http://devblog.mapquest.com/2016/06/15/modernization-of-mapquest-results-in-changes-to-open-tile-access/" target="_blank"> %4$s </a>. %5$s <br/> <code>[leaflet-map tileurl=http://{s}.tile.stamen.com/watercolor/{z}/{x}/{y}.jpg subdomains=abcd]</code>',
__('See more tile servers', 'leaflet-map'),
__('here', 'leaflet-map'),
__('Please note: free tiles from MapQuest have been discontinued without use of an API key', 'leaflet-map'),
__('blog post', 'leaflet-map'),
$foreachmap
)
),
'map_tile_url_subdomains' => array(
'display_name'=>__('Map Tile URL Subdomains', 'leaflet-map'),
'default'=>'abc',
'type' => 'text',
'helptext' => sprintf(
'%1$s %2$s <br/> <code>[leaflet-map subdomains="1234"]</code>',
__('Some maps get tiles from multiple servers with subdomains such as a,b,c,d or 1,2,3,4', 'leaflet-map'),
$foreachmap
)
),
'detect_retina' => array(
'display_name' => __('Detect Retina', 'leaflet-map'),
'default' => '0',
'type' => 'checkbox',
'helptext' => sprintf(
'%1$s %2$s <br /> <code>[leaflet-map detect-retina]</code>',
__('Fetch tiles at different zoom levels to appear smoother on retina displays.', 'leaflet-map'),
$foreachmap
)
),
'tilesize' => array(
'display_name' => __('Tile Size', 'leaflet-map'),
'default' => null,
'type' => 'text',
'helptext' => sprintf(
'%1$s %2$s <br /> <code>[leaflet-map tilesize=512]</code>',
__('Width and height of tiles (in pixels) in the grid. Default is 256', 'leaflet-map'),
$foreachmap
)
),
'mapid' => array(
'display_name' => __('Tile Id', 'leaflet-map'),
'default' => null,
'type' => 'text',
'helptext' => sprintf(
'%1$s %2$s <br /> <code>[leaflet-map mapid="mapbox/streets-v11"]</code>',
__('An id that is passed to L.tileLayer; useful for Mapbox', 'leaflet-map'),
$foreachmap
)
),
'accesstoken' => array(
'display_name' => __('Access Token', 'leaflet-map'),
'default' => null,
'type' => 'text',
'helptext' => sprintf(
'%1$s %2$s <br /> <code>[leaflet-map accesstoken="your.mapbox.access.token"]</code>',
__('An access token that is passed to L.tileLayer; useful for Mapbox tiles', 'leaflet-map'),
$foreachmap
)
),
'zoomoffset' => array(
'display_name' => __('Zoom Offset', 'leaflet-map'),
'default' => null,
'type' => 'number',
'helptext' => sprintf(
'%1$s %2$s <br /> <code>[leaflet-map zoomoffset="-1"]</code>',
__('The zoom number used in tile URLs will be offset with this value', 'leaflet-map'),
$foreachmap
)
),
'tile_no_wrap' => array(
'display_name' => __('No Wrap (tiles)', 'leaflet-map'),
'default' => '0',
'type' => 'checkbox',
'helptext' => sprintf(
'%1$s %2$s <br /> <code>[leaflet-map nowrap]</code>',
__('Boolean for whether the layer is wrapped around the antimeridian', 'leaflet-map'),
$foreachmap
)
),
'js_url' => array(
'display_name'=>__('JavaScript URL', 'leaflet-map'),
'default' => sprintf('https://unpkg.com/leaflet@%s/dist/leaflet.js', $leaflet_version),
'type' => 'text',
'helptext' => __('If you host your own Leaflet files, then paste the URL here.', 'leaflet-map')
),
'css_url' => array(
'display_name'=>__('CSS URL', 'leaflet-map'),
'default' => sprintf('https://unpkg.com/leaflet@%s/dist/leaflet.css', $leaflet_version),
'type' => 'text',
'helptext' => __('Same as above.', 'leaflet-map')
),
'default_attribution' => array(
'display_name' => __('Default Attribution', 'leaflet-map'),
'default' => sprintf(
'<a href="http://leafletjs.com" title="%1$s">Leaflet</a>; © <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a> %2$s',
__("A JS library for interactive maps", 'leaflet-map'),
__("contributors", 'leaflet-map')
),
'type' => 'textarea',
'helptext' => __('Attribution to a custom tile url. Use semi-colons (;) to separate multiple.', 'leaflet-map')
),
'show_scale' => array(
'display_name' => __('Show Scale', 'leaflet-map'),
'default' => '0',
'type' => 'checkbox',
'helptext' => __(
'Add a scale to each map. Can also be added via shortcode <br /> <code>[leaflet-scale]</code>',
'leaflet-map'
)
),
'geocoder' => array(
'display_name'=>__('Geocoder', 'leaflet-map'),
'default' => 'osm',
'type' => 'select',
'options' => array(
'osm' => __('OpenStreetMap Nominatim', 'leaflet-map'),
'google' => __('Google Maps', 'leaflet-map'),
'dawa' => __('Denmark Addresses', 'leaflet-map')
),
'helptext' => __('Select the Geocoding provider to use to retrieve addresses defined in shortcode.', 'leaflet-map')
),
'google_appkey' => array(
'display_name'=>__('Google API Key (optional)', 'leaflet-map'),
'default' => __('Supply a Google API Key', 'leaflet-map'),
'type' => 'text',
'noreset' => true,
'helptext' => sprintf(
'%1$s: <a href="https://cloud.google.com/maps-platform/?apis=places" target="_blank">%2$s</a>. %3$s %4$s',
__('The Google Geocoder requires an API key with the Places product enabled', 'leaflet-map'),
__('here', 'leaflet-map'),
__('You must create a project and set up a billing account, then you will be given an API key.', 'leaflet-map'),
__('You are unlikely to ever be charged for geocoding.', 'leaflet-map')
),
),
'togeojson_url' => array(
'display_name'=>__('KML/GPX JavaScript Converter', 'leaflet-map'),
'default' => 'https://unpkg.com/@mapbox/[email protected]/togeojson.js',
'type' => 'text',
'helptext' => __('ToGeoJSON converts KML and GPX files to GeoJSON; if you plan to use [leaflet-kml] or [leaflet-gpx] then this library is loaded. You can change the default if you need.', 'leaflet-map')
),
'shortcode_in_excerpt' => array(
'display_name' => __('Show maps in excerpts', 'leaflet-map'),
'default' => '0',
'type' => 'checkbox',
),
);
foreach ($this->options as $name => $details) {
$this->options[ $name ] = new Leaflet_Map_Plugin_Option($details);
}
}
/**
* Wrapper for WordPress get_options (adds prefix to default options)
*
* @param string $key
*
* @return varies
*/
public function get($key)
{
$default = $this->options[ $key ]->default;
$key = $this->prefix . $key;
return get_option($key, $default);
}
/**
* Wrapper for WordPress update_option (adds prefix to default options)
*
* @param string $key Unique db key
* @param varies $value Value to insert
*
* @return Leaflet_Map_Plugin_Settings
*/
public function set ($key, $value) {
$key = $this->prefix . $key;
update_option($key, $value);
return $this;
}
/**
* Wrapper for WordPress delete_option (adds prefix to default options)
*
* @param string $key Unique db key
*
* @return boolean
*/
public function delete($key)
{
$key = $this->prefix . $key;
return delete_option($key);
}
/**
* Delete all options
*
* @return Leaflet_Map_Plugin_Settings
*/
public function reset()
{
foreach ($this->options as $name => $option) {
if (
!property_exists($option, 'noreset') ||
$option->noreset != true
) {
$this->delete($name);
}
}
return $this;
}
}