PluginProbe ʕ •ᴥ•ʔ
Secure Custom Fields / 6.4.1-beta7
Secure Custom Fields v6.4.1-beta7
6.9.5 6.9.4 6.9.3 6.9.2 6.9.1 6.9.0 6.8.9 6.8.7 6.8.8 6.8.6 6.8.4 6.8.5 trunk 6.4.0-beta1 6.4.0-beta2 6.4.1 6.4.1-beta3 6.4.1-beta4 6.4.1-beta5 6.4.1-beta6 6.4.1-beta7 6.4.2 6.5.0 6.5.1 6.5.2 6.5.3 6.5.4 6.5.5 6.5.6 6.5.7 6.6.0 6.7.0 6.7.1 6.8.0 6.8.1 6.8.2 6.8.3
secure-custom-fields / includes / fields / class-acf-field-button-group.php
secure-custom-fields / includes / fields Last commit date
class-acf-field-accordion.php 1 year ago class-acf-field-button-group.php 1 year ago class-acf-field-checkbox.php 1 year ago class-acf-field-clone.php 1 year ago class-acf-field-color_picker.php 1 year ago class-acf-field-date_picker.php 1 year ago class-acf-field-date_time_picker.php 1 year ago class-acf-field-email.php 1 year ago class-acf-field-file.php 1 year ago class-acf-field-flexible-content.php 1 year ago class-acf-field-gallery.php 1 year ago class-acf-field-google-map.php 1 year ago class-acf-field-group.php 1 year ago class-acf-field-icon_picker.php 1 year ago class-acf-field-image.php 1 year ago class-acf-field-link.php 1 year ago class-acf-field-message.php 1 year ago class-acf-field-number.php 1 year ago class-acf-field-oembed.php 1 year ago class-acf-field-output.php 1 year ago class-acf-field-page_link.php 1 year ago class-acf-field-password.php 1 year ago class-acf-field-post_object.php 1 year ago class-acf-field-radio.php 1 year ago class-acf-field-range.php 1 year ago class-acf-field-relationship.php 1 year ago class-acf-field-repeater.php 1 year ago class-acf-field-select.php 1 year ago class-acf-field-separator.php 1 year ago class-acf-field-tab.php 1 year ago class-acf-field-taxonomy.php 1 year ago class-acf-field-text.php 1 year ago class-acf-field-textarea.php 1 year ago class-acf-field-time_picker.php 1 year ago class-acf-field-true_false.php 1 year ago class-acf-field-url.php 1 year ago class-acf-field-user.php 1 year ago class-acf-field-wysiwyg.php 1 year ago class-acf-field.php 1 year ago class-acf-repeater-table.php 1 year ago index.php 1 year ago
class-acf-field-button-group.php
310 lines
1 <?php
2
3 if ( ! class_exists( 'acf_field_button_group' ) ) :
4
5 class acf_field_button_group extends acf_field {
6
7
8 /**
9 * initialize()
10 *
11 * This function will setup the field type data
12 *
13 * @date 18/9/17
14 * @since ACF 5.6.3
15 *
16 * @param n/a
17 * @return n/a
18 */
19 function initialize() {
20
21 // vars
22 $this->name = 'button_group';
23 $this->label = __( 'Button Group', 'secure-custom-fields' );
24 $this->category = 'choice';
25 $this->description = __( 'A group of buttons with values that you specify, users can choose one option from the values provided.', 'secure-custom-fields' );
26 $this->preview_image = acf_get_url() . '/assets/images/field-type-previews/field-preview-button-group.png';
27 $this->doc_url = 'https://developer.wordpress.org/secure-custom-fields/features/fields/button-group/';
28 $this->tutorial_url = 'https://developer.wordpress.org/secure-custom-fields/features/fields/button-group/button-group-tutorial/';
29 $this->defaults = array(
30 'choices' => array(),
31 'default_value' => '',
32 'allow_null' => 0,
33 'return_format' => 'value',
34 'layout' => 'horizontal',
35 );
36 }
37
38
39 /**
40 * Creates the field's input HTML
41 *
42 * @since ACF 5.6.3
43 *
44 * @param array $field The field settings array
45 */
46 public function render_field( $field ) {
47
48 // vars
49 $html = '';
50 $selected = null;
51 $buttons = array();
52 $value = esc_attr( $field['value'] );
53
54 // bail ealrly if no choices
55 if ( empty( $field['choices'] ) ) {
56 return;
57 }
58
59 // buttons
60 foreach ( $field['choices'] as $_value => $_label ) {
61
62 // checked
63 $checked = ( $value === esc_attr( $_value ) );
64 if ( $checked ) {
65 $selected = true;
66 }
67
68 // append
69 $buttons[] = array(
70 'name' => $field['name'],
71 'value' => $_value,
72 'label' => $_label,
73 'checked' => $checked,
74 );
75 }
76
77 // maybe select initial value
78 if ( ! $field['allow_null'] && $selected === null ) {
79 $buttons[0]['checked'] = true;
80 }
81
82 // div
83 $div = array( 'class' => 'acf-button-group' );
84
85 if ( $field['layout'] == 'vertical' ) {
86 $div['class'] .= ' -vertical'; }
87 if ( $field['class'] ) {
88 $div['class'] .= ' ' . $field['class']; }
89 if ( $field['allow_null'] ) {
90 $div['data-allow_null'] = 1; }
91
92 // hdden input
93 $html .= acf_get_hidden_input( array( 'name' => $field['name'] ) );
94
95 // open
96 $html .= '<div ' . acf_esc_attrs( $div ) . '>';
97
98 // loop
99 foreach ( $buttons as $button ) {
100
101 // checked
102 if ( $button['checked'] ) {
103 $button['checked'] = 'checked';
104 } else {
105 unset( $button['checked'] );
106 }
107
108 // append
109 $html .= acf_get_radio_input( $button );
110 }
111
112 // close
113 $html .= '</div>';
114
115 // return
116 echo $html; //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- safe HTML, escaped by input building functions.
117 }
118
119
120 /**
121 * render_field_settings()
122 *
123 * Creates the field's settings HTML
124 *
125 * @date 18/9/17
126 * @since ACF 5.6.3
127 *
128 * @param array $field The field settings array
129 * @return n/a
130 */
131 function render_field_settings( $field ) {
132 // Encode choices (convert from array).
133 $field['choices'] = acf_encode_choices( $field['choices'] );
134
135 acf_render_field_setting(
136 $field,
137 array(
138 'label' => __( 'Choices', 'secure-custom-fields' ),
139 'instructions' => __( 'Enter each choice on a new line.', 'secure-custom-fields' ) . '<br />' . __( 'For more control, you may specify both a value and label like this:', 'secure-custom-fields' ) . '<br /><span class="acf-field-setting-example">' . __( 'red : Red', 'secure-custom-fields' ) . '</span>',
140 'type' => 'textarea',
141 'name' => 'choices',
142 )
143 );
144
145 acf_render_field_setting(
146 $field,
147 array(
148 'label' => __( 'Default Value', 'secure-custom-fields' ),
149 'instructions' => __( 'Appears when creating a new post', 'secure-custom-fields' ),
150 'type' => 'text',
151 'name' => 'default_value',
152 )
153 );
154
155 acf_render_field_setting(
156 $field,
157 array(
158 'label' => __( 'Return Value', 'secure-custom-fields' ),
159 'instructions' => __( 'Specify the returned value on front end', 'secure-custom-fields' ),
160 'type' => 'radio',
161 'name' => 'return_format',
162 'layout' => 'horizontal',
163 'choices' => array(
164 'value' => __( 'Value', 'secure-custom-fields' ),
165 'label' => __( 'Label', 'secure-custom-fields' ),
166 'array' => __( 'Both (Array)', 'secure-custom-fields' ),
167 ),
168 )
169 );
170 }
171
172 /**
173 * Renders the field settings used in the "Validation" tab.
174 *
175 * @since ACF 6.0
176 *
177 * @param array $field The field settings array.
178 * @return void
179 */
180 function render_field_validation_settings( $field ) {
181 acf_render_field_setting(
182 $field,
183 array(
184 'label' => __( 'Allow Null', 'secure-custom-fields' ),
185 'instructions' => '',
186 'name' => 'allow_null',
187 'type' => 'true_false',
188 'ui' => 1,
189 )
190 );
191 }
192
193 /**
194 * Renders the field settings used in the "Presentation" tab.
195 *
196 * @since ACF 6.0
197 *
198 * @param array $field The field settings array.
199 * @return void
200 */
201 function render_field_presentation_settings( $field ) {
202 acf_render_field_setting(
203 $field,
204 array(
205 'label' => __( 'Layout', 'secure-custom-fields' ),
206 'instructions' => '',
207 'type' => 'radio',
208 'name' => 'layout',
209 'layout' => 'horizontal',
210 'choices' => array(
211 'horizontal' => __( 'Horizontal', 'secure-custom-fields' ),
212 'vertical' => __( 'Vertical', 'secure-custom-fields' ),
213 ),
214 )
215 );
216 }
217
218 /**
219 * This filter is appied to the $field before it is saved to the database
220 *
221 * @date 18/9/17
222 * @since ACF 5.6.3
223 *
224 * @param array $field The field array holding all the field options
225 * @return $field
226 */
227 function update_field( $field ) {
228
229 return acf_get_field_type( 'radio' )->update_field( $field );
230 }
231
232
233 /**
234 * This filter is appied to the $value after it is loaded from the db
235 *
236 * @date 18/9/17
237 * @since ACF 5.6.3
238 *
239 * @param mixed $value The value found in the database
240 * @param mixed $post_id The post ID from which the value was loaded from
241 * @param array $field The field array holding all the field options
242 * @return $value
243 */
244 function load_value( $value, $post_id, $field ) {
245
246 return acf_get_field_type( 'radio' )->load_value( $value, $post_id, $field );
247 }
248
249
250 /**
251 * This function will translate field settings
252 *
253 * @date 18/9/17
254 * @since ACF 5.6.3
255 *
256 * @param array $field The field array holding all the field options
257 * @return $field
258 */
259 function translate_field( $field ) {
260
261 return acf_get_field_type( 'radio' )->translate_field( $field );
262 }
263
264
265 /**
266 * This filter is appied to the $value after it is loaded from the db and before it is returned to the template
267 *
268 * @date 18/9/17
269 * @since ACF 5.6.3
270 *
271 * @param mixed $value The value found in the database
272 * @param mixed $post_id The post ID from which the value was loaded from
273 * @param array $field The field array holding all the field options
274 * @return $value
275 */
276 function format_value( $value, $post_id, $field ) {
277
278 return acf_get_field_type( 'radio' )->format_value( $value, $post_id, $field );
279 }
280
281 /**
282 * Return the schema array for the REST API.
283 *
284 * @param array $field
285 * @return array
286 */
287 function get_rest_schema( array $field ) {
288 $schema = parent::get_rest_schema( $field );
289
290 if ( isset( $field['default_value'] ) && '' !== $field['default_value'] ) {
291 $schema['default'] = $field['default_value'];
292 }
293
294 $schema['enum'] = acf_get_field_type( 'select' )->format_rest_choices( $field['choices'] );
295 $schema['enum'][] = null;
296
297 // Allow null via UI will value to empty string.
298 if ( ! empty( $field['allow_null'] ) ) {
299 $schema['enum'][] = '';
300 }
301
302 return $schema;
303 }
304 }
305
306
307 // initialize
308 acf_register_field_type( 'acf_field_button_group' );
309 endif; // class_exists check
310