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

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