| 1 |
<?php |
| 2 |
/** |
| 3 |
* |
| 4 |
* Used to get and set values |
| 5 |
* |
| 6 |
* Features: |
| 7 |
* * Add prefixes to db options |
| 8 |
* * built-in admin settings page method |
| 9 |
* |
| 10 |
*/ |
| 11 |
|
| 12 |
// Exit if accessed directly |
| 13 |
if ( !defined( 'ABSPATH' ) ) exit; |
| 14 |
|
| 15 |
include_once(LEAFLET_MAP__PLUGIN_DIR . 'class.plugin-option.php'); |
| 16 |
|
| 17 |
class Leaflet_Map_Plugin_Settings { |
| 18 |
/** |
| 19 |
* Prefix for options, for unique db entries |
| 20 |
* @var string $prefix |
| 21 |
*/ |
| 22 |
public $prefix = 'leaflet_'; |
| 23 |
|
| 24 |
/** |
| 25 |
* @var Leaflet_Map_Plugin_Settings |
| 26 |
**/ |
| 27 |
private static $instance = null; |
| 28 |
|
| 29 |
/** |
| 30 |
* Default values and admin form information |
| 31 |
* Needs to be created within __construct |
| 32 |
* in order to use a function such as __() |
| 33 |
* @var array $options |
| 34 |
*/ |
| 35 |
public $options = array(); |
| 36 |
|
| 37 |
/** |
| 38 |
* Singleton |
| 39 |
* @static |
| 40 |
*/ |
| 41 |
public static function init() { |
| 42 |
if ( !self::$instance ) { |
| 43 |
self::$instance = new self; |
| 44 |
} |
| 45 |
|
| 46 |
return self::$instance; |
| 47 |
} |
| 48 |
|
| 49 |
private function __construct () { |
| 50 |
|
| 51 |
/* update leaflet version from main class */ |
| 52 |
$leaflet_version = Leaflet_Map::$leaflet_version; |
| 53 |
|
| 54 |
$foreachmap = __('You can also change this for each map'); |
| 55 |
|
| 56 |
/* |
| 57 |
* initiate options using internationalization! |
| 58 |
*/ |
| 59 |
$this->options = array( |
| 60 |
'default_lat' => array( |
| 61 |
'display_name'=>__('Default Latitude', 'leaflet-map'), |
| 62 |
'default'=>'44.67', |
| 63 |
'type' => 'text', |
| 64 |
'helptext' => sprintf('%1$s %2$s <br /> <code>[leaflet-map lat="44.67"]</code>', |
| 65 |
__('Default latitude for maps.', 'leaflet-map'), |
| 66 |
$foreachmap |
| 67 |
) |
| 68 |
), |
| 69 |
'default_lng' => array( |
| 70 |
'display_name'=>__('Default Longitude', 'leaflet-map'), |
| 71 |
'default'=>'-63.61', |
| 72 |
'type' => 'text', |
| 73 |
'helptext' => sprintf('%1$s %2$s <br /> <code>[leaflet-map lng="-63.61"]</code>', |
| 74 |
__('Default longitude for maps.', 'leaflet-map'), |
| 75 |
$foreachmap |
| 76 |
) |
| 77 |
), |
| 78 |
'default_zoom' => array( |
| 79 |
'display_name'=>__('Default Zoom', 'leaflet-map'), |
| 80 |
'default'=>'12', |
| 81 |
'type' => 'text', |
| 82 |
'helptext' => sprintf('%1$s %2$s <br /> <code>[leaflet-map zoom="5"]</code>', |
| 83 |
__('Default zoom for maps.', 'leaflet-map'), |
| 84 |
$foreachmap |
| 85 |
) |
| 86 |
), |
| 87 |
'default_height' => array( |
| 88 |
'display_name'=>__('Default Height', 'leaflet-map'), |
| 89 |
'default'=>'250', |
| 90 |
'type' => 'text', |
| 91 |
'helptext' => sprintf('%1$s %2$s <br /> <code>[leaflet-map height="250"]</code>', |
| 92 |
__('Default height for maps. Values can include "px" but it is not necessary. Can also be "%". ', 'leaflet-map'), |
| 93 |
$foreachmap |
| 94 |
) |
| 95 |
), |
| 96 |
'default_width' => array( |
| 97 |
'display_name'=>__('Default Width', 'leaflet-map'), |
| 98 |
'default'=>'100%', |
| 99 |
'type' => 'text', |
| 100 |
'helptext' => sprintf('%1$s %2$s <br /> <code>[leaflet-map width="100%"]</code>', |
| 101 |
__('Default width for maps. Values can include "px" but it is not necessary. Can also be "%".', 'leaflet-map'), |
| 102 |
$foreachmap |
| 103 |
) |
| 104 |
), |
| 105 |
'fit_markers' => array( |
| 106 |
'display_name'=>__('Fit Markers', 'leaflet-map'), |
| 107 |
'default' => '0', |
| 108 |
'type' => 'checkbox', |
| 109 |
'helptext' => sprintf('%1$s %2$s <br /> <code>[leaflet-map fit_markers="1"]</code>', |
| 110 |
__('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'), |
| 111 |
$foreachmap |
| 112 |
) |
| 113 |
), |
| 114 |
'show_zoom_controls' => array( |
| 115 |
'display_name'=>__('Show Zoom Controls', 'leaflet-map'), |
| 116 |
'default' => '0', |
| 117 |
'type' => 'checkbox', |
| 118 |
'helptext' => sprintf('%1$s %2$s <br /> <code>[leaflet-map zoomcontrol="0"]</code>', |
| 119 |
__('The zoom buttons can be large and annoying.', 'leaflet-map'), |
| 120 |
$foreachmap |
| 121 |
) |
| 122 |
), |
| 123 |
'scroll_wheel_zoom' => array( |
| 124 |
'display_name'=>__('Scroll Wheel Zoom', 'leaflet-map'), |
| 125 |
'default' => '0', |
| 126 |
'type' => 'checkbox', |
| 127 |
'helptext' => sprintf('%1$s %2$s <br /> <code>[leaflet-map scrollwheel="0"]</code>', |
| 128 |
__('Disable zoom with mouse scroll wheel. Sometimes someone wants to scroll down the page, and not zoom the map.', 'leaflet-map'), |
| 129 |
$foreachmap |
| 130 |
) |
| 131 |
), |
| 132 |
'double_click_zoom' => array( |
| 133 |
'display_name'=>__('Double Click Zoom', 'leaflet-map'), |
| 134 |
'default' => '0', |
| 135 |
'type' => 'checkbox', |
| 136 |
'helptext' => sprintf('%1$s %2$s <br /> <code>[leaflet-map doubleClickZoom=false]</code>', |
| 137 |
__('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'), |
| 138 |
$foreachmap |
| 139 |
) |
| 140 |
), |
| 141 |
'default_min_zoom' => array( |
| 142 |
'display_name'=>__('Default Min Zoom', 'leaflet-map'), |
| 143 |
'default' => '0', |
| 144 |
'type' => 'text', |
| 145 |
'helptext' => sprintf('%1$s %2$s <br /> <code>[leaflet-map min_zoom="1"]</code>', |
| 146 |
__('Restrict the viewer from zooming in past the minimum zoom. Can set per map in shortcode or adjust for all maps here.', 'leaflet-map'), |
| 147 |
$foreachmap |
| 148 |
) |
| 149 |
), |
| 150 |
'default_max_zoom' => array( |
| 151 |
'display_name'=>__('Default Max Zoom', 'leaflet-map'), |
| 152 |
'default' => '20', |
| 153 |
'type' => 'text', |
| 154 |
'helptext' => sprintf('%1$s %2%s <br /> <code>%3$s</code>', |
| 155 |
__('Restrict the viewer from zooming out past the maximum zoom. Can set per map in shortcode or adjust for all maps here', 'leaflet-map'), |
| 156 |
$foreachmap, |
| 157 |
'[leaflet-map max_zoom="10"]' |
| 158 |
) |
| 159 |
), |
| 160 |
'default_tiling_service' => array( |
| 161 |
'display_name'=>__('Default Tiling Service', 'leaflet-map'), |
| 162 |
'default' => 'other', |
| 163 |
'type' => 'select', |
| 164 |
'options' => array( |
| 165 |
'other' => __('I will provide my own map tile URL', 'leaflet-map'), |
| 166 |
'mapquest' => __('MapQuest (I have an app key)', 'leaflet-map'), |
| 167 |
), |
| 168 |
'helptext' => __('Choose a tiling service or provide your own.', 'leaflet-map') |
| 169 |
), |
| 170 |
'mapquest_appkey' => array( |
| 171 |
'display_name'=>__('MapQuest App Key', 'leaflet-map'), |
| 172 |
'default' => __('Supply an app key if you choose MapQuest', 'leaflet-map'), |
| 173 |
'type' => 'text', |
| 174 |
'noreset' => true, |
| 175 |
'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', |
| 176 |
__('If you choose MapQuest, you must provide an app key.', 'leaflet-map'), |
| 177 |
__('Sign up', 'leaflet-map'), |
| 178 |
__('then', 'leaflet-map'), |
| 179 |
__('Create a new app', 'leaflet-map'), |
| 180 |
__('then supply the "Consumer Key" here.', 'leaflet-map') |
| 181 |
) |
| 182 |
), |
| 183 |
'map_tile_url' => array( |
| 184 |
'display_name'=>__('Map Tile URL', 'leaflet-map'), |
| 185 |
'default'=>'https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', |
| 186 |
'type' => 'text', |
| 187 |
'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>', |
| 188 |
__('See more tile servers', 'leaflet-map'), |
| 189 |
__('here', 'leaflet-map'), |
| 190 |
__('Please note: free tiles from MapQuest have been discontinued without use of an app key', 'leaflet-map'), |
| 191 |
__('blog post', 'leaflet-map'), |
| 192 |
$foreachmap |
| 193 |
) |
| 194 |
), |
| 195 |
'map_tile_url_subdomains' => array( |
| 196 |
'display_name'=>__('Map Tile URL Subdomains', 'leaflet-map'), |
| 197 |
'default'=>'abc', |
| 198 |
'type' => 'text', |
| 199 |
'helptext' => sprintf('%1$s %2$s <br/> <code>[leaflet-map subdomains="1234"]</code>', |
| 200 |
__('Some maps get tiles from multiple servers with subdomains such as a,b,c,d or 1,2,3,4', 'leaflet-map'), |
| 201 |
$foreachmap |
| 202 |
) |
| 203 |
), |
| 204 |
'js_url' => array( |
| 205 |
'display_name'=>__('JavaScript URL', 'leaflet-map'), |
| 206 |
'default' => sprintf('https://unpkg.com/leaflet@%s/dist/leaflet.js', $leaflet_version), |
| 207 |
'type' => 'text', |
| 208 |
'helptext' => __('If you host your own Leaflet files, then paste the URL here.', 'leaflet-map') |
| 209 |
), |
| 210 |
'css_url' => array( |
| 211 |
'display_name'=>__('CSS URL', 'leaflet-map'), |
| 212 |
'default' => sprintf('https://unpkg.com/leaflet@%s/dist/leaflet.css', $leaflet_version), |
| 213 |
'type' => 'text', |
| 214 |
'helptext' => __('Same as above.', 'leaflet-map') |
| 215 |
), |
| 216 |
'default_attribution' => array( |
| 217 |
'display_name'=>__('Default Attribution', 'leaflet-map'), |
| 218 |
'default' => sprintf('<a href="http://leafletjs.com" title="%1$s">Leaflet</a>; \r\n© <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a> %2$s', |
| 219 |
__("A JS library for interactive maps", 'leaflet-map'), |
| 220 |
__("contributors", 'leaflet-map') |
| 221 |
), |
| 222 |
'type' => 'textarea', |
| 223 |
'helptext' => __('Attribution to a custom tile url. Use semi-colons (;) to separate multiple.', 'leaflet-map') |
| 224 |
), |
| 225 |
'geocoder' => array( |
| 226 |
'display_name'=>__('Geocoder', 'leaflet-map'), |
| 227 |
'default' => 'google', |
| 228 |
'type' => 'select', |
| 229 |
'options' => array( |
| 230 |
'google' => __('Google Maps', 'leaflet-map'), |
| 231 |
'osm' => __('OpenStreetMap Nominatim', 'leaflet-map'), |
| 232 |
'dawa' => __('Denmark Addresses', 'leaflet-map') |
| 233 |
), |
| 234 |
'helptext' => __('Select the Geocoding provider to use to retrieve addresses defined in shortcode.', 'leaflet-map') |
| 235 |
) |
| 236 |
); |
| 237 |
|
| 238 |
foreach ($this->options as $name => $details) { |
| 239 |
$this->options[ $name ] = new Leaflet_Map_Plugin_Option( $details ); |
| 240 |
} |
| 241 |
} |
| 242 |
|
| 243 |
/* |
| 244 |
* wrapper for WordPress get_options (adds prefix to default options) |
| 245 |
* |
| 246 |
* @param string $key |
| 247 |
* @param varies $default default value if not found in db |
| 248 |
* @return varies |
| 249 |
*/ |
| 250 |
|
| 251 |
public function get ($key) { |
| 252 |
$default = $this->options[ $key ]->default; |
| 253 |
$key = $this->prefix . $key; |
| 254 |
return get_option($key, $default); |
| 255 |
} |
| 256 |
|
| 257 |
/* |
| 258 |
* wrapper for WordPress update_option (adds prefix to default options) |
| 259 |
* |
| 260 |
* @param string $key |
| 261 |
* @param varies $value |
| 262 |
* @param varies $default default value if not found in db |
| 263 |
* @return varies |
| 264 |
*/ |
| 265 |
|
| 266 |
public function set ($key, $value) { |
| 267 |
$key = $this->prefix . $key; |
| 268 |
update_option($key, $value); |
| 269 |
return $this; |
| 270 |
} |
| 271 |
|
| 272 |
/* |
| 273 |
* wrapper for WordPress delete_option (adds prefix to default options) |
| 274 |
* |
| 275 |
* @param string $key |
| 276 |
* @param varies $default default value if not found in db |
| 277 |
* @return varies |
| 278 |
*/ |
| 279 |
|
| 280 |
public function delete ($key) { |
| 281 |
$key = $this->prefix . $key; |
| 282 |
return delete_option($key); |
| 283 |
} |
| 284 |
|
| 285 |
/* |
| 286 |
* wrapper for WordPress delete_option (adds prefix to default options) |
| 287 |
* |
| 288 |
* @param string $key |
| 289 |
* @param varies $default default value if not found in db |
| 290 |
* @return varies |
| 291 |
*/ |
| 292 |
|
| 293 |
public function reset () { |
| 294 |
foreach ($this->options as $name => $option) { |
| 295 |
$this->delete( $name ); |
| 296 |
} |
| 297 |
return $this; |
| 298 |
} |
| 299 |
} |