PluginProbe
Gallery Box / trunk
Gallery Box vtrunk
trunk 1.3.1 1.3.3 1.4.0 1.4.1 1.4.2 1.5.1 1.7.35
gallery-box / admin / src / cmb2-select2 / select2.php

select2.php in Gallery Box trunk, at admin/src/cmb2-select2/select2.php

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