PluginProbe
Leaflet Map / trunk
Leaflet Map vtrunk
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.plugin-settings.php

class.plugin-settings.php in Leaflet Map trunk, at class.plugin-settings.php

455 lines 18.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Class for getting and setting db/default values
4 *
5 * @category Admin
6 * @author Benjamin J DeLong <ben@bozdoz.com>
7 */
8
9 // Exit if accessed directly
10 if (!defined('ABSPATH')) {
11 exit;
12 }
13
14 require_once LEAFLET_MAP__PLUGIN_DIR . 'class.plugin-option.php';
15
16 // TODO: add option to reset just a single field
17
18 /**
19 * Used to get and set values
20 *
21 * Features:
22 * * Add prefixes to db options
23 * * built-in admin settings page method
24 */
25 class Leaflet_Map_Plugin_Settings
26 {
27 /**
28 * Prefix for options, for unique db entries
29 *
30 * @var string $prefix
31 */
32 public $prefix = 'leaflet_';
33
34 /**
35 * Singleton instance
36 *
37 * @var Leaflet_Map_Plugin_Settings
38 **/
39 private static $_instance = null;
40
41 /**
42 * Default values and admin form information
43 * Needs to be created within __construct
44 * in order to use a function such as __()
45 *
46 * @var array $options
47 */
48 public $options = array();
49
50 /**
51 * Singleton
52 *
53 * @static
54 *
55 * @return Leaflet_Map_Plugin_Settings
56 */
57 public static function init() {
58 if ( !self::$_instance ) {
59 self::$_instance = new self;
60 }
61
62 return self::$_instance;
63 }
64
65 /**
66 * Instantiate the class
67 */
68 private function __construct()
69 {
70
71 /* update leaflet version from main class */
72 $leaflet_version = Leaflet_Map::$leaflet_version;
73
74 $foreachmap = __('You can also change this for each map', 'leaflet-map');
75
76 /*
77 * initiate options using internationalization!
78 */
79 $this->options = array(
80 'default_lat' => array(
81 'display_name'=>__('Default Latitude', 'leaflet-map'),
82 'default'=>'44.67',
83 'type' => 'number',
84 'helptext' => sprintf(
85 '%1$s %2$s <br /> <code>[leaflet-map lat="44.67"]</code>',
86 __('Default latitude for maps.', 'leaflet-map'),
87 $foreachmap
88 )
89 ),
90 'default_lng' => array(
91 'display_name'=>__('Default Longitude', 'leaflet-map'),
92 'default'=>'-63.61',
93 'type' => 'number',
94 'helptext' => sprintf(
95 '%1$s %2$s <br /> <code>[leaflet-map lng="-63.61"]</code>',
96 __('Default longitude for maps.', 'leaflet-map'),
97 $foreachmap
98 )
99 ),
100 'default_zoom' => array(
101 'display_name'=>__('Default Zoom', 'leaflet-map'),
102 'default'=>'12',
103 'type' => 'number',
104 'min' => 0,
105 'max' => 20,
106 'step' => 1,
107 'helptext' => sprintf(
108 '%1$s %2$s <br /> <code>[leaflet-map zoom="5"]</code>',
109 __('Default zoom for maps.', 'leaflet-map'),
110 $foreachmap
111 )
112 ),
113 'default_height' => array(
114 'display_name'=>__('Default Height', 'leaflet-map'),
115 'default'=>'250',
116 'type' => 'text',
117 'helptext' => sprintf(
118 '%1$s %2$s <br /> <code>[leaflet-map height="250"]</code>',
119 __('Default height for maps. Values can include "px" but it is not necessary. Can also be "%". ', 'leaflet-map'),
120 $foreachmap
121 )
122 ),
123 'default_width' => array(
124 'display_name'=>__('Default Width', 'leaflet-map'),
125 'default'=>'100%',
126 'type' => 'text',
127 'helptext' => sprintf(
128 '%1$s %2$s <br /> <code>[leaflet-map width="100%%"]</code>',
129 __('Default width for maps. Values can include "px" but it is not necessary. Can also be "%".', 'leaflet-map'),
130 $foreachmap
131 )
132 ),
133 'fit_markers' => array(
134 'display_name'=>__('Fit Bounds', 'leaflet-map'),
135 'default' => '0',
136 'type' => 'checkbox',
137 'helptext' => sprintf(
138 '%1$s %2$s <br /> <code>[leaflet-map fitbounds]</code>',
139 __('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'),
140 $foreachmap
141 )
142 ),
143 'show_zoom_controls' => array(
144 'display_name'=>__('Show Zoom Controls', 'leaflet-map'),
145 'default' => '0',
146 'type' => 'checkbox',
147 'helptext' => sprintf(
148 '%1$s %2$s <br /> <code>[leaflet-map !zoomcontrol]</code>',
149 __('The zoom buttons can be large and annoying.', 'leaflet-map'),
150 $foreachmap
151 )
152 ),
153 'scroll_wheel_zoom' => array(
154 'display_name'=>__('Scroll Wheel Zoom', 'leaflet-map'),
155 'default' => '0',
156 'type' => 'checkbox',
157 'helptext' => sprintf(
158 '%1$s %2$s <br /> <code>[leaflet-map !scrollwheel]</code>',
159 __('Disable zoom with mouse scroll wheel. Sometimes someone wants to scroll down the page, and not zoom the map.', 'leaflet-map'),
160 $foreachmap
161 )
162 ),
163 'double_click_zoom' => array(
164 'display_name'=>__('Double Click Zoom', 'leaflet-map'),
165 'default' => '0',
166 'type' => 'checkbox',
167 'helptext' => sprintf(
168 '%1$s %2$s <br /> <code>[leaflet-map !doubleClickZoom]</code>',
169 __('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'),
170 $foreachmap
171 )
172 ),
173 'default_min_zoom' => array(
174 'display_name'=>__('Default Min Zoom', 'leaflet-map'),
175 'default' => '0',
176 'type' => 'number',
177 'min' => 0,
178 'max' => 20,
179 'step' => 1,
180 'helptext' => sprintf(
181 '%1$s %2$s <br /> <code>[leaflet-map min_zoom="1"]</code>',
182 __('Restrict the viewer from zooming in past the minimum zoom. Can set per map in shortcode or adjust for all maps here.', 'leaflet-map'),
183 $foreachmap
184 )
185 ),
186 'default_max_zoom' => array(
187 'display_name'=>__('Default Max Zoom', 'leaflet-map'),
188 'default' => '19',
189 'type' => 'number',
190 'min' => 0,
191 'max' => 20,
192 'step' => 1,
193 'helptext' => sprintf(
194 '%1$s %2$s <br /> <code>%3$s</code>',
195 __('Restrict the viewer from zooming out past the maximum zoom. Can set per map in shortcode or adjust for all maps here', 'leaflet-map'),
196 $foreachmap,
197 '[leaflet-map max_zoom="10"]'
198 )
199 ),
200 'default_tiling_service' => array(
201 'display_name'=>__('Default Tiling Service', 'leaflet-map'),
202 'default' => 'other',
203 'type' => 'select',
204 'options' => array(
205 'other' => __('I will provide my own map tile URL', 'leaflet-map'),
206 'mapquest' => __('MapQuest (I have an API key)', 'leaflet-map'),
207 ),
208 'helptext' => __('Choose a tiling service or provide your own.', 'leaflet-map')
209 ),
210 'mapquest_appkey' => array(
211 'display_name'=>__('MapQuest API Key (optional)', 'leaflet-map'),
212 'default' => '',
213 'placeholder' => __('Supply an API key if you choose MapQuest', 'leaflet-map'),
214 'type' => 'text',
215 'noreset' => true,
216 'helptext' => sprintf(
217 '%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',
218 __('If you choose MapQuest, you must provide an API key.', 'leaflet-map'),
219 __('Sign up', 'leaflet-map'),
220 __('then', 'leaflet-map'),
221 __('Create a new app', 'leaflet-map'),
222 __('then supply the "Consumer Key" here.', 'leaflet-map')
223 )
224 ),
225 'map_tile_url' => array(
226 'display_name'=>__('Map Tile URL', 'leaflet-map'),
227 'default'=>'https://tile.openstreetmap.org/{z}/{x}/{y}.png',
228 'type' => 'text',
229 'helptext' => sprintf(
230 '%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}.example.com/{z}/{x}/{y}.jpg subdomains=abcd]</code>',
231 __('See more tile servers', 'leaflet-map'),
232 __('here', 'leaflet-map'),
233 __('Please note: free tiles from MapQuest have been discontinued without use of an API key', 'leaflet-map'),
234 __('blog post', 'leaflet-map'),
235 $foreachmap
236 )
237 ),
238 'map_tile_url_subdomains' => array(
239 'display_name'=>__('Map Tile URL Subdomains', 'leaflet-map'),
240 'default'=>'',
241 'placeholder'=>'abc',
242 'type' => 'text',
243 'helptext' => sprintf(
244 '%1$s %2$s <br/> <code>[leaflet-map subdomains="1234"]</code>',
245 __('Some maps get tiles from multiple servers with subdomains such as a,b,c or 1,2,3 or cache1,cache2; pass a string where each char is its own subdomain, or pass comma-separated string', 'leaflet-map'),
246 $foreachmap
247 )
248 ),
249 'detect_retina' => array(
250 'display_name' => __('Detect Retina', 'leaflet-map'),
251 'default' => '0',
252 'type' => 'checkbox',
253 'helptext' => sprintf(
254 '%1$s %2$s <br /> <code>[leaflet-map detect-retina]</code>',
255 __('Fetch tiles at different zoom levels to appear smoother on retina displays.', 'leaflet-map'),
256 $foreachmap
257 )
258 ),
259 'tilesize' => array(
260 'display_name' => __('Tile Size', 'leaflet-map'),
261 'default' => null,
262 'type' => 'text',
263 'helptext' => sprintf(
264 '%1$s %2$s <br /> <code>[leaflet-map tilesize=512]</code>',
265 __('Width and height of tiles (in pixels) in the grid. Default is 256', 'leaflet-map'),
266 $foreachmap
267 )
268 ),
269 'mapid' => array(
270 'display_name' => __('Tile Id', 'leaflet-map'),
271 'default' => null,
272 'type' => 'text',
273 'helptext' => sprintf(
274 '%1$s %2$s <br /> <code>[leaflet-map mapid="mapbox/streets-v11"]</code>',
275 __('An id that is passed to L.tileLayer; useful for Mapbox', 'leaflet-map'),
276 $foreachmap
277 )
278 ),
279 'accesstoken' => array(
280 'display_name' => __('Access Token', 'leaflet-map'),
281 'default' => null,
282 'type' => 'text',
283 'helptext' => sprintf(
284 '%1$s %2$s <br /> <code>[leaflet-map accesstoken="your.mapbox.access.token"]</code>',
285 __('An access token that is passed to L.tileLayer; useful for Mapbox tiles', 'leaflet-map'),
286 $foreachmap
287 )
288 ),
289 'zoomoffset' => array(
290 'display_name' => __('Zoom Offset', 'leaflet-map'),
291 'default' => null,
292 'type' => 'number',
293 'helptext' => sprintf(
294 '%1$s %2$s <br /> <code>[leaflet-map zoomoffset="-1"]</code>',
295 __('The zoom number used in tile URLs will be offset with this value', 'leaflet-map'),
296 $foreachmap
297 )
298 ),
299 'tile_no_wrap' => array(
300 'display_name' => __('No Wrap (tiles)', 'leaflet-map'),
301 'default' => '0',
302 'type' => 'checkbox',
303 'helptext' => sprintf(
304 '%1$s %2$s <br /> <code>[leaflet-map nowrap]</code>',
305 __('Boolean for whether the layer is wrapped around the antimeridian', 'leaflet-map'),
306 $foreachmap
307 )
308 ),
309 'js_url' => array(
310 'display_name'=>__('JavaScript URL', 'leaflet-map'),
311 'default' => sprintf('https://unpkg.com/leaflet@%s/dist/leaflet.js', $leaflet_version),
312 'type' => 'text',
313 'helptext' => __('If you host your own Leaflet files, then paste the URL here.', 'leaflet-map')
314 ),
315 'css_url' => array(
316 'display_name'=>__('CSS URL', 'leaflet-map'),
317 'default' => sprintf('https://unpkg.com/leaflet@%s/dist/leaflet.css', $leaflet_version),
318 'type' => 'text',
319 'helptext' => __('Same as above.', 'leaflet-map')
320 ),
321 'default_attribution' => array(
322 'display_name' => __('Default Attribution', 'leaflet-map'),
323 'default' => sprintf(
324 '<a href="http://leafletjs.com" title="%1$s">Leaflet</a>; © <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a> %2$s',
325 __("A JS library for interactive maps", 'leaflet-map'),
326 __("contributors", 'leaflet-map')
327 ),
328 'type' => 'textarea',
329 'helptext' => __('Attribution to a custom tile url. Use semi-colons (;) to separate multiple.', 'leaflet-map')
330 ),
331 'show_scale' => array(
332 'display_name' => __('Show Scale', 'leaflet-map'),
333 'default' => '0',
334 'type' => 'checkbox',
335 'helptext' => __(
336 'Add a scale to each map. Can also be added via shortcode <br /> <code>[leaflet-scale]</code>',
337 'leaflet-map'
338 )
339 ),
340 'geocoder' => array(
341 'display_name'=>__('Geocoder', 'leaflet-map'),
342 'default' => 'osm',
343 'type' => 'select',
344 'options' => array(
345 'osm' => __('OpenStreetMap Nominatim', 'leaflet-map'),
346 'google' => __('Google Maps', 'leaflet-map'),
347 'dawa' => __('Denmark Addresses', 'leaflet-map')
348 ),
349 'helptext' => __('Select the Geocoding provider to use to retrieve addresses defined in shortcode.', 'leaflet-map')
350 ),
351 'nominatim_contact_email' => array(
352 'display_name'=>__('Nominatim Contact Email (optional)', 'leaflet-map'),
353 'default' => '',
354 'type' => 'email',
355 'placeholder' => sprintf(
356 __('defaults to admin email (%s)', 'leaflet-map'),
357 get_bloginfo('admin_email')
358 ),
359 'helptext' => __(
360 'Optional contact email to send with OpenStreetMap Nominatim geocoding requests. If blank, the site admin email will be used.',
361 'leaflet-map'
362 ),
363 ),
364 'google_appkey' => array(
365 'display_name'=>__('Google API Key (optional)', 'leaflet-map'),
366 'default' => '',
367 'placeholder' => __('Supply a Google API Key', 'leaflet-map'),
368 'type' => 'text',
369 'noreset' => true,
370 'helptext' => sprintf(
371 '%1$s: <a href="https://cloud.google.com/maps-platform/?apis=places" target="_blank">%2$s</a>. %3$s %4$s',
372 __('The Google Geocoder requires an API key with the Places product enabled', 'leaflet-map'),
373 __('here', 'leaflet-map'),
374 __('You must create a project and set up a billing account, then you will be given an API key.', 'leaflet-map'),
375 __('You are unlikely to ever be charged for geocoding.', 'leaflet-map')
376 ),
377 ),
378 'togeojson_url' => array(
379 'display_name'=>__('KML/GPX JavaScript Converter', 'leaflet-map'),
380 'default' => 'https://unpkg.com/@mapbox/togeojson@0.16.0/togeojson.js',
381 'type' => 'text',
382 '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')
383 ),
384 'shortcode_in_excerpt' => array(
385 'display_name' => __('Show maps in excerpts', 'leaflet-map'),
386 'default' => '0',
387 'type' => 'checkbox',
388 ),
389 );
390
391 foreach ($this->options as $name => $details) {
392 $this->options[ $name ] = new Leaflet_Map_Plugin_Option($details);
393 }
394 }
395
396 /**
397 * Wrapper for WordPress get_options (adds prefix to default options)
398 *
399 * @param string $key
400 *
401 * @return varies
402 */
403 public function get($key)
404 {
405 $default = $this->options[ $key ]->default;
406 $key = $this->prefix . $key;
407 return get_option($key, $default);
408 }
409
410 /**
411 * Wrapper for WordPress update_option (adds prefix to default options)
412 *
413 * @param string $key Unique db key
414 * @param varies $value Value to insert
415 *
416 * @return Leaflet_Map_Plugin_Settings
417 */
418 public function set ($key, $value) {
419 $key = $this->prefix . $key;
420 update_option($key, $value);
421 return $this;
422 }
423
424 /**
425 * Wrapper for WordPress delete_option (adds prefix to default options)
426 *
427 * @param string $key Unique db key
428 *
429 * @return boolean
430 */
431 public function delete($key)
432 {
433 $key = $this->prefix . $key;
434 return delete_option($key);
435 }
436
437 /**
438 * Delete all options
439 *
440 * @return Leaflet_Map_Plugin_Settings
441 */
442 public function reset()
443 {
444 foreach ($this->options as $name => $option) {
445 if (
446 !property_exists($option, 'noreset') ||
447 $option->noreset != true
448 ) {
449 $this->delete($name);
450 }
451 }
452 return $this;
453 }
454 }
455