From a834070ababe60453d6e8dce7d8c92553d28010e Mon Sep 17 00:00:00 2001 From: Justin Sternberg Date: Tue, 3 Feb 2015 23:45:50 -0500 Subject: [PATCH] Refactor to use CMB2 APIs. No longer need a sanitization_cb, and should setup structure for repeatable properly. --- cmb-field-select2.php | 92 +++++++++++++++++++++---------------------- 1 file changed, 45 insertions(+), 47 deletions(-) diff --git a/cmb-field-select2.php b/cmb-field-select2.php index 22e39eb..1ee024d 100644 --- a/cmb-field-select2.php +++ b/cmb-field-select2.php @@ -15,73 +15,71 @@ /** * Enqueue scripts and styles, call requested select box field */ -function pw_select2( $field, $meta ) { +function pw_select2_enqueue() { wp_enqueue_script( 'pw-select2-field-js', PW_SELECT2_URL . 'js/select2/select2.min.js', array( 'jquery-ui-sortable' ), '3.5.1' ); wp_enqueue_script( 'pw-select2-field-init', PW_SELECT2_URL . 'js/select2-init.js', array( 'pw-select2-field-js' ), null ); wp_enqueue_style( 'pw-select2-field-css', PW_SELECT2_URL . 'js/select2/select2.css', array(), '3.5.1' ); wp_enqueue_style( 'pw-select2-field-mods', PW_SELECT2_URL . 'css/select2.css', array(), null ); - - call_user_func( $field->args( 'type' ), $field->args(), $meta ); - - $desc = $field->args('desc'); - echo ( ! empty( $desc ) && ! empty( $meta ) ? '

' . $desc . '

' : '' ); } -add_filter( 'cmb2_render_pw_select', 'pw_select2', 10, 2 ); -add_filter( 'cmb2_render_pw_multiselect', 'pw_select2', 10, 2 ); /** * Render select box field */ -function pw_select( $field, $meta ) { - echo ''; +function pw_select2_render( $field, $value, $object_id, $object_type, $field_type_object ) { + pw_select2_enqueue(); + + echo $field_type_object->select( array( + 'class' => 'cmb2_select select2', + // Append an empty option (used by the placeholder) + 'options' => '' . $field_type_object->concat_items(), + // Use description as placeholder + 'desc' => $field->args( 'desc' ) && ! empty( $value ) ? $field_type_object->_desc( true ) : '', + 'data-placeholder' => $field->args( 'desc' ), + ) ); } +add_filter( 'cmb2_render_pw_select', 'pw_select2_render', 10, 5 ); /** - * Render multi-value select box field + * Render multi-value select input field */ -function pw_multiselect( $field, $meta ) { - $options = array(); +function pw_multiselect_render( $field, $value, $object_id, $object_type, $field_type_object ) { + pw_select2_enqueue(); - if ( isset( $field['options'] ) && ! empty( $field['options'] ) ) { - foreach ( $field['options'] as $option_key => $option ) { - $opt_label = is_array( $option ) && array_key_exists( 'name', $option ) ? $option['name'] : $option; - $opt_value = is_array( $option ) && array_key_exists( 'value', $option ) ? $option['value'] : $option_key; + $options = array(); - $options[] = array( - 'id' => $opt_value, - 'text' => $opt_label - ); - } + foreach ( (array) $field->options() as $opt_value => $opt_label ) { + $options[] = array( + 'id' => $opt_value, + 'text' => $opt_label + ); } - wp_localize_script( 'pw-select2-field-init', $field['id'] . '_data', $options ); + wp_localize_script( 'pw-select2-field-init', $field_type_object->_id() . '_data', $options ); - if ( ! empty( $meta ) ) { - $meta = implode( ',', $meta ); - } + echo $field_type_object->input( array( + 'type' => 'hidden', + 'class' => 'select2', + // Use description as placeholder + 'desc' => $field->args( 'desc' ) && ! empty( $value ) ? $field_type_object->_desc( true ) : '', + 'data-placeholder' => esc_attr( $field->args( 'description' ) ), + ) ); - echo ''; } +add_filter( 'cmb2_render_pw_multiselect', 'pw_multiselect_render', 10, 5 ); -/** - * Handle saving of single and multi-value select fields - */ -function pw_select2_sanitise( $meta_value, $field ) { - if ( empty( $meta_value ) ) { - $meta_value = ''; - } elseif ( 'pw_multiselect' == $field['type'] ) { - $meta_value = explode( ',', $meta_value ); + +function pw_multiselect_escape( $check, $meta_value ) { + return ! empty( $meta_value ) ? implode( ',', $meta_value ) : $check; +} +add_filter( 'cmb2_types_esc_pw_multiselect', 'pw_multiselect_escape', 10, 2 ); + + +function pw_multiselect_sanitize( $check, $meta_value ) { + + if ( ! empty( $meta_value ) ) { + return explode( ',', $meta_value ); } - return $meta_value; -} \ No newline at end of file + return $check; +} +add_filter( 'cmb2_sanitize_pw_multiselect', 'pw_multiselect_sanitize', 10, 2 );