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