| 1 |
<?php |
| 2 |
/** |
| 3 |
* Class for getting and setting db/default values |
| 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 |
require_once LEAFLET_MAP__PLUGIN_DIR . 'class.plugin-option.php'; |
| 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'); |
| 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 |
'helptext' => sprintf( |
| 105 |
'%1$s %2$s <br /> <code>[leaflet-map zoom="5"]</code>', |
| 106 |
__('Default zoom for maps.', 'leaflet-map'), |
| 107 |
$foreachmap |
| 108 |
) |
| 109 |
), |
| 110 |
'default_height' => array( |
| 111 |
'display_name'=>__('Default Height', 'leaflet-map'), |
| 112 |
'default'=>'250', |
| 113 |
'type' => 'text', |
| 114 |
'helptext' => sprintf( |
| 115 |
'%1$s %2$s <br /> <code>[leaflet-map height="250"]</code>', |
| 116 |
__('Default height for maps. Values can include "px" but it is not necessary. Can also be "%". ', 'leaflet-map'), |
| 117 |
$foreachmap |
| 118 |
) |
| 119 |
), |
| 120 |
'default_width' => array( |
| 121 |
'display_name'=>__('Default Width', 'leaflet-map'), |
| 122 |
'default'=>'100%', |
| 123 |
'type' => 'text', |
| 124 |
'helptext' => sprintf( |
| 125 |
'%1$s %2$s <br /> <code>[leaflet-map width="100%%"]</code>', |
| 126 |
__('Default width for maps. Values can include "px" but it is not necessary. Can also be "%".', 'leaflet-map'), |
| 127 |
$foreachmap |
| 128 |
) |
| 129 |
), |
| 130 |
'fit_markers' => array( |
| 131 |
'display_name'=>__('Fit Bounds', 'leaflet-map'), |
| 132 |
'default' => '0', |
| 133 |
'type' => 'checkbox', |
| 134 |
'helptext' => sprintf( |
| 135 |
'%1$s %2$s <br /> <code>[leaflet-map fitbounds]</code>', |
| 136 |
__('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'), |
| 137 |
$foreachmap |
| 138 |
) |
| 139 |
), |
| 140 |
'show_zoom_controls' => array( |
| 141 |
'display_name'=>__('Show Zoom Controls', 'leaflet-map'), |
| 142 |
'default' => '0', |
| 143 |
'type' => 'checkbox', |
| 144 |
'helptext' => sprintf( |
| 145 |
'%1$s %2$s <br /> <code>[leaflet-map !zoomcontrol]</code>', |
| 146 |
__('The zoom buttons can be large and annoying.', 'leaflet-map'), |
| 147 |
$foreachmap |
| 148 |
) |
| 149 |
), |
| 150 |
'scroll_wheel_zoom' => array( |
| 151 |
'display_name'=>__('Scroll Wheel Zoom', 'leaflet-map'), |
| 152 |
'default' => '0', |
| 153 |
'type' => 'checkbox', |
| 154 |
'helptext' => sprintf( |
| 155 |
'%1$s %2$s <br /> <code>[leaflet-map !scrollwheel]</code>', |
| 156 |
__('Disable zoom with mouse scroll wheel. Sometimes someone wants to scroll down the page, and not zoom the map.', 'leaflet-map'), |
| 157 |
$foreachmap |
| 158 |
) |
| 159 |
), |
| 160 |
'double_click_zoom' => array( |
| 161 |
'display_name'=>__('Double Click Zoom', 'leaflet-map'), |
| 162 |
'default' => '0', |
| 163 |
'type' => 'checkbox', |
| 164 |
'helptext' => sprintf( |
| 165 |
'%1$s %2$s <br /> <code>[leaflet-map !doubleClickZoom]</code>', |
| 166 |
__('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'), |
| 167 |
$foreachmap |
| 168 |
) |
| 169 |
), |
| 170 |
'default_min_zoom' => array( |
| 171 |
'display_name'=>__('Default Min Zoom', 'leaflet-map'), |
| 172 |
'default' => '0', |
| 173 |
'type' => 'number', |
| 174 |
'helptext' => sprintf( |
| 175 |
'%1$s %2$s <br /> <code>[leaflet-map min_zoom="1"]</code>', |
| 176 |
__('Restrict the viewer from zooming in past the minimum zoom. Can set per map in shortcode or adjust for all maps here.', 'leaflet-map'), |
| 177 |
$foreachmap |
| 178 |
) |
| 179 |
), |
| 180 |
'default_max_zoom' => array( |
| 181 |
'display_name'=>__('Default Max Zoom', 'leaflet-map'), |
| 182 |
'default' => '20', |
| 183 |
'type' => 'number', |
| 184 |
'helptext' => sprintf( |
| 185 |
'%1$s %2%s <br /> <code>%3$s</code>', |
| 186 |
__('Restrict the viewer from zooming out past the maximum zoom. Can set per map in shortcode or adjust for all maps here', 'leaflet-map'), |
| 187 |
$foreachmap, |
| 188 |
'[leaflet-map max_zoom="10"]' |
| 189 |
) |
| 190 |
), |
| 191 |
'default_tiling_service' => array( |
| 192 |
'display_name'=>__('Default Tiling Service', 'leaflet-map'), |
| 193 |
'default' => 'other', |
| 194 |
'type' => 'select', |
| 195 |
'options' => array( |
| 196 |
'other' => __('I will provide my own map tile URL', 'leaflet-map'), |
| 197 |
'mapquest' => __('MapQuest (I have an API key)', 'leaflet-map'), |
| 198 |
), |
| 199 |
'helptext' => __('Choose a tiling service or provide your own.', 'leaflet-map') |
| 200 |
), |
| 201 |
'mapquest_appkey' => array( |
| 202 |
'display_name'=>__('MapQuest API Key (optional)', 'leaflet-map'), |
| 203 |
'default' => __('Supply an API key if you choose MapQuest', 'leaflet-map'), |
| 204 |
'type' => 'text', |
| 205 |
'noreset' => true, |
| 206 |
'helptext' => sprintf( |
| 207 |
'%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', |
| 208 |
__('If you choose MapQuest, you must provide an API key.', 'leaflet-map'), |
| 209 |
__('Sign up', 'leaflet-map'), |
| 210 |
__('then', 'leaflet-map'), |
| 211 |
__('Create a new app', 'leaflet-map'), |
| 212 |
__('then supply the "Consumer Key" here.', 'leaflet-map') |
| 213 |
) |
| 214 |
), |
| 215 |
'map_tile_url' => array( |
| 216 |
'display_name'=>__('Map Tile URL', 'leaflet-map'), |
| 217 |
'default'=>'https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', |
| 218 |
'type' => 'text', |
| 219 |
'helptext' => sprintf( |
| 220 |
'%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>', |
| 221 |
__('See more tile servers', 'leaflet-map'), |
| 222 |
__('here', 'leaflet-map'), |
| 223 |
__('Please note: free tiles from MapQuest have been discontinued without use of an API key', 'leaflet-map'), |
| 224 |
__('blog post', 'leaflet-map'), |
| 225 |
$foreachmap |
| 226 |
) |
| 227 |
), |
| 228 |
'map_tile_url_subdomains' => array( |
| 229 |
'display_name'=>__('Map Tile URL Subdomains', 'leaflet-map'), |
| 230 |
'default'=>'abc', |
| 231 |
'type' => 'text', |
| 232 |
'helptext' => sprintf( |
| 233 |
'%1$s %2$s <br/> <code>[leaflet-map subdomains="1234"]</code>', |
| 234 |
__('Some maps get tiles from multiple servers with subdomains such as a,b,c,d or 1,2,3,4', 'leaflet-map'), |
| 235 |
$foreachmap |
| 236 |
) |
| 237 |
), |
| 238 |
'detect_retina' => array( |
| 239 |
'display_name' => __('Detect Retina', 'leaflet-map'), |
| 240 |
'default' => '0', |
| 241 |
'type' => 'checkbox', |
| 242 |
'helptext' => sprintf( |
| 243 |
'%1$s %2$s <br /> <code>[leaflet-map detect-retina]</code>', |
| 244 |
__('Fetch tiles at different zoom levels to appear smoother on retina displays.', 'leaflet-map'), |
| 245 |
$foreachmap |
| 246 |
) |
| 247 |
), |
| 248 |
'tilesize' => array( |
| 249 |
'display_name' => __('Tile Size', 'leaflet-map'), |
| 250 |
'default' => null, |
| 251 |
'type' => 'text', |
| 252 |
'helptext' => sprintf( |
| 253 |
'%1$s %2$s <br /> <code>[leaflet-map tilesize=512]</code>', |
| 254 |
__('Width and height of tiles (in pixels) in the grid. Default is 256', 'leaflet-map'), |
| 255 |
$foreachmap |
| 256 |
) |
| 257 |
), |
| 258 |
'mapid' => array( |
| 259 |
'display_name' => __('Tile Id', 'leaflet-map'), |
| 260 |
'default' => null, |
| 261 |
'type' => 'text', |
| 262 |
'helptext' => sprintf( |
| 263 |
'%1$s %2$s <br /> <code>[leaflet-map mapid="mapbox/streets-v11"]</code>', |
| 264 |
__('An id that is passed to L.tileLayer; useful for Mapbox', 'leaflet-map'), |
| 265 |
$foreachmap |
| 266 |
) |
| 267 |
), |
| 268 |
'accesstoken' => array( |
| 269 |
'display_name' => __('Access Token', 'leaflet-map'), |
| 270 |
'default' => null, |
| 271 |
'type' => 'text', |
| 272 |
'helptext' => sprintf( |
| 273 |
'%1$s %2$s <br /> <code>[leaflet-map accesstoken="your.mapbox.access.token"]</code>', |
| 274 |
__('An access token that is passed to L.tileLayer; useful for Mapbox tiles', 'leaflet-map'), |
| 275 |
$foreachmap |
| 276 |
) |
| 277 |
), |
| 278 |
'zoomoffset' => array( |
| 279 |
'display_name' => __('Zoom Offset', 'leaflet-map'), |
| 280 |
'default' => null, |
| 281 |
'type' => 'number', |
| 282 |
'helptext' => sprintf( |
| 283 |
'%1$s %2$s <br /> <code>[leaflet-map zoomoffset="-1"]</code>', |
| 284 |
__('The zoom number used in tile URLs will be offset with this value', 'leaflet-map'), |
| 285 |
$foreachmap |
| 286 |
) |
| 287 |
), |
| 288 |
'tile_no_wrap' => array( |
| 289 |
'display_name' => __('No Wrap (tiles)', 'leaflet-map'), |
| 290 |
'default' => '0', |
| 291 |
'type' => 'checkbox', |
| 292 |
'helptext' => sprintf( |
| 293 |
'%1$s %2$s <br /> <code>[leaflet-map nowrap]</code>', |
| 294 |
__('Boolean for whether the layer is wrapped around the antimeridian', 'leaflet-map'), |
| 295 |
$foreachmap |
| 296 |
) |
| 297 |
), |
| 298 |
'js_url' => array( |
| 299 |
'display_name'=>__('JavaScript URL', 'leaflet-map'), |
| 300 |
'default' => sprintf('https://unpkg.com/leaflet@%s/dist/leaflet.js', $leaflet_version), |
| 301 |
'type' => 'text', |
| 302 |
'helptext' => __('If you host your own Leaflet files, then paste the URL here.', 'leaflet-map') |
| 303 |
), |
| 304 |
'css_url' => array( |
| 305 |
'display_name'=>__('CSS URL', 'leaflet-map'), |
| 306 |
'default' => sprintf('https://unpkg.com/leaflet@%s/dist/leaflet.css', $leaflet_version), |
| 307 |
'type' => 'text', |
| 308 |
'helptext' => __('Same as above.', 'leaflet-map') |
| 309 |
), |
| 310 |
'default_attribution' => array( |
| 311 |
'display_name' => __('Default Attribution', 'leaflet-map'), |
| 312 |
'default' => sprintf( |
| 313 |
'<a href="http://leafletjs.com" title="%1$s">Leaflet</a>; © <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a> %2$s', |
| 314 |
__("A JS library for interactive maps", 'leaflet-map'), |
| 315 |
__("contributors", 'leaflet-map') |
| 316 |
), |
| 317 |
'type' => 'textarea', |
| 318 |
'helptext' => __('Attribution to a custom tile url. Use semi-colons (;) to separate multiple.', 'leaflet-map') |
| 319 |
), |
| 320 |
'show_scale' => array( |
| 321 |
'display_name' => __('Show Scale', 'leaflet-map'), |
| 322 |
'default' => '0', |
| 323 |
'type' => 'checkbox', |
| 324 |
'helptext' => __( |
| 325 |
'Add a scale to each map. Can also be added via shortcode <br /> <code>[leaflet-scale]</code>', |
| 326 |
'leaflet-map' |
| 327 |
) |
| 328 |
), |
| 329 |
'geocoder' => array( |
| 330 |
'display_name'=>__('Geocoder', 'leaflet-map'), |
| 331 |
'default' => 'osm', |
| 332 |
'type' => 'select', |
| 333 |
'options' => array( |
| 334 |
'osm' => __('OpenStreetMap Nominatim', 'leaflet-map'), |
| 335 |
'google' => __('Google Maps', 'leaflet-map'), |
| 336 |
'dawa' => __('Denmark Addresses', 'leaflet-map') |
| 337 |
), |
| 338 |
'helptext' => __('Select the Geocoding provider to use to retrieve addresses defined in shortcode.', 'leaflet-map') |
| 339 |
), |
| 340 |
'google_appkey' => array( |
| 341 |
'display_name'=>__('Google API Key (optional)', 'leaflet-map'), |
| 342 |
'default' => __('Supply a Google API Key', 'leaflet-map'), |
| 343 |
'type' => 'text', |
| 344 |
'noreset' => true, |
| 345 |
'helptext' => sprintf( |
| 346 |
'%1$s: <a href="https://cloud.google.com/maps-platform/?apis=places" target="_blank">%2$s</a>. %3$s %4$s', |
| 347 |
__('The Google Geocoder requires an API key with the Places product enabled', 'leaflet-map'), |
| 348 |
__('here', 'leaflet-map'), |
| 349 |
__('You must create a project and set up a billing account, then you will be given an API key.', 'leaflet-map'), |
| 350 |
__('You are unlikely to ever be charged for geocoding.', 'leaflet-map') |
| 351 |
), |
| 352 |
), |
| 353 |
'togeojson_url' => array( |
| 354 |
'display_name'=>__('KML/GPX JavaScript Converter', 'leaflet-map'), |
| 355 |
'default' => 'https://unpkg.com/@mapbox/togeojson@0.16.0/togeojson.js', |
| 356 |
'type' => 'text', |
| 357 |
'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') |
| 358 |
), |
| 359 |
'shortcode_in_excerpt' => array( |
| 360 |
'display_name' => __('Show maps in excerpts', 'leaflet-map'), |
| 361 |
'default' => '0', |
| 362 |
'type' => 'checkbox', |
| 363 |
), |
| 364 |
); |
| 365 |
|
| 366 |
foreach ($this->options as $name => $details) { |
| 367 |
$this->options[ $name ] = new Leaflet_Map_Plugin_Option($details); |
| 368 |
} |
| 369 |
} |
| 370 |
|
| 371 |
/** |
| 372 |
* Wrapper for WordPress get_options (adds prefix to default options) |
| 373 |
* |
| 374 |
* @param string $key |
| 375 |
* |
| 376 |
* @return varies |
| 377 |
*/ |
| 378 |
public function get($key) |
| 379 |
{ |
| 380 |
$default = $this->options[ $key ]->default; |
| 381 |
$key = $this->prefix . $key; |
| 382 |
return get_option($key, $default); |
| 383 |
} |
| 384 |
|
| 385 |
/** |
| 386 |
* Wrapper for WordPress update_option (adds prefix to default options) |
| 387 |
* |
| 388 |
* @param string $key Unique db key |
| 389 |
* @param varies $value Value to insert |
| 390 |
* |
| 391 |
* @return Leaflet_Map_Plugin_Settings |
| 392 |
*/ |
| 393 |
public function set ($key, $value) { |
| 394 |
$key = $this->prefix . $key; |
| 395 |
update_option($key, $value); |
| 396 |
return $this; |
| 397 |
} |
| 398 |
|
| 399 |
/** |
| 400 |
* Wrapper for WordPress delete_option (adds prefix to default options) |
| 401 |
* |
| 402 |
* @param string $key Unique db key |
| 403 |
* |
| 404 |
* @return boolean |
| 405 |
*/ |
| 406 |
public function delete($key) |
| 407 |
{ |
| 408 |
$key = $this->prefix . $key; |
| 409 |
return delete_option($key); |
| 410 |
} |
| 411 |
|
| 412 |
/** |
| 413 |
* Delete all options |
| 414 |
* |
| 415 |
* @return Leaflet_Map_Plugin_Settings |
| 416 |
*/ |
| 417 |
public function reset() |
| 418 |
{ |
| 419 |
foreach ($this->options as $name => $option) { |
| 420 |
if ( |
| 421 |
!property_exists($option, 'noreset') || |
| 422 |
$option->noreset != true |
| 423 |
) { |
| 424 |
$this->delete($name); |
| 425 |
} |
| 426 |
} |
| 427 |
return $this; |
| 428 |
} |
| 429 |
} |
| 430 |
|