forked from mustardBees/cmb-field-select2
-
Notifications
You must be signed in to change notification settings - Fork 1
/
cmb-field-select2.php
executable file
·186 lines (156 loc) · 6 KB
/
cmb-field-select2.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
<?php
/*
Plugin Name: CMB2 Field Type: Select2
Plugin URI: https://github.com/mustardBees/cmb-field-select2
GitHub Plugin URI: https://github.com/mustardBees/cmb-field-select2
Description: Select2 field type for CMB2.
Version: 3.0.4-dev
Author: Phil Wylie
Author URI: https://www.philwylie.co.uk/
License: GPLv2+
*/
/**
* Class PW_CMB2_Field_Select2
*/
class PW_CMB2_Field_Select2 {
/**
* Current version number
*/
const VERSION = '3.0.3';
/**
* Initialize the plugin by hooking into CMB2
*/
public function __construct() {
add_filter( 'cmb2_render_pw_select', array( $this, 'render_pw_select' ), 10, 5 );
add_filter( 'cmb2_render_pw_multiselect', array( $this, 'render_pw_multiselect' ), 10, 5 );
add_filter( 'cmb2_sanitize_pw_multiselect', array( $this, 'pw_multiselect_sanitize' ), 10, 4 );
add_filter( 'cmb2_types_esc_pw_multiselect', array( $this, 'pw_multiselect_escaped_value' ), 10, 3 );
add_filter( 'cmb2_repeat_table_row_types', array( $this, 'pw_multiselect_table_row_class' ), 10, 1 );
}
/**
* Render select box field
*/
public function render_pw_select( $field, $field_escaped_value, $field_object_id, $field_object_type, $field_type_object ) {
$this->setup_admin_scripts();
if ( version_compare( CMB2_VERSION, '2.2.2', '>=' ) ) {
$field_type_object->type = new CMB2_Type_Select( $field_type_object );
}
echo $field_type_object->select( array(
'class' => 'pw_select2 pw_select',
'desc' => $field_type_object->_desc( true ),
'options' => '<option></option>' . $field_type_object->concat_items(),
'data-placeholder' => $field->args( 'attributes', 'placeholder' ) ? $field->args( 'attributes', 'placeholder' ) : $field->args( 'description' ),
) );
}
/**
* Render multi-value select input field
*/
public function render_pw_multiselect( $field, $field_escaped_value, $field_object_id, $field_object_type, $field_type_object ) {
$this->setup_admin_scripts();
if ( version_compare( CMB2_VERSION, '2.2.2', '>=' ) ) {
$field_type_object->type = new CMB2_Type_Select( $field_type_object );
}
$a = $field_type_object->parse_args( 'pw_multiselect', array(
'multiple' => 'multiple',
'style' => 'width: 99%',
'class' => 'pw_select2 pw_multiselect',
'name' => $field_type_object->_name() . '[]',
'id' => $field_type_object->_id(),
'desc' => $field_type_object->_desc( true ),
'options' => $this->get_pw_multiselect_options( $field_escaped_value, $field_type_object ),
'data-placeholder' => $field->args( 'attributes', 'placeholder' ) ? $field->args( 'attributes', 'placeholder' ) : $field->args( 'description' ),
) );
$attrs = $field_type_object->concat_attrs( $a, array( 'desc', 'options' ) );
echo sprintf( '<select%s>%s</select>%s', $attrs, $a['options'], $a['desc'] );
}
/**
* Return list of options for pw_multiselect.
*
* @version 3.0.4
*
* Return the list of options, with selected options at the top preserving their order. This also handles the
* removal of selected options which no longer exist in the options array.
*/
public function get_pw_multiselect_options( $field_escaped_value = array(), $field_type_object = null ) {
$options = (array) $field_type_object->field->options();
// If we have selected items, we need to preserve their order
if ( ! empty( $field_escaped_value ) ) {
$options = $this->sort_array_by_array( $options, $field_escaped_value );
}
$selected_items = '';
$other_items = '';
foreach ( $options as $option_value => $option_label ) {
// Clone args & modify for just this item
$option = array(
'value' => $option_value,
'label' => $option_label,
);
// Split options into those which are selected and the rest
if ( in_array( $option_value, (array) $field_escaped_value ) ) {
$option['checked'] = true;
$selected_items .= $field_type_object->select_option( $option );
} else {
$other_items .= $field_type_object->select_option( $option );
}
}
return $selected_items . $other_items;
}
/**
* Sort an array by the keys of another array
*
* @author Eran Galperin
* @link http://link.from.pw/1Waji4l
*/
public function sort_array_by_array( array $array, array $orderArray ) {
$ordered = array();
foreach ( $orderArray as $key ) {
if ( array_key_exists( $key, $array ) ) {
$ordered[ $key ] = $array[ $key ];
unset( $array[ $key ] );
}
}
return $ordered + $array;
}
/**
* Handle sanitization for repeatable fields
*/
public function pw_multiselect_sanitize( $check, $meta_value, $object_id, $field_args ) {
if ( ! is_array( $meta_value ) || ! $field_args['repeatable'] ) {
return $check;
}
foreach ( $meta_value as $key => $val ) {
$meta_value[$key] = array_map( 'sanitize_text_field', $val );
}
return $meta_value;
}
/**
* Handle escaping for repeatable fields
*/
public function pw_multiselect_escaped_value( $check, $meta_value, $field_args ) {
if ( ! is_array( $meta_value ) || ! $field_args['repeatable'] ) {
return $check;
}
foreach ( $meta_value as $key => $val ) {
$meta_value[$key] = array_map( 'esc_attr', $val );
}
return $meta_value;
}
/**
* Add 'table-layout' class to multi-value select field
*/
public function pw_multiselect_table_row_class( $check ) {
$check[] = 'pw_multiselect';
return $check;
}
/**
* Enqueue scripts and styles
*/
public function setup_admin_scripts() {
$asset_path = apply_filters( 'pw_cmb2_field_select2_asset_path', plugins_url( '', __FILE__ ) );
wp_register_script( 'pw-select2', $asset_path . '/js/select2.min.js', array( 'jquery-ui-sortable' ), '4.0.3' );
wp_enqueue_script( 'pw-select2-init', $asset_path . '/js/script.js', array( 'cmb2-scripts', 'pw-select2' ), self::VERSION );
wp_register_style( 'pw-select2', $asset_path . '/css/select2.min.css', array(), '4.0.3' );
wp_enqueue_style( 'pw-select2-tweaks', $asset_path . '/css/style.css', array( 'pw-select2' ), self::VERSION );
}
}
$pw_cmb2_field_select2 = new PW_CMB2_Field_Select2();