PluginProbe
Code Snippets / 3.6.6
Code Snippets v3.6.6
3.10.2 3.10.1 3.10.0 3.10.0-beta.2 3.10.0-beta.1 4.0.0-beta.1 3.9.6 trunk 2.10.0 2.10.1 2.12.0 2.12.1 2.13.0 2.13.1 2.13.2 2.13.3 2.14.0 2.14.1 2.14.2 2.14.3 2.14.4 2.14.5 2.14.6 3.0.0 3.0.1 All 64 releases
code-snippets / php / settings / class-setting-field.php

class-setting-field.php in Code Snippets 3.6.6, at php/settings/class-setting-field.php

265 lines 6.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * This file handles rendering the settings fields
4 *
5 * @since 2.0.0
6 * @package Code_Snippets
7 * @subpackage Settings
8 */
9
10 namespace Code_Snippets\Settings;
11
12 /**
13 * Represents a single setting field
14 *
15 * @property-read string $desc Field description.
16 * @property-read string $label Field label.
17 * @property-read string $type Field type.
18 * @property-read string $name Setting name.
19 *
20 * @property-read int $min Minimum value (for numerical inputs).
21 * @property-read int $max Maximum value(for numerical inputs).
22 * @property-read array<string, string> $options List of options for a select or checkboxes field.
23 * @property-read callable $render_callback Custom function to use when rendering a callback field.
24 * @property-read callable $sanitize_callback Custom function to use when sanitize the setting value.
25 * @property-read mixed $default Default setting value.
26 *
27 * @property-read string $input_name Value of `name` HTML attribute on an input element.
28 */
29 class Setting_Field {
30
31 /**
32 * Input field identifier.
33 *
34 * @var string
35 */
36 private string $field_id;
37
38 /**
39 * Settings section identifier.
40 *
41 * @var string
42 */
43 private string $section;
44
45 /**
46 * List of possible arguments.
47 *
48 * @var array<string, mixed>
49 */
50 private array $args = array(
51 'desc' => '',
52 'label' => '',
53 'min' => null,
54 'max' => null,
55 'options' => [],
56 );
57
58 /**
59 * Class constructor.
60 *
61 * @param string $section_id Settings section identifier.
62 * @param string $field_id Setting field identifier.
63 * @param array<string, mixed> $args The setting field attributes.
64 */
65 public function __construct( string $section_id, string $field_id, array $args ) {
66 $this->field_id = $field_id;
67 $this->section = $section_id;
68 $this->args = array_merge( $this->args, $args );
69 }
70
71 /**
72 * Retrieve a single setting attribute.
73 *
74 * @param string $argument Attribute name.
75 *
76 * @return mixed Attribute value.
77 */
78 public function __get( string $argument ) {
79
80 if ( 'input_name' === $argument ) {
81 return sprintf( '%s[%s][%s]', OPTION_NAME, $this->section, $this->field_id );
82 }
83
84 return $this->args[ $argument ];
85 }
86
87 /**
88 * Retrieve the saved value for this setting.
89 *
90 * @return mixed
91 */
92 private function get_saved_value() {
93 return get_setting( $this->section, $this->field_id );
94 }
95
96 /**
97 * Render the setting field
98 */
99 public function render() {
100 $method_name = 'render_' . $this->type . '_field';
101
102 if ( method_exists( $this, $method_name ) ) {
103 call_user_func( array( $this, $method_name ) );
104 } else {
105 // Error message, not necessary to translate.
106 printf( 'Cannot render a %s field.', esc_html( $this->type ) );
107 return;
108 }
109
110 if ( $this->desc ) {
111 echo '<p class="description">', wp_kses_post( $this->desc ), '</p>';
112 }
113 }
114
115 /**
116 * Render a callback field.
117 */
118 public function render_callback_field() {
119 call_user_func( $this->render_callback );
120 }
121
122 /**
123 * Render a single checkbox field.
124 *
125 * @param string $input_name Input name.
126 * @param string $label Input label.
127 * @param boolean $checked Whether the checkbox should be checked.
128 */
129 private static function render_checkbox( string $input_name, string $label, bool $checked ) {
130
131 $checkbox = sprintf(
132 '<input type="checkbox" name="%s"%s>',
133 esc_attr( $input_name ),
134 checked( $checked, true, false )
135 );
136
137 $kses = [
138 'input' => [
139 'type' => [],
140 'name' => [],
141 'checked' => [],
142 ],
143 ];
144
145 if ( $label ) {
146 printf(
147 '<label>%s %s</label>',
148 wp_kses( $checkbox, $kses ),
149 wp_kses_post( $label )
150 );
151 } else {
152 echo wp_kses( $checkbox, $kses );
153 }
154 }
155
156 /**
157 * Render a checkbox field for a setting
158 *
159 * @return void
160 * @since 2.0.0
161 */
162 public function render_checkbox_field() {
163 $this->render_checkbox( $this->input_name, $this->label, $this->get_saved_value() ?? false );
164 }
165
166 /**
167 * Render a checkbox field for a setting
168 *
169 * @return void
170 * @since 2.0.0
171 */
172 public function render_checkboxes_field() {
173 $saved_value = $this->get_saved_value();
174 $saved_value = is_array( $saved_value ) ? $saved_value : [];
175
176 echo '<fieldset>';
177 printf( '<legend class="screen-reader-text"><span>%s</span></legend>', esc_html( $this->name ) );
178
179 foreach ( $this->options as $option => $label ) {
180 $this->render_checkbox( $this->input_name . "[$option]", $label, in_array( $option, $saved_value, true ) );
181 echo '<br>';
182 }
183
184 echo '</fieldset>';
185 }
186
187 /**
188 * Render a basic text field for an editor setting.
189 *
190 * @return void
191 */
192 private function render_text_field() {
193 printf(
194 '<input type="text" name="%s" value="%s" class="regular-text">',
195 esc_attr( $this->input_name ),
196 esc_attr( $this->get_saved_value() )
197 );
198
199 if ( $this->label ) {
200 echo ' ' . wp_kses_post( $this->label );
201 }
202 }
203
204 /**
205 * Render a number select field for an editor setting
206 *
207 * @since 2.0.0
208 */
209 private function render_number_field() {
210 printf(
211 '<input type="number" name="%s" value="%s"',
212 esc_attr( $this->input_name ),
213 esc_attr( $this->get_saved_value() )
214 );
215
216 if ( is_numeric( $this->min ) ) {
217 printf( ' min="%d"', intval( $this->min ) );
218 }
219
220 if ( is_numeric( $this->max ) ) {
221 printf( ' max="%d"', intval( $this->max ) );
222 }
223
224 echo '>';
225
226 if ( $this->label ) {
227 echo ' ' . wp_kses_post( $this->label );
228 }
229 }
230
231 /**
232 * Render a number select field for an editor setting.
233 *
234 * @since 3.0.0
235 */
236 private function render_select_field() {
237 $saved_value = $this->get_saved_value();
238 printf( '<select name="%s">', esc_attr( $this->input_name ) );
239
240 foreach ( $this->options as $option => $option_label ) {
241 printf(
242 '<option value="%s"%s>%s</option>',
243 esc_attr( $option ),
244 selected( $option, $saved_value, false ),
245 esc_html( $option_label )
246 );
247 }
248
249 echo '</select>';
250 }
251
252 /**
253 * Render a button link.
254 *
255 * @since 3.5.1
256 */
257 private function render_action_field() {
258 printf(
259 '<button type="submit" name="%s" class="button">%s</button>',
260 esc_attr( $this->input_name ),
261 esc_html( $this->label ? $this->label : $this->name )
262 );
263 }
264 }
265