diff --git a/README.md b/README.md index 9cea624..d741be5 100644 --- a/README.md +++ b/README.md @@ -36,35 +36,33 @@ require_once 'cmb-field-select2/cmb-field-select2.php'; `pw_select` - Select box with with typeahead-style search. Example: ```php array( - 'name' => 'Cooking time', - 'id' => $prefix . 'cooking_time', - 'desc' => 'Cooking time', + 'name' => 'Cooking time', + 'id' => $prefix . 'cooking_time', + 'desc' => 'Cooking time', + 'type' => 'pw_select', 'options' => array( - '5' => '5 minutes', + '5' => '5 minutes', '10' => '10 minutes', '30' => 'Half an hour', '60' => '1 hour', ), - 'type' => 'pw_select', - 'sanitization_cb' => 'pw_select2_sanitise', ), ``` `pw_multiselect` - Multi-value select box with drag and drop reordering. Example: ```php array( - 'name' => 'Ingredients', - 'id' => $prefix . 'ingredients', - 'desc' => 'Select ingredients. Drag to reorder.', + 'name' => 'Ingredients', + 'id' => $prefix . 'ingredients', + 'desc' => 'Select ingredients. Drag to reorder.', + 'type' => 'pw_multiselect', 'options' => array( - 'flour' => 'Flour', - 'salt' => 'Salt', - 'eggs' => 'Eggs', - 'milk' => 'Milk', + 'flour' => 'Flour', + 'salt' => 'Salt', + 'eggs' => 'Eggs', + 'milk' => 'Milk', 'butter' => 'Butter', ), - 'type' => 'pw_multiselect', - 'sanitization_cb' => 'pw_select2_sanitise', ), ``` diff --git a/cmb-field-select2.php b/cmb-field-select2.php index 22e39eb..151e78b 100644 --- a/cmb-field-select2.php +++ b/cmb-field-select2.php @@ -3,7 +3,7 @@ Plugin Name: CMB Field Type: Select2 Plugin URI: https://github.com/mustardBees/cmb-field-select2 Description: Select2 field type for Custom Metaboxes and Fields for WordPress -Version: 2.0.3 +Version: 2.0.4 Author: Phil Wylie Author URI: http://www.philwylie.co.uk/ License: GPLv2+ @@ -11,77 +11,76 @@ // Useful global constants define( 'PW_SELECT2_URL', plugin_dir_url( __FILE__ ) ); +define( 'PW_SELECT2_VERSION', '2.0.4' ); /** * 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_script( 'pw-select2-field-init', PW_SELECT2_URL . 'js/select2-init.js', array( 'pw-select2-field-js' ), PW_SELECT2_VERSION ); 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 . '

' : '' ); + wp_enqueue_style( 'pw-select2-field-mods', PW_SELECT2_URL . 'css/select2.css', array(), PW_SELECT2_VERSION ); } -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 );