-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass-cliowp-settings-page.php
634 lines (570 loc) · 18.7 KB
/
class-cliowp-settings-page.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
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
<?php
/**
* Plugin Name: ClioWP Settings Page
* Plugin URI: https://github.com/pontikis/cliowp-settings-page
* Description: Free WordPress plugin to generate Admin Settings Page
* Version: 1.0.0
* Author: Christos Pontikis
* Author URI: https://pontikis.net
* Text Domain: cliowp-settings-page
* Domain Path: /languages
* Requires PHP: 5.6.20
* Tested up to: 6.1
* License: GPLv2 or later
*
* @package ClioWP_Settings_Page
*/
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* ClioWP Settings Page plugin main class
*
* Before use this code in your own plugin:
* - Change the text domain (cliowp-settings-page) to your own text domain
* - Make the appropriate changes in parameters in the constructor
*/
class ClioWP_Settings_Page {
/**
* Settings Page title.
*
* @var string
*/
private $page_title;
/**
* Menu title.
*
* @var string
*/
private $menu_title;
/**
* Capability to access Settings page.
*
* @var string
*/
private $capability;
/**
* Menu slug.
*
* @var string
*/
private $menu_slug;
/**
* Settings form action.
*
* @var string
*/
private $form_action;
/**
* Option group.
*
* @var string
*/
private $option_group;
/**
* Constructor
*/
public function __construct() {
// parameters.
$this->page_title = esc_html__( 'Test Settings Page', 'cliowp-settings-page' );
$this->menu_title = esc_html__( 'Test Settings', 'cliowp-settings-page' );
$this->capability = 'manage_options';
$this->menu_slug = 'cliowp-settings-page-slug';
$this->form_action = 'options.php';
$this->option_group = 'cliowp_sp_plugin';
// actions.
add_action( 'admin_menu', array( $this, 'add_settings_page' ) );
add_action( 'admin_init', array( $this, 'add_settings' ) );
add_action( 'init', array( $this, 'load_languages' ) );
}
/**
* Adds a submenu page to the Settings main menu.
*/
public function add_settings_page() {
/**
* Params for add_options_page
*
* @param string $page_title The text to be displayed in the title tags of the page when the menu is selected.
* @param string $menu_title The text to be used for the menu.
* @param string $capability The capability required for this menu to be displayed to the user.
* @param string $menu_slug The slug name to refer to this menu by (should be unique for this menu).
* @param callable $callback Optional. The function to be called to output the content for this page.
* @param int $position Optional. The position in the menu order this item should appear.
* @return string|false The resulting page's hook_suffix, or false if the user does not have the capability required.
*/
add_options_page(
$this->page_title,
$this->menu_title,
$this->capability,
$this->menu_slug,
array( $this, 'settings_page_html' )
);
}
/**
* Compose settings
*/
public function add_settings() {
// Define Sections ----------------------------------------------------.
/**
* Adds a new section to a settings page.
*
* Part of the Settings API. Use this to define new settings sections for an admin page.
* Show settings sections in your admin page callback function with do_settings_sections().
* Add settings fields to your section with add_settings_field().
*
* The $callback argument should be the name of a function that echoes out any
* content you want to show at the top of the settings section before the actual
* fields. It can output nothing if you want.
*
* @param string $id Slug-name to identify the section. Used in the 'id' attribute of tags.
* @param string $title Formatted title of the section. Shown as the heading for the section.
* @param callable $callback Function that echos out any content at the top of the section (between heading and fields).
* @param string $page The slug-name of the settings page on which to show the section. Built-in pages include
* 'general', 'reading', 'writing', 'discussion', 'media', etc. Create your own using
* add_options_page();
*/
add_settings_section(
'cliowp_settings_page_section1',
esc_html__( 'Section A', 'cliowp-settings-page' ),
null,
$this->menu_slug
);
add_settings_section(
'cliowp_settings_page_section2',
esc_html__( 'Section B', 'cliowp-settings-page' ),
null,
$this->menu_slug
);
// Input text field ---------------------------------------------------.
/**
* Adds a new field to a section of a settings page.
*
* Part of the Settings API. Use this to define a settings field that will show
* as part of a settings section inside a settings page. The fields are shown using
* do_settings_fields() in do_settings_sections().
*
* The $callback argument should be the name of a function that echoes out the
* HTML input tags for this setting field. Use get_option() to retrieve existing
* values to show.
*
* @param string $id Slug-name to identify the field. Used in the 'id' attribute of tags.
* @param string $title Formatted title of the field. Shown as the label for the field
* during output.
* @param callable $callback Function that fills the field with the desired form inputs. The
* function should echo its output.
* @param string $page The slug-name of the settings page on which to show the section
* (general, reading, writing, ...).
* @param string $section Optional. The slug-name of the section of the settings page
* in which to show the box. Default 'default'.
* @param array $args {
* Optional. Extra arguments used when outputting the field.
*
* @type string $label_for When supplied, the setting title will be wrapped
* in a `<label>` element, its `for` attribute populated
* with this value.
* @type string $class CSS Class to be added to the `<tr>` element when the
* field is output.
* }
*/
add_settings_field(
'cliowp_sp_input1',
esc_html__( 'Input1 Label', 'cliowp-settings-page' ),
array( $this, 'input1_html' ),
$this->menu_slug,
'cliowp_settings_page_section1'
);
/**
* Registers a setting and its data.
*
* @param string $option_group A settings group name. Should correspond to an allowed option key name.
* Default allowed option key names include 'general', 'discussion', 'media',
* 'reading', 'writing', and 'options'.
* @param string $option_name The name of an option to sanitize and save.
* @param array $args {
* Data used to describe the setting when registered.
*
* @type string $type The type of data associated with this setting.
* Valid values are 'string', 'boolean', 'integer', 'number', 'array', and 'object'.
* @type string $description A description of the data attached to this setting.
* @type callable $sanitize_callback A callback function that sanitizes the option's value.
* @type bool|array $show_in_rest Whether data associated with this setting should be included in the REST API.
* When registering complex settings, this argument may optionally be an
* array with a 'schema' key.
* @type mixed $default Default value when calling `get_option()`.
* }
*/
register_setting(
$this->option_group,
'cliowp_sp_input1',
array(
'sanitize_callback' => array( $this, 'sanitize_input1' ),
'default' => 'input1 test',
)
);
// Date field ---------------------------------------------------------.
add_settings_field(
'cliowp_sp_date1',
esc_html__( 'Date1 Label', 'cliowp-settings-page' ),
array( $this, 'date1_html' ),
$this->menu_slug,
'cliowp_settings_page_section1'
);
register_setting(
$this->option_group,
'cliowp_sp_date1',
array(
'sanitize_callback' => 'sanitize_text_field',
)
);
// DateTime field -----------------------------------------------------.
add_settings_field(
'cliowp_sp_datetime1',
esc_html__( 'Datetime1 Label', 'cliowp-settings-page' ),
array( $this, 'datetime1_html' ),
$this->menu_slug,
'cliowp_settings_page_section1'
);
register_setting(
$this->option_group,
'cliowp_sp_datetime1',
array(
'sanitize_callback' => 'sanitize_text_field',
)
);
// Password field -----------------------------------------------------.
add_settings_field(
'cliowp_sp_password1',
esc_html__( 'Password1 Label', 'cliowp-settings-page' ),
array( $this, 'password1_html' ),
$this->menu_slug,
'cliowp_settings_page_section1'
);
register_setting(
$this->option_group,
'cliowp_sp_password1',
array(
'sanitize_callback' => array( $this, 'encrypt_password1' ),
)
);
// Number field -------------------------------------------------------.
add_settings_field(
'cliowp_sp_number1',
esc_html__( 'Number1 Label', 'cliowp-settings-page' ),
array( $this, 'number1_html' ),
$this->menu_slug,
'cliowp_settings_page_section1',
array(
'min' => 1,
'max' => 10,
'step' => 1,
)
);
register_setting(
$this->option_group,
'cliowp_sp_number1',
);
// Select field -------------------------------------------------------.
add_settings_field(
'cliowp_sp_select1',
esc_html__( 'Select1 Label', 'cliowp-settings-page' ),
array( $this, 'select1_html' ),
$this->menu_slug,
'cliowp_settings_page_section1'
);
register_setting(
$this->option_group,
'cliowp_sp_select1',
array(
'sanitize_callback' => array( $this, 'sanitize_select1' ),
'default' => '1',
)
);
// Checkbox field -----------------------------------------------------.
add_settings_field(
'cliowp_sp_checkbox1',
esc_html__( 'Checkbox1 Label', 'cliowp-settings-page' ),
array( $this, 'checkbox1_html' ),
$this->menu_slug,
'cliowp_settings_page_section1'
);
register_setting(
$this->option_group,
'cliowp_sp_checkbox1',
array(
'sanitize_callback' => 'sanitize_text_field',
'default' => '1',
)
);
// MultiSelect field --------------------------------------------------.
add_settings_field(
'cliowp_sp_multiselect1',
esc_html__( 'MultiSelect1 Label', 'cliowp-settings-page' ),
array( $this, 'multiselect1_html' ),
$this->menu_slug,
'cliowp_settings_page_section2'
);
register_setting(
$this->option_group,
'cliowp_sp_multiselect1',
);
// Textarea field -----------------------------------------------------.
add_settings_field(
'cliowp_sp_textarea1',
esc_html__( 'Textarea1 Label', 'cliowp-settings-page' ),
array( $this, 'textarea1_html' ),
$this->menu_slug,
'cliowp_settings_page_section2',
array(
'rows' => 4,
'cols' => 30,
)
);
register_setting(
$this->option_group,
'cliowp_sp_textarea1',
array(
'sanitize_callback' => 'sanitize_textarea_field',
)
);
// Color field --------------------------------------------------------.
add_settings_field(
'cliowp_sp_color1',
esc_html__( 'Color1 Label', 'cliowp-settings-page' ),
array( $this, 'color1_html' ),
$this->menu_slug,
'cliowp_settings_page_section2'
);
register_setting(
$this->option_group,
'cliowp_sp_color1',
);
// WYSIWYG editor field -----------------------------------------------.
add_settings_field(
'cliowp_sp_editor1',
esc_html__( 'Editor1 Label', 'cliowp-settings-page' ),
array( $this, 'editor1_html' ),
$this->menu_slug,
'cliowp_settings_page_section2',
);
register_setting(
$this->option_group,
'cliowp_sp_editor1',
array(
'sanitize_callback' => 'wp_kses_post',
)
);
}
/**
* Create HTML for input1 field
*/
public function input1_html() { ?>
<input type="text" name="cliowp_sp_input1" value="<?php echo esc_attr( get_option( 'cliowp_sp_input1' ) ); ?>">
<?php
}
/**
* Sanitize input1
*
* @param string $input The input value.
*/
public function sanitize_input1( $input ) {
if ( true === empty( trim( $input ) ) ) {
add_settings_error(
'cliowp_sp_input1',
'cliowp_sp_input1_error',
esc_html__( 'Input1 cannot be empty', 'cliowp-settings-page' ),
);
return get_option( 'cliowp_sp_input1' );
}
return sanitize_text_field( $input );
}
/**
* Create HTML for date1 field
*/
public function date1_html() {
?>
<input type="date" name="cliowp_sp_date1" value="<?php echo esc_attr( get_option( 'cliowp_sp_date1' ) ); ?>">
<?php
}
/**
* Create HTML for datetime1 field
*/
public function datetime1_html() {
?>
<input type="datetime-local" name="cliowp_sp_datetime1" value="<?php echo esc_attr( get_option( 'cliowp_sp_datetime1' ) ); ?>">
<?php
}
/**
* Create HTML for password1 field
*
* This is the only field that does not retrieve the value from the database
* (because a hash is stored and not that original value).
* Check the wp_options table to view what is saved as a hash.
*/
public function password1_html() {
?>
<input type="password" name="cliowp_sp_password1" value="">
<?php
}
/**
* Encrypt password1
*
* @param string $input The plain password.
*/
public function encrypt_password1( $input ) {
return wp_hash_password( $input );
}
/**
* Create HTML for number1 field
*
* @param array $args Arguments passed.
*/
public function number1_html( array $args ) {
?>
<input type="number" name="cliowp_sp_number1"
min="<?php echo esc_html( $args['min'] ); ?>"
max="<?php echo esc_html( $args['max'] ); ?>"
step="<?php echo esc_html( $args['step'] ); ?>"
value="<?php echo esc_attr( get_option( 'cliowp_sp_number1' ) ); ?>">
<?php
}
/**
* Create HTML for select1 field
*/
public function select1_html() {
?>
<select name="cliowp_sp_select1">
<option value="1" <?php selected( get_option( 'cliowp_sp_select1' ), '1' ); ?>><?php esc_attr_e( 'Option1', 'cliowp-settings-page' ); ?></option>
<option value="2" <?php selected( get_option( 'cliowp_sp_select1' ), '2' ); ?>><?php esc_attr_e( 'Option2', 'cliowp-settings-page' ); ?></option>
<option value="3" <?php selected( get_option( 'cliowp_sp_select1' ), '3' ); ?>><?php esc_attr_e( 'Option3', 'cliowp-settings-page' ); ?></option>
</select>
<?php
}
/**
* Sanitize select1
*
* @param string $input The selected value.
*/
public function sanitize_select1( $input ) {
$valid_input = array( '1', '2', '3' );
if ( false === in_array( $input, $valid_input, true ) ) {
add_settings_error(
'cliowp_sp_select1',
'cliowp_sp_select1_error',
esc_html__( 'Invalid option for Select1', 'cliowp-settings-page' ),
);
return get_option( 'cliowp_sp_select1' );
}
return $input;
}
/**
* Create HTML for checkbox1 field
*/
public function checkbox1_html() {
?>
<input type="checkbox" name="cliowp_sp_checkbox1" value="1" <?php checked( get_option( 'cliowp_sp_checkbox1' ), '1' ); ?>>
<?php
}
/**
* Create HTML for multiselect1 field
*/
public function multiselect1_html() {
$selected_values = get_option( 'cliowp_sp_multiselect1' );
?>
<select name="cliowp_sp_multiselect1[]" multiple>
<option value="1" <?php echo esc_html( $this->cliowp_multiselected( $selected_values, '1' ) ); ?>><?php esc_attr_e( 'MultiSelect Option1', 'cliowp-settings-page' ); ?></option>
<option value="2" <?php echo esc_html( $this->cliowp_multiselected( $selected_values, '2' ) ); ?>><?php esc_attr_e( 'MultiSelect Option2', 'cliowp-settings-page' ); ?></option>
<option value="3" <?php echo esc_html( $this->cliowp_multiselected( $selected_values, '3' ) ); ?>><?php esc_attr_e( 'MultiSelect Option3', 'cliowp-settings-page' ); ?></option>
<option value="4" <?php echo esc_html( $this->cliowp_multiselected( $selected_values, '4' ) ); ?>><?php esc_attr_e( 'MultiSelect Option4', 'cliowp-settings-page' ); ?></option>
<option value="5" <?php echo esc_html( $this->cliowp_multiselected( $selected_values, '5' ) ); ?>><?php esc_attr_e( 'MultiSelect Option5', 'cliowp-settings-page' ); ?></option>
</select>
<?php
}
/**
* Utility function to check if value is selected
*
* @param array|string $selected_values Array (or empty string) returned by get_option().
* @param string $current_value Value to check if it is selected.
*
* @return string
*/
private function cliowp_multiselected(
$selected_values,
string $current_value
): string {
if ( is_array( $selected_values ) && in_array( $current_value, $selected_values, true ) ) {
return 'selected';
}
return '';
}
/**
* Create HTML for textarea1 field
*
* @param array $args Arguments passed.
*/
public function textarea1_html( array $args ) {
?>
<textarea
name="cliowp_sp_textarea1"
rows="<?php echo esc_html( $args['rows'] ); ?>"
cols="<?php echo esc_html( $args['cols'] ); ?>"><?php echo esc_attr( get_option( 'cliowp_sp_textarea1' ) ); ?></textarea>
<?php
}
/**
* Create HTML for color1 field
*/
public function color1_html() {
?>
<input type="color" name="cliowp_sp_color1" value="<?php echo esc_attr( get_option( 'cliowp_sp_color1' ) ); ?>">
<?php
}
/**
* Create HTML for editor1 field
*/
public function editor1_html() {
wp_editor(
wp_kses_post( get_option( 'cliowp_sp_editor1' ) ),
'cliowp_sp_editor1',
);
}
/**
* Create Settings Page HTML
*/
public function settings_page_html() {
?>
<div class="wrap">
<h1><?php echo esc_attr( $this->page_title ); ?>
</h1>
<form action="<?php echo esc_attr( $this->form_action ); ?>" method="POST">
<?php
settings_fields( $this->option_group );
do_settings_sections( $this->menu_slug );
submit_button();
?>
</form>
</div>
<?php
}
/**
* Loads plugin's translated strings.
*/
public function load_languages() {
/**
* Params of load_plugin_textdomain
*
* @param string $domain Unique identifier for retrieving translated strings
* @param string|false $deprecated Optional. Deprecated. Use the $plugin_rel_path parameter instead.
* Default false.
* @param string|false $plugin_rel_path Optional. Relative path to WP_PLUGIN_DIR where the .mo file resides.
* Default false.
* @return bool True when textdomain is successfully loaded, false otherwise.
*/
load_plugin_textdomain(
'cliowp-settings-page',
false,
dirname( plugin_basename( __FILE__ ) ) . '/languages'
);
}
}
// instantiate ClioWP Settings Page plugin main class.
$cliowp_settings_page = new ClioWP_Settings_Page();