-
Notifications
You must be signed in to change notification settings - Fork 20
/
admin.php
558 lines (508 loc) · 16.9 KB
/
admin.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
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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
<?php
/**
* The user interface and activation/deactivation methods for administering
* the Object Oriented Plugin Template Solution plugin
*
* This plugin abstracts WordPress' Settings API to simplify the creation of
* a settings admin interface. Read the docblocks for the set_sections() and
* set_fields() methods to learn how to create your own settings.
*
* A table is created in the activate() method and is dropped in the
* deactivate() method. If your plugin needs tables, adjust the table
* definitions and removals as needed. If you don't need a table, remove
* those portions of the activate() and deactivate() methods.
*
* This plugin is coded to be installed in either a regular, single WordPress
* installation or as a network plugin for multisite installations. So, by
* default, multisite networks can only activate this plugin via the
* Network Admin panel. If you want your plugin to be configurable for each
* site in a multisite network, you must do the following:
*
* + Search admin.php and oop-plugin-template-solution.php
* for is_multisite() if statements. Remove the true parts and leave
* the false parts.
* + In oop-plugin-template-solution.php, go to the initialize() method
* and remove the $wpdb->get_blog_prefix(0) portion of the
* $this->table_login assignment.
*
* Beyond that, you're advised to leave the rest of this file alone.
*
* @package oop-plugin-template-solution
* @link http://wordpress.org/extend/plugins/oop-plugin-template-solution/
* @license http://www.gnu.org/licenses/gpl-2.0.html GPLv2
* @author Daniel Convissor <[email protected]>
* @copyright The Analysis and Solutions Company, 2012
*
* This plugin used the Object-Oriented Plugin Template Solution as a skeleton
* REPLACE_PLUGIN_URI
*/
/**
* The user interface and activation/deactivation methods for administering
* the Object Oriented Plugin Template Solution plugin
*
* @package oop-plugin-template-solution
* @link http://wordpress.org/extend/plugins/oop-plugin-template-solution/
* @license http://www.gnu.org/licenses/gpl-2.0.html GPLv2
* @author Daniel Convissor <[email protected]>
* @copyright The Analysis and Solutions Company, 2012
*
* This plugin used the Object-Oriented Plugin Template Solution as a skeleton
* REPLACE_PLUGIN_URI
*/
class oop_plugin_template_solution_admin extends oop_plugin_template_solution {
/**
* The WP privilege level required to use the admin interface
* @var string
*/
protected $capability_required;
/**
* Metadata and labels for each element of the plugin's options
* @var array
*/
protected $fields;
/**
* URI for the forms' action attributes
* @var string
*/
protected $form_action;
/**
* Name of the page holding the options
* @var string
*/
protected $page_options;
/**
* Metadata and labels for each settings page section
* @var array
*/
protected $settings;
/**
* Title for the plugin's settings page
* @var string
*/
protected $text_settings;
/**
* Sets the object's properties and options
*
* @return void
*
* @uses oop_plugin_template_solution::initialize() to set the object's
* properties
* @uses oop_plugin_template_solution_admin::set_sections() to populate the
* $sections property
* @uses oop_plugin_template_solution_admin::set_fields() to populate the
* $fields property
*/
public function __construct() {
$this->initialize();
$this->set_sections();
$this->set_fields();
// Translation already in WP combined with plugin's name.
$this->text_settings = self::NAME . ' ' . __('Settings');
if (is_multisite()) {
$this->capability_required = 'manage_network_options';
$this->form_action = '../options.php';
$this->page_options = 'settings.php';
} else {
$this->capability_required = 'manage_options';
$this->form_action = 'options.php';
$this->page_options = 'options-general.php';
}
}
/*
* ===== ACTIVATION & DEACTIVATION CALLBACK METHODS =====
*/
/**
* Establishes the tables and settings when the plugin is activated
* @return void
*/
public function activate() {
global $wpdb;
if (is_multisite() && !is_network_admin()) {
die($this->hsc_utf8(sprintf(__("%s must be activated via the Network Admin interface when WordPress is in multistie network mode.", 'oop-plugin-template-solution'), self::NAME)));
}
/*
* Create or alter the plugin's tables as needed.
*/
require_once ABSPATH . 'wp-admin/includes/upgrade.php';
// Note: dbDelta() requires two spaces after "PRIMARY KEY". Weird.
// WP's insert/prepare/etc don't handle NULL's (at least in 3.3).
// It also requires the keys to be named and there to be no space
// the column name and the key length.
$sql = "CREATE TABLE `$this->table_login` (
login_id BIGINT(20) NOT NULL AUTO_INCREMENT,
user_login VARCHAR(60) NOT NULL DEFAULT '',
date_login TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (login_id),
KEY user_login (user_login(5))
)";
dbDelta($sql);
if ($wpdb->last_error) {
die($wpdb->last_error);
}
/*
* Save this plugin's options to the database.
*/
if (is_multisite()) {
switch_to_blog(1);
}
update_option($this->option_name, $this->options);
if (is_multisite()) {
restore_current_blog();
}
}
/**
* Removes the tables and settings when the plugin is deactivated
* if the deactivate_deletes_data option is turned on
* @return void
*/
public function deactivate() {
global $wpdb;
$prior_error_setting = $wpdb->show_errors;
$wpdb->show_errors = false;
$denied = 'command denied to user';
$wpdb->query("DROP TABLE `$this->table_login`");
if ($wpdb->last_error) {
if (strpos($wpdb->last_error, $denied) === false) {
die($wpdb->last_error);
}
}
$wpdb->show_errors = $prior_error_setting;
$package_id = self::ID;
$wpdb->escape_by_ref($package_id);
$wpdb->query("DELETE FROM `$wpdb->options`
WHERE option_name LIKE '$package_id%'");
$wpdb->query("DELETE FROM `$wpdb->usermeta`
WHERE meta_key LIKE '$package_id%'");
}
/*
* ===== ADMIN USER INTERFACE =====
*/
/**
* Sets the metadata and labels for each settings page section
*
* Settings pages have sections for grouping related fields. This plugin
* uses the $sections property, below, to define those sections.
*
* The $sections property is a two-dimensional, associative array. The top
* level array is keyed by the section identifier (<sid>) and contains an
* array with the following key value pairs:
*
* + title: a short phrase for the section's header
* + callback: the method for rendering the section's description. If a
* description is not needed, set this to "section_blank". If a
* description is helpful, use "section_<sid>" and create a corresponding
* method named "section_<sid>()".
*
* @return void
* @uses oop_plugin_template_solution_admin::$sections to hold the data
*/
protected function set_sections() {
$this->sections = array(
'login' => array(
'title' => __("Login Policies", 'oop-plugin-template-solution'),
'callback' => 'section_login',
),
'misc' => array(
'title' => __("Miscellaneous Policies", 'oop-plugin-template-solution'),
'callback' => 'section_blank',
),
);
}
/**
* Sets the metadata and labels for each element of the plugin's
* options
*
* The $fields property is a two-dimensional, associative array. The top
* level array is keyed by the field's identifier and contains an array
* with the following key value pairs:
*
* + section: the section identifier (<sid>) for the section this
* setting should be displayed in
* + label: a very short title for the setting
* + text: the long description about what the setting does. Note:
* a description of the default value is automatically appended.
* + type: the data type ("int", "string", or "bool"). If type is "bool,"
* the following two elements are also required:
* + bool0: description for the button indicating the option is off
* + bool1: description for the button indicating the option is on
*
* WARNING: Make sure to keep this propety and the
* oop_plugin_template_solution_admin::$options_default
* property in sync.
*
* @return void
* @uses oop_plugin_template_solution_admin::$fields to hold the data
*/
protected function set_fields() {
$this->fields = array(
'track_logins' => array(
'section' => 'login',
'label' => __("Track Logins", 'oop-plugin-template-solution'),
'text' => __("Should the time of each user's login be stored?", 'oop-plugin-template-solution'),
'type' => 'bool',
'bool0' => __("No, don't track logins.", 'oop-plugin-template-solution'),
'bool1' => __("Yes, track logins.", 'oop-plugin-template-solution'),
),
'deactivate_deletes_data' => array(
'section' => 'misc',
'label' => __("Deactivation", 'oop-plugin-template-solution'),
'text' => __("Should deactivating the plugin remove all of the plugin's data and settings?", 'oop-plugin-template-solution'),
'type' => 'bool',
'bool0' => __("No, preserve the data for future use.", 'oop-plugin-template-solution'),
'bool1' => __("Yes, delete the damn data.", 'oop-plugin-template-solution'),
),
'example_int' => array(
'section' => 'misc',
'label' => __("Integer", 'oop-plugin-template-solution'),
'text' => __("An example for storing an integer value.", 'oop-plugin-template-solution'),
'type' => 'int',
),
'example_string' => array(
'section' => 'misc',
'label' => __("String", 'oop-plugin-template-solution'),
'text' => __("See how to set a string value.", 'oop-plugin-template-solution'),
'type' => 'string',
),
);
}
/**
* A filter to add a "Settings" link in this plugin's description
*
* NOTE: This method is automatically called by WordPress for each
* plugin being displayed on WordPress' Plugins admin page.
*
* @param array $links the links generated thus far
* @return array
*/
public function plugin_action_links($links) {
// Translation already in WP.
$links[] = '<a href="' . $this->hsc_utf8($this->page_options)
. '?page=' . self::ID . '">'
. $this->hsc_utf8(__('Settings')) . '</a>';
return $links;
}
/**
* Declares a menu item and callback for this plugin's settings page
*
* NOTE: This method is automatically called by WordPress when
* any admin page is rendered
*/
public function admin_menu() {
add_submenu_page(
$this->page_options,
$this->text_settings,
self::NAME,
$this->capability_required,
self::ID,
array(&$this, 'page_settings')
);
}
/**
* Declares the callbacks for rendering and validating this plugin's
* settings sections and fields
*
* NOTE: This method is automatically called by WordPress when
* any admin page is rendered
*/
public function admin_init() {
register_setting(
$this->option_name,
$this->option_name,
array(&$this, 'validate')
);
// Dynamically declares each section using the info in $sections.
foreach ($this->sections as $id => $section) {
add_settings_section(
self::ID . '-' . $id,
$this->hsc_utf8($section['title']),
array(&$this, $section['callback']),
self::ID
);
}
// Dynamically declares each field using the info in $fields.
foreach ($this->fields as $id => $field) {
add_settings_field(
$id,
$this->hsc_utf8($field['label']),
array(&$this, $id),
self::ID,
self::ID . '-' . $field['section']
);
}
}
/**
* The callback for rendering the settings page
* @return void
*/
public function page_settings() {
if (is_multisite()) {
// WordPress doesn't show the successs/error messages on
// the Network Admin screen, at least in version 3.3.1,
// so force it to happen for now.
include_once ABSPATH . 'wp-admin/options-head.php';
}
echo '<h2>' . $this->hsc_utf8($this->text_settings) . '</h2>';
echo '<form action="' . $this->hsc_utf8($this->form_action) . '" method="post">' . "\n";
settings_fields($this->option_name);
do_settings_sections(self::ID);
submit_button();
echo '</form>';
}
/**
* The callback for "rendering" the sections that don't have descriptions
* @return void
*/
public function section_blank() {
}
/**
* The callback for rendering the "Login Policies" section description
* @return void
*/
public function section_login() {
echo '<p>';
echo $this->hsc_utf8(__("An explanation of this section...", 'oop-plugin-template-solution'));
echo '</p>';
}
/**
* The callback for rendering the fields
* @return void
*
* @uses oop_plugin_template_solution_admin::input_int() for rendering
* text input boxes for numbers
* @uses oop_plugin_template_solution_admin::input_radio() for rendering
* radio buttons
* @uses oop_plugin_template_solution_admin::input_string() for rendering
* text input boxes for strings
*/
public function __call($name, $params) {
if (empty($this->fields[$name]['type'])) {
return;
}
switch ($this->fields[$name]['type']) {
case 'bool':
$this->input_radio($name);
break;
case 'int':
$this->input_int($name);
break;
case 'string':
$this->input_string($name);
break;
}
}
/**
* Renders the radio button inputs
* @return void
*/
protected function input_radio($name) {
echo $this->hsc_utf8($this->fields[$name]['text']) . '<br/>';
echo '<input type="radio" value="0" name="'
. $this->hsc_utf8($this->option_name)
. '[' . $this->hsc_utf8($name) . ']"'
. ($this->options[$name] ? '' : ' checked="checked"') . ' /> ';
echo $this->hsc_utf8($this->fields[$name]['bool0']);
echo '<br/>';
echo '<input type="radio" value="1" name="'
. $this->hsc_utf8($this->option_name)
. '[' . $this->hsc_utf8($name) . ']"'
. ($this->options[$name] ? ' checked="checked"' : '') . ' /> ';
echo $this->hsc_utf8($this->fields[$name]['bool1']);
}
/**
* Renders the text input boxes for editing integers
* @return void
*/
protected function input_int($name) {
echo '<input type="text" size="3" name="'
. $this->hsc_utf8($this->option_name)
. '[' . $this->hsc_utf8($name) . ']"'
. ' value="' . $this->hsc_utf8($this->options[$name]) . '" /> ';
echo $this->hsc_utf8($this->fields[$name]['text']
. ' ' . __('Default:', 'oop-plugin-template-solution') . ' '
. $this->options_default[$name] . '.');
}
/**
* Renders the text input boxes for editing strings
* @return void
*/
protected function input_string($name) {
echo '<input type="text" size="75" name="'
. $this->hsc_utf8($this->option_name)
. '[' . $this->hsc_utf8($name) . ']"'
. ' value="' . $this->hsc_utf8($this->options[$name]) . '" /> ';
echo '<br />';
echo $this->hsc_utf8($this->fields[$name]['text']
. ' ' . __('Default:', 'oop-plugin-template-solution') . ' '
. $this->options_default[$name] . '.');
}
/**
* Validates the user input
*
* NOTE: WordPress saves the data even if this method says there are
* errors. So this method sets any inappropriate data to the default
* values.
*
* @param array $in the input submitted by the form
* @return array the sanitized data to be saved
*/
public function validate($in) {
$out = $this->options_default;
if (!is_array($in)) {
// Not translating this since only hackers will see it.
add_settings_error($this->option_name,
$this->hsc_utf8($this->option_name),
'Input must be an array.');
return $out;
}
$gt_format = __("must be >= '%s',", 'oop-plugin-template-solution');
$default = __("so we used the default value instead.", 'oop-plugin-template-solution');
// Dynamically validate each field using the info in $fields.
foreach ($this->fields as $name => $field) {
if (!array_key_exists($name, $in)) {
continue;
}
if (!is_scalar($in[$name])) {
// Not translating this since only hackers will see it.
add_settings_error($this->option_name,
$this->hsc_utf8($name),
$this->hsc_utf8("'" . $field['label'])
. "' was not a scalar, $default");
continue;
}
switch ($field['type']) {
case 'bool':
if ($in[$name] != 0 && $in[$name] != 1) {
// Not translating this since only hackers will see it.
add_settings_error($this->option_name,
$this->hsc_utf8($name),
$this->hsc_utf8("'" . $field['label']
. "' must be '0' or '1', $default"));
continue 2;
}
break;
case 'int':
if (!ctype_digit($in[$name])) {
add_settings_error($this->option_name,
$this->hsc_utf8($name),
$this->hsc_utf8("'" . $field['label'] . "' "
. __("must be an integer,", 'oop-plugin-template-solution')
. ' ' . $default));
continue 2;
}
if (array_key_exists('greater_than', $field)
&& $in[$name] < $field['greater_than'])
{
add_settings_error($this->option_name,
$this->hsc_utf8($name),
$this->hsc_utf8("'" . $field['label'] . "' "
. sprintf($gt_format, $field['greater_than'])
. ' ' . $default));
continue 2;
}
break;
}
$out[$name] = $in[$name];
}
return $out;
}
}