-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclass-option-page.php
More file actions
420 lines (395 loc) · 14.4 KB
/
class-option-page.php
File metadata and controls
420 lines (395 loc) · 14.4 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
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
<?php
/**
* Class used to create a new options page.
*
* @package WP_Options_Page
* @author Mikael Fourré
* @version 1.4.0
* @see https://github.com/FmiKL/wp-options-page
*/
class Option_Page {
/**
* Path to the assets.
*
* @var string
* @since 1.0.0
* @see Option_Page::enqueues_scripts()
*/
private const ASSETS_PATH = '/wp-options-page/assets';
/**
* Title of the page.
*
* @var string
* @since 1.0.0
*/
private $title;
/**
* Unique identifier needed to register the settings.
*
* @var string
* @since 1.0.0
*/
private $key;
/**
* Fields to create.
*
* @var array<string, array>
* @since 1.0.0
* @see Option_Page::add_field()
*/
private $fields = array();
/**
* Sections to group fields.
*
* @var array<string, array>
* @since 1.3.0
* @see Option_Page::add_section()
*/
private $sections = array();
/**
* @param string $title Title of the page.
* @param string $key Unique identifier needed to register the settings.
* @since 1.0.0
*/
public function __construct( $title, $key ) {
$this->title = $title;
$this->key = $key;
$this->add_hooks();
}
/**
* Hooks the methods to the appropriate actions.
*
* @since 1.0.0
*/
private function add_hooks() {
add_action( 'admin_menu', array( $this, 'add_page' ) );
add_action( 'admin_init', array( $this, 'register_setting' ) );
add_action( 'admin_enqueue_scripts', array( $this, 'enqueues_scripts' ) );
}
/**
* Adds a new section.
*
* Sections allow you to group fields visually on the options page.
*
* @param string $id Unique ID of the section.
* @param string $title Title of the section.
* @param string $description Optional description displayed under the section title.
* @since 1.3.0
*/
public function add_section( $id, $title, $description = '' ) {
$this->sections[ $id ] = array(
'title' => $title,
'description' => $description,
);
}
/**
* Adds a new field.
*
* @param string $type Type of the field.
* @param string $name Name of the field.
* @param array $options Additional options for the field. Can include label and placeholder.
* If the placeholder contains a reserved word (e.g., "image", "avatar" or "icon"),
* a double-click will trigger the WordPress media library to open directly.
* If the placeholder contains an indication of an image size "{size}px", the selected image will be the one closest to this size.
* @since 1.0.0
*/
public function add_field( $type, $name, $options = array() ) {
$types = array( 'type' => $type );
$this->fields[ $name ] = array_merge( $types, $options );
}
/**
* Adds a page to the WordPress admin area.
*
* @since 1.0.0
* @link https://developer.wordpress.org/reference/functions/add_options_page/
*/
public function add_page() {
add_options_page( $this->title, $this->title, 'manage_options', $this->key, array( $this, 'render_form' ) );
}
/**
* Renders the form for the options page.
*
* @since 1.0.0
*/
public function render_form() {
?>
<div class="wrap">
<h1><?php echo esc_html( $this->title ); ?></h1>
<form method="post" action="options.php">
<?php settings_fields( $this->key ); ?>
<?php
if ( ! empty( $this->sections ) ) :
foreach ( $this->sections as $section_id => $section ) :
$section_fields = array();
foreach ( $this->fields as $name => $field ) {
if ( isset( $field['section'] ) && $field['section'] === $section_id ) {
$section_fields[ $name ] = $field;
}
}
if ( empty( $section_fields ) ) {
continue;
}
?>
<h2><?php echo esc_html( $section['title'] ); ?></h2>
<?php if ( ! empty( $section['description'] ) ) : ?>
<p class="description"><?php echo esc_html( $section['description'] ); ?></p>
<?php endif; ?>
<table class="form-table" role="presentation">
<tbody>
<?php foreach ( $section_fields as $name => $field ) : ?>
<tr>
<th scope="row">
<?php if ( ! empty( $field['label'] ) ) : ?>
<label for="<?php echo esc_attr( $name ); ?>"><?php echo esc_html( $field['label'] ); ?></label>
<?php endif; ?>
</th>
<td>
<?php $this->render_field_by_type( $name, $field ); ?>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php
endforeach;
$unsectioned = array();
foreach ( $this->fields as $name => $field ) {
if ( empty( $field['section'] ) || ! isset( $this->sections[ $field['section'] ] ) ) {
$unsectioned[ $name ] = $field;
}
}
if ( ! empty( $unsectioned ) ) : ?>
<table class="form-table" role="presentation">
<tbody>
<?php foreach ( $unsectioned as $name => $field ) : ?>
<tr>
<th scope="row">
<?php if ( ! empty( $field['label'] ) ) : ?>
<label for="<?php echo esc_attr( $name ); ?>"><?php echo esc_html( $field['label'] ); ?></label>
<?php endif; ?>
</th>
<td>
<?php $this->render_field_by_type( $name, $field ); ?>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php endif; ?>
<?php else : ?>
<table class="form-table" role="presentation">
<tbody>
<?php foreach ( $this->fields as $name => $field ) : ?>
<tr>
<th scope="row">
<?php if ( ! empty( $field['label'] ) ) : ?>
<label for="<?php echo esc_attr( $name ); ?>"><?php echo esc_html( $field['label'] ); ?></label>
<?php endif; ?>
</th>
<td>
<?php $this->render_field_by_type( $name, $field ); ?>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php endif; ?>
<?php submit_button(); ?>
</form>
</div>
<?php
}
/**
* Renders a field by its declared type.
*
* @param string $name Field name.
* @param array $field Field configuration.
* @since 1.3.0
*/
private function render_field_by_type( $name, $field ) {
switch ( $field['type'] ) {
case 'textarea':
$this->render_textarea_field( $name, $field );
break;
case 'select':
$this->render_select_field( $name, $field );
break;
case 'checkbox':
$this->render_checkbox_field( $name );
break;
case 'color':
$this->render_color_field( $name );
break;
default:
$this->render_input_field( $name, $field );
break;
}
}
/**
* Renders the textarea field.
*
* @param string $name Name of the field.
* @param array $field Field to render a textarea for.
* @since 1.2.0
*/
private function render_textarea_field( $name, $field ) {
?>
<textarea
id="<?php echo esc_attr( $name ); ?>"
class="regular-text"
name="<?php echo esc_attr( $name ); ?>"
placeholder="<?php echo esc_attr( $field['placeholder'] ?? '' ); ?>"
rows="<?php echo esc_attr( $field['rows'] ?? '10' ); ?>"
><?php echo esc_textarea( get_option( $name ) ); ?></textarea>
<?php
}
/**
* Renders the select field.
*
* @param string $name Name of the field.
* @param array $field Field to render a select for.
* @since 1.1.0
*/
private function render_select_field( $name, $field ) {
$current_value = get_option( $name );
$label = $field['label'] ?? '';
$choices = $field['choices'] ?? array();
?>
<select
id="<?php echo esc_attr( $name ); ?>"
class="regular-text"
name="<?php echo esc_attr( $name ); ?>"
>
<?php if ( $label ) : ?>
<option value="" <?php echo $current_value ? '' : 'selected'; ?> disabled><?php echo esc_html( $label ); ?></option>
<?php endif; ?>
<?php foreach ( $choices as $choice ) : ?>
<option value="<?php echo esc_attr( $choice ); ?>" <?php selected( $current_value, $choice ); ?>>
<?php echo esc_html( $choice ); ?>
</option>
<?php endforeach; ?>
</select>
<?php
}
/**
* Renders the color field.
*
* @param string $name Name of the field.
* @since 1.4.0
*/
private function render_color_field( $name ) {
?>
<input
type="color"
id="<?php echo esc_attr( $name ); ?>"
name="<?php echo esc_attr( $name ); ?>"
value="<?php echo esc_attr( get_option( $name, '#000000' ) ); ?>"
>
<?php
}
/**
* Renders the checkbox field.
*
* @param string $name Name of the field.
* @since 1.0.1
*/
private function render_checkbox_field( $name ) {
?>
<input
type="checkbox"
id="<?php echo esc_attr( $name ); ?>"
name="<?php echo esc_attr( $name ); ?>"
value="1"
<?php checked( 1, get_option( $name ), true ); ?>
>
<?php
}
/**
* Renders the input field.
*
* @param string $name Name of the field.
* @param array $field Field to render an input for.
* @since 1.0.1
*/
private function render_input_field( $name, $field ) {
?>
<input
type="<?php echo esc_attr( $field['type'] ?? 'text' ); ?>"
id="<?php echo esc_attr( $name ); ?>"
class="regular-text"
name="<?php echo esc_attr( $name ); ?>"
value="<?php echo esc_attr( get_option( $name ) ); ?>"
placeholder="<?php echo esc_attr( $field['placeholder'] ?? '' ); ?>"
>
<?php
}
/**
* Sanitizes a field value based on its type.
*
* @param mixed $input Raw input value.
* @param array $field Field configuration.
* @return string Sanitized value or empty string on invalid input.
* @since 1.3.2
*/
private function sanitize_field_value( $input, $field ) {
if ( is_array( $input ) ) {
return '';
}
$type = $field['type'] ?? 'text';
$value = trim( (string) $input );
switch ( $type ) {
case 'checkbox':
return ( '1' === $value ) ? '1' : '0';
case 'email':
return sanitize_email( $value );
case 'url':
return esc_url_raw( $value );
case 'number':
return is_numeric( $value ) ? $value : '';
case 'color':
return sanitize_hex_color( $value ) ?: '';
case 'textarea':
return sanitize_textarea_field( $value );
case 'select':
$choices = is_array( $field['choices'] ?? null ) ? array_values( $field['choices'] ) : array();
return in_array( $value, $choices, true ) ? $value : '';
default:
return sanitize_text_field( $value );
}
}
/**
* Enqueues the necessary scripts.
*
* @since 1.0.0
*/
public function enqueues_scripts() {
wp_enqueue_media();
if ( ! wp_script_is( 'field-media', 'registered' ) ) {
wp_enqueue_script( 'field-media', get_template_directory_uri() . self::ASSETS_PATH . '/js/field-media.js', array(), null, true );
}
}
/**
* Registers the settings.
*
* @since 1.0.0
* @link https://developer.wordpress.org/reference/functions/register_setting/
*/
public function register_setting() {
foreach ( $this->fields as $name => $field ) {
register_setting( $this->key, $name, array(
'sanitize_callback' => function ( $input ) use ( $name, $field ) {
if ( null === $input || '' === $input ) {
delete_option( $name );
return false;
}
$sanitized = $this->sanitize_field_value( $input, $field );
if ( '' === $sanitized ) {
delete_option( $name );
return false;
}
return $sanitized;
}
) );
}
}
}