Skip to content

Commit

Permalink
Refactor to use CMB2 APIs. No longer need a sanitization_cb, and shou…
Browse files Browse the repository at this point in the history
…ld setup structure for repeatable properly.
  • Loading branch information
jtsternberg committed Feb 4, 2015
1 parent ac56e40 commit a834070
Showing 1 changed file with 45 additions and 47 deletions.
92 changes: 45 additions & 47 deletions cmb-field-select2.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) ? '<p class="cmb2-metabox-description">' . $desc . '</p>' : '' );
}
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 a834070

Please sign in to comment.