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