Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
18 / 18 |
|
100.00% |
3 / 3 |
CRAP | |
100.00% |
1 / 1 |
Settings_Section_Element_Abstract | |
100.00% |
18 / 18 |
|
100.00% |
3 / 3 |
3 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
add_settings_field | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
1 | |||
register_setting | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
print_field_callback | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
sanitize_callback | n/a |
0 / 0 |
n/a |
0 / 0 |
0 |
1 | <?php |
2 | /** |
3 | * An abstract settings element for extending. |
4 | * |
5 | * @link https://BrianHenry.ie |
6 | * @since 1.0.0 |
7 | * |
8 | * @package bh-wp-autologin-urls |
9 | */ |
10 | |
11 | namespace BrianHenryIE\WP_Autologin_URLs\Admin\Settings_Fields; |
12 | |
13 | /** |
14 | * Code common across setting elements. |
15 | * |
16 | * @see https://github.com/reside-eng/wordpress-custom-plugin |
17 | * @see register_setting() |
18 | * @see add_settings_field() |
19 | * |
20 | * Class Settings_Section_Element |
21 | */ |
22 | abstract class Settings_Section_Element_Abstract { |
23 | |
24 | /** |
25 | * The unique setting id, as used in the wp_options table. |
26 | * |
27 | * @var string The id of the setting in the database. |
28 | */ |
29 | protected $id; |
30 | |
31 | /** |
32 | * The setting's existing value. Used in HTML value="". |
33 | * |
34 | * @var mixed The previously saved value. |
35 | */ |
36 | protected $value; |
37 | |
38 | /** |
39 | * The name of the setting as it is printed in the left column of the settings table. |
40 | * |
41 | * @var string $title The title of the setting. |
42 | */ |
43 | protected $title; |
44 | |
45 | /** |
46 | * The slug of the settings page this setting is shown on. |
47 | * |
48 | * @var string $page The settings page page slug. |
49 | */ |
50 | protected $page; |
51 | |
52 | /** |
53 | * The section name as used with add_settings_section(). |
54 | * |
55 | * @var string $section The section/tab the setting is displayed in. |
56 | */ |
57 | protected $section = 'default'; |
58 | |
59 | /** |
60 | * The data array the WordPress Settings API passes to print_field_callback(). |
61 | * |
62 | * @var array{helper:string, supplemental:string, default:mixed, placeholder:string} Array of data available to print_field_callback() |
63 | */ |
64 | protected $add_settings_field_args; |
65 | |
66 | /** |
67 | * The options array used when registering the setting. |
68 | * |
69 | * @var array{type:string, description:string, sanitize_callback:callable, show_in_rest:bool|array<string,mixed>, default:mixed} $register_setting_args Data used to describe the setting when registered. |
70 | */ |
71 | protected array $register_setting_args = array(); |
72 | |
73 | /** |
74 | * Settings_Section_Element constructor. |
75 | * |
76 | * @param string $settings_page_slug_name The page slug the settings section is on. |
77 | * @param string $section The name of the section the settings are displayed in. |
78 | */ |
79 | public function __construct( $settings_page_slug_name, $section = 'default' ) { |
80 | |
81 | $this->page = $settings_page_slug_name; |
82 | $this->section = $section; |
83 | |
84 | $this->register_setting_args['description'] = ''; |
85 | $this->register_setting_args['sanitize_callback'] = array( $this, 'sanitize_callback' ); |
86 | $this->register_setting_args['show_in_rest'] = false; |
87 | } |
88 | |
89 | /** |
90 | * Add the configured settings field to the page and section. |
91 | */ |
92 | public function add_settings_field(): void { |
93 | |
94 | add_settings_field( |
95 | $this->id, |
96 | $this->title, |
97 | array( $this, 'print_field_callback' ), |
98 | $this->page, |
99 | $this->section, |
100 | $this->add_settings_field_args |
101 | ); |
102 | } |
103 | |
104 | /** |
105 | * Register the setting with WordPress so it is whitelisted for saving. |
106 | */ |
107 | public function register_setting(): void { |
108 | |
109 | register_setting( |
110 | $this->page, |
111 | $this->id, |
112 | $this->register_setting_args |
113 | ); |
114 | } |
115 | |
116 | /** |
117 | * Echo the HTML for configuring this setting. |
118 | * |
119 | * @param array{placeholder:string, helper:string, supplemental:string, default:string} $arguments The field data as registered with add_settings_field(). |
120 | */ |
121 | abstract public function print_field_callback( $arguments ): void; |
122 | |
123 | /** |
124 | * Carry out any sanitization and pre-processing of the POSTed data before it is saved in the database. |
125 | * |
126 | * This gets run twice on the first request it is run – where false is the old value, WordPress then runs `add_option()`. |
127 | * |
128 | * @param mixed $value The value entered by the user as POSTed to WordPress. |
129 | * |
130 | * @return mixed |
131 | */ |
132 | abstract public function sanitize_callback( $value ); |
133 | } |