FlexibleContent
2 months ago
class-acf-field-accordion.php
2 months ago
class-acf-field-button-group.php
2 months ago
class-acf-field-checkbox.php
2 days ago
class-acf-field-clone.php
2 months ago
class-acf-field-color_picker.php
2 months ago
class-acf-field-date_picker.php
2 months ago
class-acf-field-date_time_picker.php
2 months ago
class-acf-field-email.php
2 months ago
class-acf-field-file.php
2 months ago
class-acf-field-flexible-content.php
1 week ago
class-acf-field-gallery.php
3 weeks ago
class-acf-field-google-map.php
2 months ago
class-acf-field-group.php
2 months ago
class-acf-field-icon_picker.php
7 months ago
class-acf-field-image.php
2 months ago
class-acf-field-link.php
2 months ago
class-acf-field-message.php
1 year ago
class-acf-field-nav-menu.php
1 week ago
class-acf-field-number.php
2 months ago
class-acf-field-oembed.php
3 weeks ago
class-acf-field-output.php
1 year ago
class-acf-field-page_link.php
3 weeks ago
class-acf-field-password.php
2 months ago
class-acf-field-post_object.php
3 weeks ago
class-acf-field-radio.php
2 days ago
class-acf-field-range.php
2 months ago
class-acf-field-relationship.php
3 weeks ago
class-acf-field-repeater.php
3 weeks ago
class-acf-field-select.php
2 days ago
class-acf-field-separator.php
1 year ago
class-acf-field-tab.php
1 year ago
class-acf-field-taxonomy.php
3 weeks ago
class-acf-field-text.php
3 weeks ago
class-acf-field-textarea.php
3 weeks ago
class-acf-field-time_picker.php
2 months ago
class-acf-field-true_false.php
2 months ago
class-acf-field-url.php
3 weeks ago
class-acf-field-user.php
3 weeks ago
class-acf-field-wysiwyg.php
2 months ago
class-acf-field.php
2 months ago
class-acf-repeater-table.php
1 year ago
index.php
1 year ago
class-acf-field-button-group.php
338 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 early 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 | 'button_group' => true, |
| 75 | ); |
| 76 | } |
| 77 | |
| 78 | // maybe select initial value |
| 79 | if ( ( ! isset( $field['allow_null'] ) || ! $field['allow_null'] ) && null === $selected ) { |
| 80 | $buttons[0]['checked'] = true; |
| 81 | } |
| 82 | |
| 83 | // Ensure roving tabindex when allow_null is enabled and no selection yet. |
| 84 | if ( $field['allow_null'] && null === $selected && ! empty( $buttons ) ) { |
| 85 | $buttons[0]['tabindex'] = '0'; |
| 86 | } |
| 87 | |
| 88 | // div |
| 89 | $div = array( |
| 90 | 'class' => 'acf-button-group', |
| 91 | 'role' => 'radiogroup', |
| 92 | ); |
| 93 | |
| 94 | // Add aria-labelledby if field has an ID for proper screen reader announcement |
| 95 | if ( ! empty( $field['id'] ) ) { |
| 96 | $div['aria-labelledby'] = $field['id'] . '-label'; |
| 97 | } |
| 98 | |
| 99 | if ( 'vertical' === $field['layout'] ) { |
| 100 | $div['class'] .= ' -vertical'; |
| 101 | } |
| 102 | if ( $field['class'] ) { |
| 103 | $div['class'] .= ' ' . $field['class']; |
| 104 | } |
| 105 | if ( $field['allow_null'] ) { |
| 106 | $div['data-allow_null'] = 1; |
| 107 | } |
| 108 | |
| 109 | // hidden input |
| 110 | $html .= acf_get_hidden_input( array( 'name' => $field['name'] ) ); |
| 111 | |
| 112 | // open |
| 113 | $html .= '<div ' . acf_esc_attrs( $div ) . '>'; |
| 114 | |
| 115 | // loop |
| 116 | foreach ( $buttons as $button ) { |
| 117 | |
| 118 | // checked |
| 119 | if ( $button['checked'] ) { |
| 120 | $button['checked'] = 'checked'; |
| 121 | } else { |
| 122 | unset( $button['checked'] ); |
| 123 | } |
| 124 | |
| 125 | // append |
| 126 | $html .= acf_get_radio_input( $button ); |
| 127 | } |
| 128 | |
| 129 | // close |
| 130 | $html .= '</div>'; |
| 131 | |
| 132 | // return |
| 133 | echo $html; //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- safe HTML, escaped by input building functions. |
| 134 | } |
| 135 | |
| 136 | |
| 137 | /** |
| 138 | * render_field_settings() |
| 139 | * |
| 140 | * Creates the field's settings HTML |
| 141 | * |
| 142 | * @date 18/9/17 |
| 143 | * @since ACF 5.6.3 |
| 144 | * |
| 145 | * @param array $field The field settings array |
| 146 | * @return n/a |
| 147 | */ |
| 148 | function render_field_settings( $field ) { |
| 149 | // Encode choices (convert from array). |
| 150 | $field['choices'] = acf_encode_choices( $field['choices'] ); |
| 151 | |
| 152 | acf_render_field_setting( |
| 153 | $field, |
| 154 | array( |
| 155 | 'label' => __( 'Choices', 'secure-custom-fields' ), |
| 156 | '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>', |
| 157 | 'type' => 'textarea', |
| 158 | 'name' => 'choices', |
| 159 | ) |
| 160 | ); |
| 161 | |
| 162 | acf_render_field_setting( |
| 163 | $field, |
| 164 | array( |
| 165 | 'label' => __( 'Default Value', 'secure-custom-fields' ), |
| 166 | 'instructions' => __( 'Appears when creating a new post', 'secure-custom-fields' ), |
| 167 | 'type' => 'text', |
| 168 | 'name' => 'default_value', |
| 169 | ) |
| 170 | ); |
| 171 | |
| 172 | acf_render_field_setting( |
| 173 | $field, |
| 174 | array( |
| 175 | 'label' => __( 'Return Value', 'secure-custom-fields' ), |
| 176 | 'instructions' => __( 'Specify the returned value on front end', 'secure-custom-fields' ), |
| 177 | 'type' => 'radio', |
| 178 | 'name' => 'return_format', |
| 179 | 'layout' => 'horizontal', |
| 180 | 'choices' => array( |
| 181 | 'value' => __( 'Value', 'secure-custom-fields' ), |
| 182 | 'label' => __( 'Label', 'secure-custom-fields' ), |
| 183 | 'array' => __( 'Both (Array)', 'secure-custom-fields' ), |
| 184 | ), |
| 185 | ) |
| 186 | ); |
| 187 | } |
| 188 | |
| 189 | /** |
| 190 | * Renders the field settings used in the "Validation" tab. |
| 191 | * |
| 192 | * @since ACF 6.0 |
| 193 | * |
| 194 | * @param array $field The field settings array. |
| 195 | * @return void |
| 196 | */ |
| 197 | function render_field_validation_settings( $field ) { |
| 198 | acf_render_field_setting( |
| 199 | $field, |
| 200 | array( |
| 201 | 'label' => __( 'Allow Null', 'secure-custom-fields' ), |
| 202 | 'instructions' => '', |
| 203 | 'name' => 'allow_null', |
| 204 | 'type' => 'true_false', |
| 205 | 'ui' => 1, |
| 206 | ) |
| 207 | ); |
| 208 | } |
| 209 | |
| 210 | /** |
| 211 | * Renders the field settings used in the "Presentation" tab. |
| 212 | * |
| 213 | * @since ACF 6.0 |
| 214 | * |
| 215 | * @param array $field The field settings array. |
| 216 | * @return void |
| 217 | */ |
| 218 | function render_field_presentation_settings( $field ) { |
| 219 | acf_render_field_setting( |
| 220 | $field, |
| 221 | array( |
| 222 | 'label' => __( 'Layout', 'secure-custom-fields' ), |
| 223 | 'instructions' => '', |
| 224 | 'type' => 'radio', |
| 225 | 'name' => 'layout', |
| 226 | 'layout' => 'horizontal', |
| 227 | 'choices' => array( |
| 228 | 'horizontal' => __( 'Horizontal', 'secure-custom-fields' ), |
| 229 | 'vertical' => __( 'Vertical', 'secure-custom-fields' ), |
| 230 | ), |
| 231 | ) |
| 232 | ); |
| 233 | } |
| 234 | |
| 235 | /** |
| 236 | * This filter is applied to the $field before it is saved to the database |
| 237 | * |
| 238 | * @date 18/9/17 |
| 239 | * @since ACF 5.6.3 |
| 240 | * |
| 241 | * @param array $field The field array holding all the field options |
| 242 | * @return $field |
| 243 | */ |
| 244 | function update_field( $field ) { |
| 245 | |
| 246 | return acf_get_field_type( 'radio' )->update_field( $field ); |
| 247 | } |
| 248 | |
| 249 | |
| 250 | /** |
| 251 | * This filter is applied to the $value after it is loaded from the db |
| 252 | * |
| 253 | * @date 18/9/17 |
| 254 | * @since ACF 5.6.3 |
| 255 | * |
| 256 | * @param mixed $value The value found in the database |
| 257 | * @param mixed $post_id The post ID from which the value was loaded from |
| 258 | * @param array $field The field array holding all the field options |
| 259 | * @return $value |
| 260 | */ |
| 261 | function load_value( $value, $post_id, $field ) { |
| 262 | |
| 263 | return acf_get_field_type( 'radio' )->load_value( $value, $post_id, $field ); |
| 264 | } |
| 265 | |
| 266 | |
| 267 | /** |
| 268 | * This function will translate field settings |
| 269 | * |
| 270 | * @date 18/9/17 |
| 271 | * @since ACF 5.6.3 |
| 272 | * |
| 273 | * @param array $field The field array holding all the field options |
| 274 | * @return $field |
| 275 | */ |
| 276 | function translate_field( $field ) { |
| 277 | |
| 278 | return acf_get_field_type( 'radio' )->translate_field( $field ); |
| 279 | } |
| 280 | |
| 281 | |
| 282 | /** |
| 283 | * This filter is applied to the $value after it is loaded from the db and before it is returned to the template |
| 284 | * |
| 285 | * @date 18/9/17 |
| 286 | * @since ACF 5.6.3 |
| 287 | * |
| 288 | * @param mixed $value The value found in the database |
| 289 | * @param mixed $post_id The post ID from which the value was loaded from |
| 290 | * @param array $field The field array holding all the field options |
| 291 | * @return $value |
| 292 | */ |
| 293 | function format_value( $value, $post_id, $field ) { |
| 294 | |
| 295 | return acf_get_field_type( 'radio' )->format_value( $value, $post_id, $field ); |
| 296 | } |
| 297 | |
| 298 | /** |
| 299 | * Return the schema array for the REST API. |
| 300 | * |
| 301 | * @param array $field |
| 302 | * @return array |
| 303 | */ |
| 304 | function get_rest_schema( array $field ) { |
| 305 | $schema = parent::get_rest_schema( $field ); |
| 306 | |
| 307 | if ( isset( $field['default_value'] ) && '' !== $field['default_value'] ) { |
| 308 | $schema['default'] = $field['default_value']; |
| 309 | } |
| 310 | |
| 311 | $schema['enum'] = acf_get_field_type( 'select' )->format_rest_choices( $field['choices'] ); |
| 312 | $schema['enum'][] = null; |
| 313 | |
| 314 | // Allow null via UI will value to empty string. |
| 315 | if ( ! empty( $field['allow_null'] ) ) { |
| 316 | $schema['enum'][] = ''; |
| 317 | } |
| 318 | |
| 319 | return $schema; |
| 320 | } |
| 321 | |
| 322 | /** |
| 323 | * Returns an array of JSON-LD Property output types that are supported by this field type. |
| 324 | * |
| 325 | * @since 6.8 |
| 326 | * |
| 327 | * @return string[] |
| 328 | */ |
| 329 | public function get_jsonld_output_types(): array { |
| 330 | return array( 'Text' ); |
| 331 | } |
| 332 | } |
| 333 | |
| 334 | |
| 335 | // initialize |
| 336 | acf_register_field_type( 'acf_field_button_group' ); |
| 337 | endif; // class_exists check |
| 338 |