forked from bozdoz/wp-plugin-leaflet-map
-
Notifications
You must be signed in to change notification settings - Fork 0
/
class.plugin-option.php
171 lines (152 loc) · 3.88 KB
/
class.plugin-option.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
<?php
/**
* Leaflet_Map_Plugin_Option
*
* Store values; render widgets
*
* PHP Version 5.5
*
* @category Shortcode
* @author Benjamin J DeLong <[email protected]>
*/
// Exit if accessed directly
if (!defined('ABSPATH')) {
exit;
}
/**
* Leaflet_Map_Plugin_Option
*/
class Leaflet_Map_Plugin_Option
{
/**
* Default Value
*
* @var varies $default
*/
public $default = '';
/**
* Input type ex: ('text', 'select', 'checkbox')
*
* @var string $type
*/
public $type;
/**
* Optional used for select; maybe checkbox/radio
*
* @var array $options
*/
public $options = array();
/**
* Optional used for label under input
*
* @var string $helptext
*/
public $helptext = '';
/**
* Instantiate class
*
* @param array $details A list of options
*/
function __construct($details = array())
{
if (!$details) {
// just an empty db entry (for now)
// nothing to store, nothing to render
return;
}
$option_filter = array(
'display_name' => FILTER_SANITIZE_STRING,
'default' => null,
'type' => FILTER_SANITIZE_STRING,
'options' => array(
'filter' => FILTER_SANITIZE_STRING,
'flags' => FILTER_FORCE_ARRAY
),
'helptext' => null
);
// get matching keys only
$details = array_intersect_key($details, $option_filter);
// apply filter
$details = filter_var_array($details, $option_filter);
foreach ($details as $key => $value) {
$this->$key = $value;
}
}
/**
* Renders a widget
*
* @param string $name widget name
* @param varies $value widget value
*
* @return HTML
*/
function widget ($name, $value)
{
switch ($this->type) {
case 'text':
?>
<input
class="full-width"
name="<?php echo $name; ?>"
type="<?php echo $this->type; ?>"
id="<?php echo $name; ?>"
value="<?php echo htmlspecialchars($value); ?>"
/>
<?php
break;
case 'number':
?>
<input
class="full-width"
step="any"
name="<?php echo $name; ?>"
type="<?php echo $this->type; ?>"
id="<?php echo $name; ?>"
value="<?php echo htmlspecialchars($value); ?>"
/>
<?php
break;
case 'textarea':
?>
<textarea
id="<?php echo $name; ?>"
class="full-width"
name="<?php echo $name; ?>"><?php echo htmlspecialchars($value); ?></textarea>
<?php
break;
case 'checkbox':
?>
<input
class="checkbox"
name="<?php echo $name; ?>"
type="checkbox"
id="<?php echo $name; ?>"
<?php if ($value) echo ' checked="checked"' ?>
/>
<?php
break;
case 'select':
?>
<select id="<?php echo $name; ?>"
name="<?php echo $name; ?>"
class="full-width">
<?php
foreach ($this->options as $o => $n) {
?>
<option value="<?php echo $o; ?>"<?php if ($value == $o) echo ' selected' ?>>
<?php echo $n; ?>
</option>
<?php
}
?>
</select>
<?php
break;
default:
?>
<div>No option type chosen for <?php echo $name; ?> with value <?php echo htmlspecialchars($value); ?></div>
<?php
break;
}
}
}