This repository has been archived by the owner on Dec 5, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheasy-settings.php
506 lines (475 loc) · 14.1 KB
/
easy-settings.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
<?php
/**
* Plugin Name: Easy Settings
* Plugin URI: https://github.com/mrcsmcln/easy-settings
* Description: Easily create options pages without having to use the Settings
* API.
* Author: Marcus McLean
* Version: 0.5
* Author URI: https://www.odesk.com/users/%7E012eaf926aadbd170e
* License: GPL2
*
* @package Easy_Settings
* @version 0.5
*/
class Easy_Settings {
/**
* Whether Easy Settings will be run from a theme or a plugin.
*
* @since Easy Settings 0.5
* @var string
*/
// const TYPE = 'plugin';
const TYPE = 'theme';
/**
* The directory in which the settings will be stored. Applicable only when
* Easy_Settings::TYPE is 'theme'.
*
* @since Easy Settings 0.5
* @var string
*/
const ROOT = '/easy-settings';
/**
* The current submenu.
*
* @since Easy Settings 0.5
* @var string
*/
public $current_submenu;
/**
* The current submenu page.
*
* @since Easy Settings 0.5
* @var string
*/
public $current_page;
/**
* The current option.
*
* @since Easy Settings 0.5
* @var string
*/
public $current_option;
/**
* The current settings section.
*
* @since Easy Settings 0.5
* @var string
*/
public $current_section;
/**
* The current settings field.
*
* @since Easy Settings 0.5
* @var string
*/
public $current_field;
/**
* The settings fields for the current option.
*
* @since Easy Settings 0.5
* @var array
*/
public $fields;
/**
* The errors generated by silly programming.
*
* @since Easy Settings 0.5
* @var array
*/
public $errors;
/**
* Easy_Settings constructor.
*
* @since Easy Settings 0.5
*/
public function __construct() {
if ( 'theme' !== self::TYPE && 'plugin' !== self::TYPE ) {
$this->error( "<code>Easy_Settings::TYPE</code> must be either <code>'plugin'</code> or <code>'theme'</code>." );
} else {
add_action( 'admin_menu', array( $this, 'admin_menu' ) );
add_action( 'admin_init', array( $this, 'admin_init' ) );
}
}
/**
* Displays the submenu page.
*
* @since Easy Settings 0.5
*/
public function submenu_page() {
$this->current_submenu = $this->get_current_submenu();
$this->current_page = $this->get_current_page();
$this->current_option = "mjm_{$this->current_submenu}_$this->current_page";
$this->fields = get_option( $this->current_option );
$page_file = $this->get_file( "$this->current_submenu-pages/$this->current_page/index.php" );
if ( $page_file ) {
include $page_file;
} else { ?>
<div class="wrap">
<h2><?php echo $this->format_title( $this->current_page ); ?></h2>
<form action="options.php" method="post"><?php
settings_fields( $this->current_option );
do_settings_sections( 'mjm-' . $this->current_page );
submit_button();
?></form>
</div>
<?php }
}
/**
* Displays the submenu page for the current submenu.
*
* @since Easy Settings 0.5
*/
public function submenu_pages() {
$default_capability = 'edit_theme_options';
$submenu_dir = $this->get_dir( $this->current_submenu );
switch ( $this->current_submenu ) {
case 'dashboard-pages':
$parent_slug = 'index.php';
break;
case 'posts-pages':
$parent_slug = 'edit.php';
$capability = 'edit_posts';
break;
case 'media-pages':
$parent_slug = 'upload.php';
$capability = 'upload_files';
break;
case 'comments-pages':
$parent_slug = 'edit.php?post_type=page';
$capability = 'edit_pages';
break;
case 'theme-pages':
$capability = 'edit_theme_options';
$parent_slug = 'themes.php';
break;
case 'plugins-pages':
$parent_slug = 'plugins.php';
$capability = 'activate_plugins';
break;
case 'users-pages':
$parent_slug = 'users.php';
$capability = 'promote_users';
break;
case 'management-pages':
$parent_slug = 'tools.php';
$capability = 'manage_options';
break;
case 'options-pages':
$parent_slug = 'options-general.php';
$capability = 'manage_options';
break;
case 'settings-pages': // non-standard
$parent_slug = 'settings.php';
$capability = 'manage_options';
break;
default:
$parent_slug = 'edit.php?post_type=' . substr( $this->current_submenu, 0, -6 );
$capability = 'edit_posts';
}
$default_headers = array(
'PageTitle' => 'Page Title',
'MenuTitle' => 'Menu Title',
'Capability' => 'Capability',
);
foreach ( preg_grep('/^[^\.]/', scandir( $submenu_dir ) ) as &$page ) {
$page_dir = $this->get_dir( "$this->current_submenu/$page" );
if ( $page_dir ) {
$page_file = $this->get_file( "$this->current_submenu/$page/index.php" );
if ( $page_file ) {
$headers = get_file_data( $page_file, $default_headers );
}
$page_title = empty( $headers['PageTitle'] ) ? $this->format_title( $page ) : $headers['PageTitle'];
$menu_title = empty( $headers['MenuTitle'] ) ? $this->format_title( $page ) : $headers['MenuTitle'];
$capability = empty( $headers['Capability'] ) ? $default_capability : $headers['Capability'];
add_submenu_page( $parent_slug, $page_title, $menu_title, $capability, "mjm-$page", array( $this, 'submenu_page' ) );
}
}
unset( $page );
}
/**
* Scans the admin menu for submenu pages.
*
* @since Easy Settings 0.5
*/
public function admin_menu() {
$admin_dir = $this->get_dir( '' );
if ( $admin_dir ) {
foreach ( scandir( $admin_dir ) as &$submenu ) {
$submenu_dir = $this->get_dir( $submenu );
if ( $submenu_dir && '-pages' === substr( $submenu, -6 ) ) {
$this->current_submenu = $submenu;
$this->submenu_pages( $submenu );
}
}
unset( $submenu );
}
}
/**
* Displays the current settings field.
*
* @since Easy Settings 0.5
*
* @param string $field The current settings field.
*/
public function settings_field( $field ) {
$this->current_field = $field['field'];
$field_file = $this->get_file( "$this->current_submenu-pages/$this->current_page/$this->current_section/$this->current_field.php" );
if ( $field_file && filesize( $field_file ) ) {
include $field_file;
} else { ?>
<input type="text" id="<?php echo esc_attr( $this->current_field ); ?>" name="<?php echo esc_attr( "$this->current_option[$this->current_field]" ); ?>" value="<?php echo isset( $this->fields[ $this->current_field ] ) ? esc_attr( $this->fields[ $this->current_field ] ) : ''; ?>">
<?php }
}
/**
* Displays the current settings section.
*
* @since Easy Settings 0.5
*
* @param string $section The current settings section.
*/
public function settings_section( $section ) {
$this->current_section = $section['id'];
$section_file = $this->get_file( "$this->current_submenu-pages/$this->current_page/$this->current_section/index.php" );
if ( $section_file ) {
include $section_file;
}
}
/**
* Adds settings fields to the current settings section.
*
* @since Easy Settings 0.5
*/
public function settings_fields() {
$default_headers = array(
'Title' => 'Title',
'LabelFor' => 'Label For',
);
$section_dir = $this->get_dir( "$this->current_submenu/$this->current_page/$this->current_section" );
foreach ( scandir( $section_dir ) as &$field ) {
$field_file = $this->get_file( "$this->current_submenu/$this->current_page/$this->current_section/$field" );
if ( $field_file && 'php' === pathinfo( $field_file, PATHINFO_EXTENSION ) && 'index.php' !== $field ) {
$headers = get_file_data( $field_file, $default_headers );
$id = substr( $field, 0, -4 );
$title = empty( $headers['Title'] ) ? $this->format_title( $id ) : $headers['Title'];
$label_for = $headers['LabelFor'];
add_settings_field( $id, $title, array( $this, 'settings_field' ), "mjm-$this->current_page", $this->current_section, array(
'field' => substr( $field, 0, -4 ),
'label_for' => $headers['LabelFor'],
) );
}
}
unset( $field );
}
/**
* Adds settings sections to the current submenu page.
*
* @since Easy Settings 0.5
*/
public function settings_sections() {
$default_headers = array(
'Title' => 'Title',
);
$page_dir = $this->get_dir( "$this->current_submenu/$this->current_page" );
foreach ( scandir( $page_dir ) as &$section ) {
$section_dir = $this->get_dir( "$this->current_submenu/$this->current_page/$section" );
if ( $section_dir && '.' !== $section[0] ) {
$this->current_section = $section;
$section_file = $this->get_file( "$section_dir/index.php" );
if ( $section_file ) {
$headers = get_file_data( $section_file, $default_headers );
}
$title = empty( $headers['Title'] ) ? $this->format_title( $this->current_section ) : $headers['Title'];
add_settings_section( $this->current_section, $title, array( $this, 'settings_section' ), "mjm-$this->current_page" );
$this->settings_fields();
}
}
unset( $section );
}
/**
* Registers settings for the current page.
*
* @since Easy Settings 0.5
*/
public function admin_init() {
$default_headers = array(
'SanitizeCallback' => 'Sanitize Callback',
);
$admin_dir = $this->get_dir( '' );
if ( $admin_dir ) {
foreach ( scandir( $admin_dir ) as &$submenu ) {
$submenu_dir = $this->get_dir( $submenu );
if ( $submenu_dir && '-pages' === substr( $submenu, -6 ) ) {
$this->current_submenu = $submenu;
foreach ( scandir( $submenu_dir ) as &$page ) {
$page_dir = $this->get_dir( "$submenu/$page" );
if ( $page_dir && '.' !== $page[0] ) {
$this->current_page = $page;
$page_file = $this->get_file( "$submenu/$page/index.php" );
if ( $page_file ) {
$headers = get_file_data( $page_file, $default_headers );
}
if ( !empty( $headers['SanitizeCallback'] ) ) {
if ( function_exists( $headers['SanitizeCallback'] ) ) {
$sanitize_callback = $headers['SanitizeCallback'];
} else {
$this->error( "The function <code>{$headers['SanitizeCallback']}</code> does not exist." );
$sanitize_callback = array( $this, 'default_sanitize_callback' );
}
} else {
$sanitize_callback = array( $this, 'default_sanitize_callback' );
}
$option = 'mjm_' . substr( $submenu, 0, -6 ) . "_$page";
register_setting( $option, $option, $sanitize_callback );
$this->settings_sections();
}
}
unset( $page );
}
}
unset( $submenu );
}
}
/**
* Formats an ID as a title.
*
* @since Easy Settings 0.5
*
* @param string $id The ID to format.
*/
public function format_title( $id ) {
return ucwords( str_replace( array ( '-', '_' ), ' ', $id ) );
}
/**
* Gets the current page.
*
* @since Easy Settings 0.5
*/
public function get_current_page() {
$current_screen = get_current_screen();
$base = explode( 'mjm-', $current_screen->base, 2 );
return $base[1];
}
/**
* Gets the current submenu.
*
* @since Easy Settings 0.5
*/
public function get_current_submenu() {
$current_screen = get_current_screen();
switch ( $current_screen->parent_file ) {
case 'index.php':
return 'dashboard';
case 'edit.php':
return 'posts';
case 'upload.php':
return 'media';
case 'edit.php?post_type=page':
return 'comments';
case 'themes.php':
return 'theme';
case 'plugins.php':
return 'plugins';
case 'users.php':
return 'users';
case 'tools.php':
return 'management';
case 'options-general.php':
return 'options';
case 'settings.php':
return 'settings';
default:
return $current_screen->parent_file;
}
}
/**
* Checks if a file exists within {@see Easy_Settings::ROOT}, and if it
* does, returns the path to that file. If {@see Easy_Settings::TYPE}
* is `'theme'` and a child theme is in use, checks the parent
* {@see Easy_Settings::ROOT} as well.
*
* @since Easy Settings 0.5
*
* @param string $file The file
*/
public function get_file( $file ) {
if ( 'plugin' === self::TYPE ) {
if ( is_file( plugin_dir_path( __FILE__ ) . $file ) ) {
return plugin_dir_path( __FILE__ ) . $file;
}
} elseif ( 'theme' === self::TYPE ) {
if ( is_file( get_template_directory() . self::ROOT . "/$file" ) ) {
return get_template_directory() . self::ROOT . "/$file";
} elseif ( is_child_theme() && is_file( get_stylesheet_directory() . self::ROOT . "/$file" ) ) {
return get_stylesheet_directory() . self::ROOT . "/$file";
} else {
return false;
}
}
}
/**
* Checks if a directory exists within {@see Easy_Settings::ROOT}, and if
* so, returns the path to that directory. If {@see Easy_Settings::TYPE} is
* `'theme'` and a child theme is in use, checks the parent
* {@see Easy_Settings::ROOT} as well.
*
* @since Easy Settings 0.5
*
* @param string $dir The directory.
*/
public function get_dir( $dir ) {
if ( 'plugin' === self::TYPE ) {
if ( is_dir( plugin_dir_path( __FILE__ ) . $dir ) ) {
return plugin_dir_path( __FILE__ ) . $dir;
}
} elseif ( 'theme' === self::TYPE ) {
if ( is_dir( get_template_directory() . self::ROOT . "/$dir" ) ) {
return get_template_directory() . self::ROOT . "/$dir";
} elseif ( is_child_theme() && is_dir( get_stylesheet_directory() . self::ROOT . "/$dir" ) ) {
return get_stylesheet_directory() . self::ROOT . "/$dir";
} else {
return false;
}
}
}
/**
* The default sanitize callback.
*
* @since Easy Settings 0.5
*
* @param array $input The input to sanitize.
*/
public function default_sanitize_callback( $input ) {
foreach ( $input as &$value ) {
$value = sanitize_text_field( $value );
}
unset( $value );
return $input;
}
/**
* Adds an error to be displayed later.
*
* @since Easy Settings 0.5
*
* @param string $error The error to be displayed.
*/
public function error( $error ) {
$this->errors[] = $error;
if ( !has_action( 'admin_notices', array( $this, 'display_error' ) ) ) {
add_action( 'admin_notices', array( $this, 'display_error' ) );
}
}
/**
* Displays an error.
*
* @since Easy Settings 0.5
*/
public function display_error() {
foreach ( $this->errors as &$error ) { ?>
<div class="error">
<p><strong>Easy Settings Error:</strong> <?php echo $error; ?></p>
</div>
<?php }
}
}
new Easy_Settings();