| @@ -1,0 +1,101 @@ | ||
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * CMB2 Select Multiple Field Type | |
| 4 | + */ | |
| 5 | + | |
| 6 | +if( ! function_exists( 'cmb2_render_automatorwp_select_field_type' ) ) { | |
| 7 | + | |
| 8 | + /** | |
| 9 | + * Adds a custom field type for select multiples. | |
| 10 | + * @param object $field The CMB2_Field type object. | |
| 11 | + * @param string $escaped_value The saved (and escaped) value. | |
| 12 | + * @param int $object_id The current post ID. | |
| 13 | + * @param string $object_type The current object type. | |
| 14 | + * @param CMB2_Types $field_type_object The CMB2_Types object. | |
| 15 | + * @return void | |
| 16 | + */ | |
| 17 | + function cmb2_render_automatorwp_select_field_type( $field, $escaped_value, $object_id, $object_type, $field_type_object ) { | |
| 18 | + | |
| 19 | + // Parse args | |
| 20 | + $attrs = $field_type_object->parse_args( 'select', array( | |
| 21 | + 'class' => 'cmb2_select', | |
| 22 | + 'name' => $field_type_object->_name(), | |
| 23 | + 'id' => $field_type_object->_id(), | |
| 24 | + ) ); | |
| 25 | + | |
| 26 | + // Support for multiple | |
| 27 | + if( isset( $field->args['attributes']['multiple'] ) && $field->args['attributes']['multiple'] ) { | |
| 28 | + $attrs['name'] = $attrs['name'] . '[]'; | |
| 29 | + $attrs['multiple'] = 'true'; | |
| 30 | + } | |
| 31 | + | |
| 32 | + // Parse options | |
| 33 | + $options = ''; | |
| 34 | + | |
| 35 | + foreach ( $field->options() as $value => $option ) { | |
| 36 | + | |
| 37 | + if( is_array( $option ) ) { | |
| 38 | + | |
| 39 | + // Options group | |
| 40 | + $options .= '<optgroup label="'. $value .'">'; | |
| 41 | + | |
| 42 | + foreach ( $option as $key => $label ) { | |
| 43 | + $selected = ( is_array( $escaped_value ) && in_array( $key, $escaped_value ) ) || $escaped_value === $key; | |
| 44 | + | |
| 45 | + $options .= sprintf( '<option value="%s"%s>%s</option>', | |
| 46 | + $key, | |
| 47 | + $selected ? 'selected="selected"' : '', | |
| 48 | + $label | |
| 49 | + ); | |
| 50 | + } | |
| 51 | + | |
| 52 | + $options .= '</optgroup>'; | |
| 53 | + | |
| 54 | + } else { | |
| 55 | + | |
| 56 | + $selected = ( is_array( $escaped_value ) && in_array( $value, $escaped_value ) ) || $escaped_value === $value; | |
| 57 | + | |
| 58 | + // Single option | |
| 59 | + $options .= sprintf( '<option value="%s"%s>%s</option>', | |
| 60 | + $value, | |
| 61 | + $selected ? 'selected="selected"' : '', | |
| 62 | + $option | |
| 63 | + ); | |
| 64 | + | |
| 65 | + } | |
| 66 | + } | |
| 67 | + | |
| 68 | + echo sprintf( '<select%s>%s</select>%s', | |
| 69 | + $field_type_object->concat_attrs( $attrs ), | |
| 70 | + $options, | |
| 71 | + $field_type_object->_desc( true ) | |
| 72 | + ); | |
| 73 | + | |
| 74 | + } | |
| 75 | + add_action( 'cmb2_render_automatorwp_select', 'cmb2_render_automatorwp_select_field_type', 10, 5 ); | |
| 76 | + | |
| 77 | + | |
| 78 | + /** | |
| 79 | + * Sanitize the selected value. | |
| 80 | + */ | |
| 81 | + function cmb2_sanitize_automatorwp_select_callback( $override_value, $value ) { | |
| 82 | + | |
| 83 | + // Sanitize multiple value | |
| 84 | + if ( is_array( $value ) ) { | |
| 85 | + | |
| 86 | + foreach ( $value as $key => $saved_value ) { | |
| 87 | + | |
| 88 | + $value[$key] = sanitize_text_field( $saved_value ); | |
| 89 | + | |
| 90 | + } | |
| 91 | + | |
| 92 | + return $value; | |
| 93 | + | |
| 94 | + } | |
| 95 | + | |
| 96 | + return sanitize_text_field( $value ); | |
| 97 | + | |
| 98 | + } | |
| 99 | + add_filter( 'cmb2_sanitize_automatorwp_select', 'cmb2_sanitize_automatorwp_select_callback', 10, 2 ); | |
| 100 | + | |
| 101 | +} | |