PluginProbe
Code Snippets / 3.2.1
Code Snippets v3.2.1
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.2.1, at php/settings/class-setting-field.php

250 lines 5.6 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 $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
28 */
29 class Setting_Field {
30
31 /**
32 * Input field identifier.
33 *
34 * @var string
35 */
36 private $field_id;
37
38 /**
39 * Settings section identifier.
40 *
41 * @var string
42 */
43 private $section;
44
45 /**
46 * List of possible arguments.
47 *
48 * @var array
49 */
50 private $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 $args The setting field attributes.
64 */
65 public function __construct( $section_id, $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( $argument ) {
79
80 if ( 'input_name' === $argument ) {
81 return sprintf( 'code_snippets_settings[%s][%s]', $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( $input_name, $label, $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 * @since 2.0.0
160 */
161 public function render_checkbox_field() {
162 $this->render_checkbox( $this->input_name, $this->label, $this->get_saved_value() );
163 }
164
165 /**
166 * Render a checkbox field for a setting
167 *
168 * @since 2.0.0
169 */
170 public function render_checkboxes_field() {
171 $saved_value = $this->get_saved_value();
172 $saved_value = is_array( $saved_value ) ? $saved_value : [];
173
174 echo '<fieldset>';
175 printf( '<legend class="screen-reader-text"><span>%s</span></legend>', esc_html( $this->name ) );
176
177 foreach ( $this->options as $option => $label ) {
178 $this->render_checkbox( $this->input_name . "[$option]", $label, in_array( $option, $saved_value, true ) );
179 echo '<br>';
180 }
181
182 echo '</fieldset>';
183 }
184
185 /**
186 * Render a basic text field for an editor setting.
187 */
188 private function render_text_field() {
189
190 printf(
191 '<input type="text" name="%s" value="%s" class="regular-text">',
192 esc_attr( $this->input_name ),
193 esc_attr( $this->get_saved_value() )
194 );
195
196 if ( $this->label ) {
197 echo ' ' . wp_kses_post( $this->label );
198 }
199 }
200
201 /**
202 * Render a number select field for an editor setting
203 *
204 * @since 2.0.0
205 */
206 private function render_number_field() {
207
208 printf(
209 '<input type="number" name="%s" value="%s"',
210 esc_attr( $this->input_name ),
211 esc_attr( $this->get_saved_value() )
212 );
213
214 if ( is_numeric( $this->min ) ) {
215 printf( ' min="%d"', intval( $this->min ) );
216 }
217
218 if ( is_numeric( $this->max ) ) {
219 printf( ' max="%d"', intval( $this->max ) );
220 }
221
222 echo '>';
223
224 if ( $this->label ) {
225 echo ' ' . wp_kses_post( $this->label );
226 }
227 }
228
229 /**
230 * Render a number select field for an editor setting
231 *
232 * @since 3.0.0
233 */
234 private function render_select_field() {
235 $saved_value = $this->get_saved_value();
236 printf( '<select name="%s">', esc_attr( $this->input_name ) );
237
238 foreach ( $this->options as $option => $option_label ) {
239 printf(
240 '<option value="%s"%s>%s</option>',
241 esc_attr( $option ),
242 selected( $option, $saved_value, false ),
243 esc_html( $option_label )
244 );
245 }
246
247 echo '</select>';
248 }
249 }
250