Skip to content

Commit

Permalink
Merge pull request #13 from jtsternberg/refactor
Browse files Browse the repository at this point in the history
Refactor to use CMB2 APIs. No longer need sanitization_cb, and should setup structure for repeatable properly.
  • Loading branch information
mustardBees committed Feb 4, 2015
2 parents ac56e40 + 8c075a4 commit a1350b7
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 65 deletions.
28 changes: 13 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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',
),
```

Expand Down
99 changes: 49 additions & 50 deletions cmb-field-select2.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,85 +3,84 @@
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+
*/

// 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 ) ? '<p class="cmb2-metabox-description">' . $desc . '</p>' : '' );
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 '<select name="', $field['id'], '" id="', $field['id'], '" data-placeholder="' . $field['desc'] . '" class="select2">';
echo '<option></option>';
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;

echo '<option value="', $opt_value, '" ', selected( $meta == $opt_value ) ,'>', $opt_label, '</option>';
}
}
echo '</select>';
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' => '<option></option>' . $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 '<input type="hidden" name="' . $field['id'] . '" id="' . $field['id'] . '" data-placeholder="' . $field['desc'] . '" class="select2" value="' . $meta . '" />';
}
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;
}
return $check;
}
add_filter( 'cmb2_sanitize_pw_multiselect', 'pw_multiselect_sanitize', 10, 2 );

0 comments on commit a1350b7

Please sign in to comment.