-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathAST_OptionsManager.php
More file actions
353 lines (351 loc) · 10.8 KB
/
AST_OptionsManager.php
File metadata and controls
353 lines (351 loc) · 10.8 KB
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
<?php
class AST_OptionsManager
{
public function getOptionNamePrefix()
{
return get_class($this) . '_';
}
public function getOptionMetaData()
{
return array();
}
public function getOptionNames()
{
return array_keys($this->getOptionMetaData());
}
protected function initOptions()
{
}
protected function deleteSavedOptions()
{
$optionMetaData = $this->getOptionMetaData();
if (is_array($optionMetaData))
{
foreach ($optionMetaData as $aOptionKey => $aOptionMeta)
{
$prefixedOptionName = $this->prefix($aOptionKey);
delete_option($prefixedOptionName);
}
}
}
public function getPluginDisplayName()
{
return get_class($this);
}
public function prefix($name)
{
$optionNamePrefix = $this->getOptionNamePrefix();
if (strpos($name, $optionNamePrefix) === 0)
{
return $name;
}
return $optionNamePrefix . $name;
}
public function &unPrefix($name)
{
$optionNamePrefix = $this->getOptionNamePrefix();
if (strpos($name, $optionNamePrefix) === 0)
{
return substr($name, strlen($optionNamePrefix));
}
return $name;
}
public function getOption($optionName, $default = null)
{
$prefixedOptionName = $this->prefix($optionName);
$retVal = get_option($prefixedOptionName);
if (!$retVal && $default)
{
$retVal = $default;
}
return $retVal;
}
public function deleteOption($optionName)
{
$prefixedOptionName = $this->prefix($optionName);
return delete_option($prefixedOptionName);
}
public function addOption($optionName, $value)
{
$prefixedOptionName = $this->prefix($optionName);
return add_option($prefixedOptionName, $value);
}
public function updateOption($optionName, $value)
{
$prefixedOptionName = $this->prefix($optionName);
return update_option($prefixedOptionName, $value);
}
public function getRoleOption($optionName)
{
$roleAllowed = $this->getOption($optionName);
if (!$roleAllowed || $roleAllowed == '')
{
$roleAllowed = 'Administrator';
}
return $roleAllowed;
}
protected function roleToCapability($roleName)
{
switch ($roleName)
{
case 'Super Admin':
return 'manage_options';
case 'Administrator':
return 'manage_options';
case 'Editor':
return 'publish_pages';
case 'Author':
return 'publish_posts';
case 'Contributor':
return 'edit_posts';
case 'Subscriber':
return 'read';
case 'Anyone':
return 'read';
}
return '';
}
public function isUserRoleEqualOrBetterThan($roleName)
{
if ('Anyone' == $roleName)
{
return true;
}
$capability = $this->roleToCapability($roleName);
return current_user_can($capability);
}
public function canUserDoRoleOption($optionName)
{
$roleAllowed = $this->getRoleOption($optionName);
if ('Anyone' == $roleAllowed)
{
return true;
}
return $this->isUserRoleEqualOrBetterThan($roleAllowed);
}
public function createSettingsMenu()
{
$pluginName = $this->getPluginDisplayName();
add_menu_page($pluginName . ' Plugin Settings', $pluginName, 'administrator', get_class($this), array(
&$this,
'settingsPage'
));
add_action('admin_init', array(
&$this,
'registerSettings'
));
}
public function registerSettings()
{
$settingsGroup = get_class($this) . '-settings-group';
$optionMetaData = $this->getOptionMetaData();
foreach ($optionMetaData as $aOptionKey => $aOptionMeta)
{
register_setting($settingsGroup, $aOptionMeta);
}
}
public function settingsPage()
{
if (!current_user_can('manage_options'))
{
wp_die(__('You do not have sufficient permissions to access this page.', ''));
}
$optionMetaData = $this->getOptionMetaData();
if ($optionMetaData != null)
{
foreach ($optionMetaData as $aOptionKey => $aOptionMeta)
{
if (isset($_POST[$aOptionKey]))
{
$this->updateOption($aOptionKey, $_POST[$aOptionKey]);
}
}
}
$settingsGroup = get_class($this) . '-settings-group';
?>
<div class="wrap">
<h2><?php
_e('System Settings', '');
?></h2>
<table cellspacing="1" cellpadding="2"><tbody>
<tr><td><?php
_e('System', '');
?></td><td><?php
echo php_uname();
?></td></tr>
<tr><td><?php
_e('PHP Version', '');
?></td>
<td><?php
echo phpversion();
?>
<?php
if (version_compare('5.2', phpversion()) > 0)
{
echo ' <span style="background-color: #ffcc00;">';
_e('(WARNING: This plugin may not work properly with versions earlier than PHP 5.2)', '');
echo '</span>';
}
?>
</td>
</tr>
<tr><td><?php
_e('MySQL Version', '');
?></td>
<td><?php
echo $this->getMySqlVersion();
?>
<?php
echo ' <span style="background-color: #ffcc00;">';
if (version_compare('5.0', $this->getMySqlVersion()) > 0)
{
_e('(WARNING: This plugin may not work properly with versions earlier than MySQL 5.0)', '');
}
echo '</span>';
?>
</td>
</tr>
</tbody></table>
<h2><?php
echo $this->getPluginDisplayName();
echo ' ';
_e('Settings', '');
?></h2>
<form method="post" action="">
<?php
settings_fields($settingsGroup);
?>
<style type="text/css">
table.plugin-options-table {width: 100%; padding: 0;}
table.plugin-options-table tr:nth-child(even) {background: #f9f9f9}
table.plugin-options-table tr:nth-child(odd) {background: #FFF}
table.plugin-options-table tr:first-child {width: 35%;}
table.plugin-options-table td {vertical-align: middle;}
table.plugin-options-table td+td {width: auto}
table.plugin-options-table td > p {margin-top: 0; margin-bottom: 0;}
</style>
<table class="plugin-options-table"><tbody>
<?php
if ($optionMetaData != null)
{
foreach ($optionMetaData as $aOptionKey => $aOptionMeta)
{
$displayText = is_array($aOptionMeta) ? $aOptionMeta[0] : $aOptionMeta;
?>
<tr valign="top">
<th scope="row"><p><label for="<?php
echo $aOptionKey;
?>"><?php
echo $displayText;
?></label></p></th>
<td>
<?php
$this->createFormControl($aOptionKey, $aOptionMeta, $this->getOption($aOptionKey));
?>
</td>
</tr>
<?php
}
}
?>
</tbody></table>
<p class="submit">
<input type="submit" class="button-primary"
value="<?php
_e('Save Changes', '');
?>"/>
</p>
</form>
<?php
$stat = sprintf('%d queries in %.3f seconds, using %.2fMB memory', get_num_queries(), timer_stop(0, 3), memory_get_peak_usage() / 1024 / 1024);
echo $stat;
?>
</div>
<?php
}
protected function createFormControl($aOptionKey, $aOptionMeta, $savedOptionValue)
{
if (is_array($aOptionMeta) && count($aOptionMeta) >= 2)
{
$choices = array_slice($aOptionMeta, 1);
?>
<p><select name="<?php
echo $aOptionKey;
?>" id="<?php
echo $aOptionKey;
?>">
<?php
foreach ($choices as $aChoice)
{
$selected = ($aChoice == $savedOptionValue) ? 'selected' : '';
?>
<option value="<?php
echo $aChoice;
?>" <?php
echo $selected;
?>><?php
echo $this->getOptionValueI18nString($aChoice);
?></option>
<?php
}
?>
</select></p>
<?php
}
else
{
?>
<p><input type="text" name="<?php
echo $aOptionKey;
?>" id="<?php
echo $aOptionKey;
?>"
value="<?php
echo esc_attr($savedOptionValue);
?>" size="50"/></p>
<?php
}
}
protected function getOptionValueI18nString($optionValue)
{
switch ($optionValue)
{
case 'true':
return __('true', '');
case 'false':
return __('false', '');
case 'Administrator':
return __('Administrator', '');
case 'Editor':
return __('Editor', '');
case 'Author':
return __('Author', '');
case 'Contributor':
return __('Contributor', '');
case 'Subscriber':
return __('Subscriber', '');
case 'Anyone':
return __('Anyone', '');
}
return $optionValue;
}
protected function getMySqlVersion()
{
global $wpdb;
$rows = $wpdb->get_results('select version() as mysqlversion');
if (!empty($rows))
{
return $rows[0]->mysqlversion;
}
return false;
}
public function getEmailDomain()
{
$sitename = strtolower($_SERVER['SERVER_NAME']);
if (substr($sitename, 0, 4) == 'www.')
{
$sitename = substr($sitename, 4);
}
return $sitename;
}
}