| 1 |
<?php |
| 2 |
/** |
| 3 |
* CMB2 Advanced Select Field Type |
| 4 |
*/ |
| 5 |
|
| 6 |
if( ! function_exists( 'cmb2_render_advanced_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_advanced_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 |
$attrs['data-close-on-select'] = 'true'; |
| 27 |
|
| 28 |
// Support for multiple |
| 29 |
if( $field->args['multiple'] ) { |
| 30 |
$attrs['name'] = $attrs['name'] . '[]'; |
| 31 |
$attrs['multiple'] = 'true'; |
| 32 |
$attrs['data-close-on-select'] = 'false'; |
| 33 |
} |
| 34 |
|
| 35 |
// Parse options |
| 36 |
$options = ''; |
| 37 |
|
| 38 |
foreach ( $field->options() as $value => $option ) { |
| 39 |
|
| 40 |
if( is_array( $option ) ) { |
| 41 |
|
| 42 |
// Options group |
| 43 |
$options .= '<optgroup label="'. $value .'">'; |
| 44 |
|
| 45 |
foreach ( $option as $key => $label ) { |
| 46 |
$selected = ( is_array( $escaped_value ) && in_array( $key, $escaped_value ) ) || $escaped_value === $key; |
| 47 |
|
| 48 |
$options .= sprintf( '<option value="%s"%s>%s</option>', |
| 49 |
$key, |
| 50 |
$selected ? 'selected="selected"' : '', |
| 51 |
$label |
| 52 |
); |
| 53 |
} |
| 54 |
|
| 55 |
$options .= '</optgroup>'; |
| 56 |
|
| 57 |
} else { |
| 58 |
|
| 59 |
$selected = ( is_array( $escaped_value ) && in_array( $value, $escaped_value ) ) || $escaped_value === $value; |
| 60 |
|
| 61 |
//Single option |
| 62 |
$options .= sprintf( '<option value="%s"%s>%s</option>', |
| 63 |
$value, |
| 64 |
$selected ? 'selected="selected"' : '', |
| 65 |
$option |
| 66 |
); |
| 67 |
|
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
echo sprintf( '<select%s>%s</select>%s', |
| 72 |
$field_type_object->concat_attrs( $attrs ), |
| 73 |
$options, |
| 74 |
$field_type_object->_desc( true ) |
| 75 |
); |
| 76 |
|
| 77 |
} |
| 78 |
add_action( 'cmb2_render_advanced_select', 'cmb2_render_advanced_select_field_type', 10, 5 ); |
| 79 |
|
| 80 |
|
| 81 |
/** |
| 82 |
* Sanitize the selected value. |
| 83 |
*/ |
| 84 |
function cmb2_sanitize_advanced_select_callback( $override_value, $value ) { |
| 85 |
|
| 86 |
// Sanitize multiple value |
| 87 |
if ( is_array( $value ) ) { |
| 88 |
|
| 89 |
foreach ( $value as $key => $saved_value ) { |
| 90 |
|
| 91 |
$value[$key] = sanitize_text_field( $saved_value ); |
| 92 |
|
| 93 |
} |
| 94 |
|
| 95 |
return $value; |
| 96 |
|
| 97 |
} |
| 98 |
|
| 99 |
return sanitize_text_field( $value ); |
| 100 |
|
| 101 |
} |
| 102 |
add_filter( 'cmb2_sanitize_advanced_select', 'cmb2_sanitize_advanced_select_callback', 10, 2 ); |
| 103 |
|
| 104 |
endif; |
| 105 |
|