Plugin Directory

source: leaflet-map/trunk/class.plugin-settings.php

Last change on this file was 3483997, checked in by bozdoz, 12 days ago

Update gsm/nominatim geocoder implementation with user agent support and fix cache bug (#287)

  • feat(geocoder): rewrite osm geocoder with new endpoint, user agent, and wp_remote_get
  • feat(geocoder): add accept-language based on gotcha tip in nominatim docs
  • fix(geocoder): throw exception on fail instead of polluting cache with false as an object
  • refactor(geocoder): don't reuse var in endpoint url
  • feat(geocoder): osm lazy cache self-healing
  • feat(plugin-option): add placeholder support to input text
  • feat(plugin-settings): add nominatim_contact_email support
  • feat(geocode): add leaflet_map_nominatim_contact_email filter
  • docs(readme): document setting and filter for the nominatim contact email sent in the geocoder user agent
  • fix(geocoder): only use site language otherwise the global cache can end up multilingual depending on the users language
File size: 18.8 KB
Line 
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
10if (!defined('ABSPATH')) {
11    exit;
12}
13
14require_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 */
25class 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' => __('Supply an API key if you choose MapQuest', 'leaflet-map'),
213                'type' => 'text',
214                'noreset' => true,
215                'helptext' => sprintf(
216                    '%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',
217                    __('If you choose MapQuest, you must provide an API key.', 'leaflet-map'),
218                    __('Sign up', 'leaflet-map'),
219                    __('then', 'leaflet-map'),
220                    __('Create a new app', 'leaflet-map'),
221                    __('then supply the "Consumer Key" here.', 'leaflet-map')
222                )
223            ),
224            'map_tile_url' => array(
225                'display_name'=>__('Map Tile URL', 'leaflet-map'),
226                'default'=>'https://tile.openstreetmap.org/{z}/{x}/{y}.png',
227                'type' => 'text',
228                'helptext' => sprintf(
229                    '%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>',
230                    __('See more tile servers', 'leaflet-map'),
231                    __('here', 'leaflet-map'),
232                    __('Please note: free tiles from MapQuest have been discontinued without use of an API key', 'leaflet-map'),
233                    __('blog post', 'leaflet-map'),
234                    $foreachmap
235                )
236            ),
237            'map_tile_url_subdomains' => array(
238                'display_name'=>__('Map Tile URL Subdomains', 'leaflet-map'),
239                'default'=>'abc',
240                'type' => 'text',
241                'helptext' => sprintf(
242                    '%1$s %2$s <br/> <code>[leaflet-map subdomains="1234"]</code>',
243                    __('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'),
244                    $foreachmap
245                )
246            ),
247            'detect_retina' => array(
248                'display_name' => __('Detect Retina', 'leaflet-map'),
249                'default' => '0',
250                'type' => 'checkbox',
251                'helptext' => sprintf(
252                    '%1$s %2$s <br /> <code>[leaflet-map detect-retina]</code>',
253                    __('Fetch tiles at different zoom levels to appear smoother on retina displays.', 'leaflet-map'),
254                    $foreachmap
255                )
256            ),
257            'tilesize' => array(
258                'display_name' => __('Tile Size', 'leaflet-map'),
259                'default' => null,
260                'type' => 'text',
261                'helptext' => sprintf(
262                    '%1$s %2$s <br /> <code>[leaflet-map tilesize=512]</code>',
263                    __('Width and height of tiles (in pixels) in the grid. Default is 256', 'leaflet-map'),
264                    $foreachmap
265                )
266            ),
267            'mapid' => array(
268                'display_name' => __('Tile Id', 'leaflet-map'),
269                'default' => null,
270                'type' => 'text',
271                'helptext' => sprintf(
272                    '%1$s %2$s <br /> <code>[leaflet-map mapid="mapbox/streets-v11"]</code>',
273                    __('An id that is passed to L.tileLayer; useful for Mapbox', 'leaflet-map'),
274                    $foreachmap
275                )
276            ),
277            'accesstoken' => array(
278                'display_name' => __('Access Token', 'leaflet-map'),
279                'default' => null,
280                'type' => 'text',
281                'helptext' => sprintf(
282                    '%1$s %2$s <br /> <code>[leaflet-map accesstoken="your.mapbox.access.token"]</code>',
283                    __('An access token that is passed to L.tileLayer; useful for Mapbox tiles', 'leaflet-map'),
284                    $foreachmap
285                )
286            ),
287            'zoomoffset' => array(
288                'display_name' => __('Zoom Offset', 'leaflet-map'),
289                'default' => null,
290                'type' => 'number',
291                'helptext' => sprintf(
292                    '%1$s %2$s <br /> <code>[leaflet-map zoomoffset="-1"]</code>',
293                    __('The zoom number used in tile URLs will be offset with this value', 'leaflet-map'),
294                    $foreachmap
295                )
296            ),
297            'tile_no_wrap' => array(
298                'display_name' => __('No Wrap (tiles)', 'leaflet-map'),
299                'default' => '0',
300                'type' => 'checkbox',
301                'helptext' => sprintf(
302                    '%1$s %2$s <br /> <code>[leaflet-map nowrap]</code>',
303                    __('Boolean for whether the layer is wrapped around the antimeridian', 'leaflet-map'),
304                    $foreachmap
305                )
306            ),
307            'js_url' => array(
308                'display_name'=>__('JavaScript URL', 'leaflet-map'),
309                'default' => sprintf('https://unpkg.com/leaflet@%s/dist/leaflet.js', $leaflet_version),
310                'type' => 'text',
311                'helptext' => __('If you host your own Leaflet files, then paste the URL here.', 'leaflet-map')
312            ),
313            'css_url' => array(
314                'display_name'=>__('CSS URL', 'leaflet-map'),
315                'default' => sprintf('https://unpkg.com/leaflet@%s/dist/leaflet.css', $leaflet_version),
316                'type' => 'text',
317                'helptext' => __('Same as above.', 'leaflet-map')
318            ),
319            'default_attribution' => array(
320                'display_name' => __('Default Attribution', 'leaflet-map'),
321                'default' => sprintf(
322                    '<a href="http://leafletjs.com" title="%1$s">Leaflet</a>; © <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a> %2$s',
323                    __("A JS library for interactive maps", 'leaflet-map'),
324                    __("contributors", 'leaflet-map')
325                ),
326                'type' => 'textarea',
327                'helptext' => __('Attribution to a custom tile url.  Use semi-colons (;) to separate multiple.', 'leaflet-map')
328            ),
329            'show_scale' => array(
330                'display_name' => __('Show Scale', 'leaflet-map'),
331                'default' => '0',
332                'type' => 'checkbox',
333                'helptext' => __(
334                    'Add a scale to each map. Can also be added via shortcode <br /> <code>[leaflet-scale]</code>', 
335                    'leaflet-map'
336                )
337            ),
338            'geocoder' => array(
339                'display_name'=>__('Geocoder', 'leaflet-map'),
340                'default' => 'osm',
341                'type' => 'select',
342                'options' => array(
343                    'osm' => __('OpenStreetMap Nominatim', 'leaflet-map'),
344                    'google' => __('Google Maps', 'leaflet-map'),
345                    'dawa' => __('Denmark Addresses', 'leaflet-map')
346                ),
347                'helptext' => __('Select the Geocoding provider to use to retrieve addresses defined in shortcode.', 'leaflet-map')
348            ),
349            'nominatim_contact_email' => array(
350                'display_name'=>__('Nominatim Contact Email (optional)', 'leaflet-map'),
351                'default' => '',
352                'type' => 'text',
353                'placeholder' => sprintf(
354                    __('defaults to admin email (%s)', 'leaflet-map'),
355                    get_bloginfo('admin_email')
356                ),
357                'helptext' => __(
358                    'Optional contact email to send with OpenStreetMap Nominatim geocoding requests. If blank, the site admin email will be used.',
359                    'leaflet-map'
360                ),
361            ),
362            'google_appkey' => array(
363                'display_name'=>__('Google API Key (optional)', 'leaflet-map'),
364                'default' => __('Supply a Google API Key', 'leaflet-map'),
365                'type' => 'text',
366                'noreset' => true,
367                'helptext' => sprintf(
368                    '%1$s: <a href="https://cloud.google.com/maps-platform/?apis=places" target="_blank">%2$s</a>.  %3$s %4$s',
369                    __('The Google Geocoder requires an API key with the Places product enabled', 'leaflet-map'),
370                    __('here', 'leaflet-map'),
371                    __('You must create a project and set up a billing account, then you will be given an API key.', 'leaflet-map'),
372                    __('You are unlikely to ever be charged for geocoding.', 'leaflet-map')
373                ),
374            ),
375            'togeojson_url' => array(
376                'display_name'=>__('KML/GPX JavaScript Converter', 'leaflet-map'),
377                'default' => 'https://unpkg.com/@mapbox/togeojson@0.16.0/togeojson.js',
378                'type' => 'text',
379                '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')
380            ),
381            'shortcode_in_excerpt' => array(
382                'display_name' => __('Show maps in excerpts', 'leaflet-map'),
383                'default' => '0',
384                'type' => 'checkbox',
385            ),
386        );
387
388        foreach ($this->options as $name => $details) {
389            $this->options[ $name ] = new Leaflet_Map_Plugin_Option($details);
390        }
391    }
392
393    /**
394     * Wrapper for WordPress get_options (adds prefix to default options)
395     *
396     * @param string $key               
397     *
398     * @return varies
399     */
400    public function get($key) 
401    {
402        $default = $this->options[ $key ]->default;
403        $key = $this->prefix . $key;
404        return get_option($key, $default);
405    }
406
407    /**
408     * Wrapper for WordPress update_option (adds prefix to default options)
409     *
410     * @param string $key   Unique db key
411     * @param varies $value Value to insert
412     *
413     * @return Leaflet_Map_Plugin_Settings
414     */
415    public function set ($key, $value) {
416        $key = $this->prefix . $key;
417        update_option($key, $value);
418        return $this;
419    }
420
421    /**
422     * Wrapper for WordPress delete_option (adds prefix to default options)
423     *
424     * @param string $key Unique db key
425     *
426     * @return boolean
427     */
428    public function delete($key) 
429    {
430        $key = $this->prefix . $key;
431        return delete_option($key);
432    }
433
434    /**
435     * Delete all options
436     *
437     * @return Leaflet_Map_Plugin_Settings
438     */
439    public function reset()
440    {
441        foreach ($this->options as $name => $option) {
442            if (
443                !property_exists($option, 'noreset') ||
444                $option->noreset != true
445            ) {
446                $this->delete($name);
447            }
448        }
449        return $this;
450    }
451}
Note: See TracBrowser for help on using the repository browser.