This repository has been archived by the owner on Jul 18, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 329
/
Copy pathclass-wordpress-plugin-template-settings.php
502 lines (443 loc) · 15 KB
/
class-wordpress-plugin-template-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
<?php
/**
* Settings class file.
*
* @package WordPress Plugin Template/Settings
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Settings class.
*/
class WordPress_Plugin_Template_Settings {
/**
* The single instance of WordPress_Plugin_Template_Settings.
*
* @var object
* @access private
* @since 1.0.0
*/
private static $_instance = null; //phpcs:ignore
/**
* The main plugin object.
*
* @var object
* @access public
* @since 1.0.0
*/
public $parent = null;
/**
* Prefix for plugin settings.
*
* @var string
* @access public
* @since 1.0.0
*/
public $base = '';
/**
* Available settings for plugin.
*
* @var array
* @access public
* @since 1.0.0
*/
public $settings = array();
/**
* Constructor function.
*
* @param object $parent Parent object.
*/
public function __construct( $parent ) {
$this->parent = $parent;
$this->base = 'wpt_';
// Initialise settings.
add_action( 'init', array( $this, 'init_settings' ), 11 );
// Register plugin settings.
add_action( 'admin_init', array( $this, 'register_settings' ) );
// Add settings page to menu.
add_action( 'admin_menu', array( $this, 'add_menu_item' ) );
// Add settings link to plugins page.
add_filter(
'plugin_action_links_' . plugin_basename( $this->parent->file ),
array(
$this,
'add_settings_link',
)
);
// Configure placement of plugin settings page. See readme for implementation.
add_filter( $this->base . 'menu_settings', array( $this, 'configure_settings' ) );
}
/**
* Initialise settings
*
* @return void
*/
public function init_settings() {
$this->settings = $this->settings_fields();
}
/**
* Add settings page to admin menu
*
* @return void
*/
public function add_menu_item() {
$args = $this->menu_settings();
// Do nothing if wrong location key is set.
if ( is_array( $args ) && isset( $args['location'] ) && function_exists( 'add_' . $args['location'] . '_page' ) ) {
switch ( $args['location'] ) {
case 'options':
case 'submenu':
$page = add_submenu_page( $args['parent_slug'], $args['page_title'], $args['menu_title'], $args['capability'], $args['menu_slug'], $args['function'] );
break;
case 'menu':
$page = add_menu_page( $args['page_title'], $args['menu_title'], $args['capability'], $args['menu_slug'], $args['function'], $args['icon_url'], $args['position'] );
break;
default:
return;
}
add_action( 'admin_print_styles-' . $page, array( $this, 'settings_assets' ) );
}
}
/**
* Prepare default settings page arguments
*
* @return mixed|void
*/
private function menu_settings() {
return apply_filters(
$this->base . 'menu_settings',
array(
'location' => 'options', // Possible settings: options, menu, submenu.
'parent_slug' => 'options-general.php',
'page_title' => __( 'Plugin Settings', 'wordpress-plugin-template' ),
'menu_title' => __( 'Plugin Settings', 'wordpress-plugin-template' ),
'capability' => 'manage_options',
'menu_slug' => $this->parent->_token . '_settings',
'function' => array( $this, 'settings_page' ),
'icon_url' => '',
'position' => null,
)
);
}
/**
* Container for settings page arguments
*
* @param array $settings Settings array.
*
* @return array
*/
public function configure_settings( $settings = array() ) {
return $settings;
}
/**
* Load settings JS & CSS
*
* @return void
*/
public function settings_assets() {
// We're including the farbtastic script & styles here because they're needed for the colour picker
// If you're not including a colour picker field then you can leave these calls out as well as the farbtastic dependency for the wpt-admin-js script below.
wp_enqueue_style( 'farbtastic' );
wp_enqueue_script( 'farbtastic' );
// We're including the WP media scripts here because they're needed for the image upload field.
// If you're not including an image upload then you can leave this function call out.
wp_enqueue_media();
wp_register_script( $this->parent->_token . '-settings-js', $this->parent->assets_url . 'js/settings' . $this->parent->script_suffix . '.js', array( 'farbtastic', 'jquery' ), '1.0.0', true );
wp_enqueue_script( $this->parent->_token . '-settings-js' );
}
/**
* Add settings link to plugin list table
*
* @param array $links Existing links.
* @return array Modified links.
*/
public function add_settings_link( $links ) {
$settings_link = '<a href="options-general.php?page=' . $this->parent->_token . '_settings">' . __( 'Settings', 'wordpress-plugin-template' ) . '</a>';
array_push( $links, $settings_link );
return $links;
}
/**
* Build settings fields
*
* @return array Fields to be displayed on settings page
*/
private function settings_fields() {
$settings['standard'] = array(
'title' => __( 'Standard', 'wordpress-plugin-template' ),
'description' => __( 'These are fairly standard form input fields.', 'wordpress-plugin-template' ),
'fields' => array(
array(
'id' => 'text_field',
'label' => __( 'Some Text', 'wordpress-plugin-template' ),
'description' => __( 'This is a standard text field.', 'wordpress-plugin-template' ),
'type' => 'text',
'default' => '',
'placeholder' => __( 'Placeholder text', 'wordpress-plugin-template' ),
),
array(
'id' => 'password_field',
'label' => __( 'A Password', 'wordpress-plugin-template' ),
'description' => __( 'This is a standard password field.', 'wordpress-plugin-template' ),
'type' => 'password',
'default' => '',
'placeholder' => __( 'Placeholder text', 'wordpress-plugin-template' ),
),
array(
'id' => 'secret_text_field',
'label' => __( 'Some Secret Text', 'wordpress-plugin-template' ),
'description' => __( 'This is a secret text field - any data saved here will not be displayed after the page has reloaded, but it will be saved.', 'wordpress-plugin-template' ),
'type' => 'text_secret',
'default' => '',
'placeholder' => __( 'Placeholder text', 'wordpress-plugin-template' ),
),
array(
'id' => 'text_block',
'label' => __( 'A Text Block', 'wordpress-plugin-template' ),
'description' => __( 'This is a standard text area.', 'wordpress-plugin-template' ),
'type' => 'textarea',
'default' => '',
'placeholder' => __( 'Placeholder text for this textarea', 'wordpress-plugin-template' ),
),
array(
'id' => 'single_checkbox',
'label' => __( 'An Option', 'wordpress-plugin-template' ),
'description' => __( 'A standard checkbox - if you save this option as checked then it will store the option as \'on\', otherwise it will be an empty string.', 'wordpress-plugin-template' ),
'type' => 'checkbox',
'default' => '',
),
array(
'id' => 'select_box',
'label' => __( 'A Select Box', 'wordpress-plugin-template' ),
'description' => __( 'A standard select box.', 'wordpress-plugin-template' ),
'type' => 'select',
'options' => array(
'drupal' => 'Drupal',
'joomla' => 'Joomla',
'wordpress' => 'WordPress',
),
'default' => 'wordpress',
),
array(
'id' => 'radio_buttons',
'label' => __( 'Some Options', 'wordpress-plugin-template' ),
'description' => __( 'A standard set of radio buttons.', 'wordpress-plugin-template' ),
'type' => 'radio',
'options' => array(
'superman' => 'Superman',
'batman' => 'Batman',
'ironman' => 'Iron Man',
),
'default' => 'batman',
),
array(
'id' => 'multiple_checkboxes',
'label' => __( 'Some Items', 'wordpress-plugin-template' ),
'description' => __( 'You can select multiple items and they will be stored as an array.', 'wordpress-plugin-template' ),
'type' => 'checkbox_multi',
'options' => array(
'square' => 'Square',
'circle' => 'Circle',
'rectangle' => 'Rectangle',
'triangle' => 'Triangle',
),
'default' => array( 'circle', 'triangle' ),
),
),
);
$settings['extra'] = array(
'title' => __( 'Extra', 'wordpress-plugin-template' ),
'description' => __( 'These are some extra input fields that maybe aren\'t as common as the others.', 'wordpress-plugin-template' ),
'fields' => array(
array(
'id' => 'number_field',
'label' => __( 'A Number', 'wordpress-plugin-template' ),
'description' => __( 'This is a standard number field - if this field contains anything other than numbers then the form will not be submitted.', 'wordpress-plugin-template' ),
'type' => 'number',
'default' => '',
'placeholder' => __( '42', 'wordpress-plugin-template' ),
),
array(
'id' => 'colour_picker',
'label' => __( 'Pick a colour', 'wordpress-plugin-template' ),
'description' => __( 'This uses WordPress\' built-in colour picker - the option is stored as the colour\'s hex code.', 'wordpress-plugin-template' ),
'type' => 'color',
'default' => '#21759B',
),
array(
'id' => 'an_image',
'label' => __( 'An Image', 'wordpress-plugin-template' ),
'description' => __( 'This will upload an image to your media library and store the attachment ID in the option field. Once you have uploaded an imge the thumbnail will display above these buttons.', 'wordpress-plugin-template' ),
'type' => 'image',
'default' => '',
'placeholder' => '',
),
array(
'id' => 'multi_select_box',
'label' => __( 'A Multi-Select Box', 'wordpress-plugin-template' ),
'description' => __( 'A standard multi-select box - the saved data is stored as an array.', 'wordpress-plugin-template' ),
'type' => 'select_multi',
'options' => array(
'linux' => 'Linux',
'mac' => 'Mac',
'windows' => 'Windows',
),
'default' => array( 'linux' ),
),
),
);
$settings = apply_filters( $this->parent->_token . '_settings_fields', $settings );
return $settings;
}
/**
* Register plugin settings
*
* @return void
*/
public function register_settings() {
if ( is_array( $this->settings ) ) {
// Check posted/selected tab.
//phpcs:disable
$current_section = '';
if ( isset( $_POST['tab'] ) && $_POST['tab'] ) {
$current_section = $_POST['tab'];
} else {
if ( isset( $_GET['tab'] ) && $_GET['tab'] ) {
$current_section = $_GET['tab'];
}
}
//phpcs:enable
foreach ( $this->settings as $section => $data ) {
if ( $current_section && $current_section !== $section ) {
continue;
}
// Add section to page.
add_settings_section( $section, $data['title'], array( $this, 'settings_section' ), $this->parent->_token . '_settings' );
foreach ( $data['fields'] as $field ) {
// Validation callback for field.
$validation = '';
if ( isset( $field['callback'] ) ) {
$validation = $field['callback'];
}
// Register field.
$option_name = $this->base . $field['id'];
register_setting( $this->parent->_token . '_settings', $option_name, $validation );
// Add field to page.
add_settings_field(
$field['id'],
$field['label'],
array( $this->parent->admin, 'display_field' ),
$this->parent->_token . '_settings',
$section,
array(
'field' => $field,
'prefix' => $this->base,
)
);
}
if ( ! $current_section ) {
break;
}
}
}
}
/**
* Settings section.
*
* @param array $section Array of section ids.
* @return void
*/
public function settings_section( $section ) {
$html = '<p> ' . $this->settings[ $section['id'] ]['description'] . '</p>' . "\n";
echo $html; //phpcs:ignore
}
/**
* Load settings page content.
*
* @return void
*/
public function settings_page() {
// Build page HTML.
$html = '<div class="wrap" id="' . $this->parent->_token . '_settings">' . "\n";
$html .= '<h2>' . __( 'Plugin Settings', 'wordpress-plugin-template' ) . '</h2>' . "\n";
$tab = '';
//phpcs:disable
if ( isset( $_GET['tab'] ) && $_GET['tab'] ) {
$tab .= $_GET['tab'];
}
//phpcs:enable
// Show page tabs.
if ( is_array( $this->settings ) && 1 < count( $this->settings ) ) {
$html .= '<h2 class="nav-tab-wrapper">' . "\n";
$c = 0;
foreach ( $this->settings as $section => $data ) {
// Set tab class.
$class = 'nav-tab';
if ( ! isset( $_GET['tab'] ) ) { //phpcs:ignore
if ( 0 === $c ) {
$class .= ' nav-tab-active';
}
} else {
if ( isset( $_GET['tab'] ) && $section == $_GET['tab'] ) { //phpcs:ignore
$class .= ' nav-tab-active';
}
}
// Set tab link.
$tab_link = add_query_arg( array( 'tab' => $section ) );
if ( isset( $_GET['settings-updated'] ) ) { //phpcs:ignore
$tab_link = remove_query_arg( 'settings-updated', $tab_link );
}
// Output tab.
$html .= '<a href="' . $tab_link . '" class="' . esc_attr( $class ) . '">' . esc_html( $data['title'] ) . '</a>' . "\n";
++$c;
}
$html .= '</h2>' . "\n";
}
$html .= '<form method="post" action="options.php" enctype="multipart/form-data">' . "\n";
// Get settings fields.
ob_start();
settings_fields( $this->parent->_token . '_settings' );
do_settings_sections( $this->parent->_token . '_settings' );
$html .= ob_get_clean();
$html .= '<p class="submit">' . "\n";
$html .= '<input type="hidden" name="tab" value="' . esc_attr( $tab ) . '" />' . "\n";
$html .= '<input name="Submit" type="submit" class="button-primary" value="' . esc_attr( __( 'Save Settings', 'wordpress-plugin-template' ) ) . '" />' . "\n";
$html .= '</p>' . "\n";
$html .= '</form>' . "\n";
$html .= '</div>' . "\n";
echo $html; //phpcs:ignore
}
/**
* Main WordPress_Plugin_Template_Settings Instance
*
* Ensures only one instance of WordPress_Plugin_Template_Settings is loaded or can be loaded.
*
* @since 1.0.0
* @static
* @see WordPress_Plugin_Template()
* @param object $parent Object instance.
* @return object WordPress_Plugin_Template_Settings instance
*/
public static function instance( $parent ) {
if ( is_null( self::$_instance ) ) {
self::$_instance = new self( $parent );
}
return self::$_instance;
} // End instance()
/**
* Cloning is forbidden.
*
* @since 1.0.0
*/
public function __clone() {
_doing_it_wrong( __FUNCTION__, esc_html( __( 'Cloning of WordPress_Plugin_Template_API is forbidden.' ) ), esc_attr( $this->parent->_version ) );
} // End __clone()
/**
* Unserializing instances of this class is forbidden.
*
* @since 1.0.0
*/
public function __wakeup() {
_doing_it_wrong( __FUNCTION__, esc_html( __( 'Unserializing instances of WordPress_Plugin_Template_API is forbidden.' ) ), esc_attr( $this->parent->_version ) );
} // End __wakeup()
}