PluginProbe ʕ •ᴥ•ʔ
Kirki – Freeform Page Builder, Website Builder & Customizer / 6.1.1
Kirki – Freeform Page Builder, Website Builder & Customizer v6.1.1
6.1.1 6.1.0 6.0.14 6.0.13 6.0.12 6.0.11 6.0.10 6.0.9 6.0.8 6.0.7 6.0.6 6.0.5 6.0.4 6.0.3 6.0.2 6.0.1 3.1.3 3.1.4 3.1.5 3.1.6 3.1.7 3.1.8 3.1.9 4.0.19 4.0.20 4.0.21 4.0.22 4.0.23 4.0.24 4.1 4.2.0 5.0.0 5.1.0 5.1.1 5.2.0 5.2.1 5.2.2 5.2.3 6.0.0 trunk 3.0.40 3.0.41 3.0.42 3.0.43 3.0.44 3.0.45 3.1.0 3.1.1 3.1.2
kirki / customizer / packages / controls / react-select / src / Field / ReactSelect.php
kirki / customizer / packages / controls / react-select / src / Field Last commit date
ReactSelect.php 5 months ago
ReactSelect.php
202 lines
1 <?php
2 /**
3 * Override field methods
4 *
5 * @package kirki-framework/control-select
6 * @copyright Copyright (c) 2023, Themeum
7 * @license https://opensource.org/licenses/MIT
8 * @since 1.0
9 */
10
11 namespace Kirki\Field;
12
13 use Kirki\Field;
14
15 /**
16 * Field overrides.
17 *
18 * @since 1.0
19 */
20 class ReactSelect extends Field {
21
22 /**
23 * The field type.
24 *
25 * @access public
26 * @since 1.0
27 * @var string
28 */
29 public $type = 'kirki-select';
30
31 /**
32 * Whether this is a multi-select or not.
33 *
34 * *Backwards compatibility note:
35 *
36 * Previously (when Kirki used Select2), $multiple is used to:
37 * - Determine whether the select is multiple or not.
38 * - Determine the maximum number of selection.
39 *
40 * Start from Kirki 4 (when Kirki uses react-select),
41 * $multiple is used to determine whether the select is multiple or not.
42 * The maximum selection number is now set in $max_selection.
43 *
44 * @since 1.0
45 * @var bool
46 */
47 protected $multiple = false;
48
49 /**
50 * The maximum selection length for multiple selection.
51 *
52 * @since 1.1
53 * @var bool
54 */
55 protected $max_selection_number = 999;
56
57 /**
58 * Placeholder text.
59 *
60 * @access protected
61 * @since 1.0
62 * @var string|false
63 */
64 protected $placeholder = false;
65
66 /**
67 * The control class-name.
68 *
69 * @access protected
70 * @since 0.1
71 * @var string
72 */
73 protected $control_class = '\Kirki\Control\ReactSelect';
74
75 /**
76 * Whether we should register the control class for JS-templating or not.
77 *
78 * @access protected
79 * @since 0.1
80 * @var bool
81 */
82 protected $control_has_js_template = true;
83
84 /**
85 * Filter arguments before creating the setting.
86 *
87 * @access public
88 * @since 0.1
89 * @param array $args The field arguments.
90 * @param WP_Customize_Manager $wp_customize The customizer instance.
91 * @return array
92 */
93 public function filter_setting_args( $args, $wp_customize ) {
94
95 if ( $args['settings'] === $this->args['settings'] ) {
96 $args = parent::filter_setting_args( $args, $wp_customize );
97
98 if ( isset( $args['multiple'] ) ) {
99 $multiple_and_max = self::get_multiple_and_max( $args['multiple'] );
100 $args['multiple'] = $multiple_and_max['multiple'];
101 $args['max_selection_number'] = $multiple_and_max['max_selection_number'];
102 } else {
103 $args['multiple'] = false;
104 }
105
106 // Set the sanitize-callback if none is defined.
107 if ( ! isset( $args['sanitize_callback'] ) || ! $args['sanitize_callback'] ) {
108 $args['sanitize_callback'] = ! $args['multiple'] ? 'sanitize_text_field' : function( $values ) use ( $args ) {
109 $values = (array) $values;
110 $sanitized_values = [];
111
112 // If total selected values > max_selection_number, then we need to remove the excess.
113 if ( count( $values ) > $args['max_selection_number'] ) {
114 for ( $i = 0; $i < $args['max_selection_number']; $i++ ) {
115 $sanitized_values[ $i ] = isset( $values[ $i ] ) ? sanitize_text_field( $values[ $i ] ) : '';
116 }
117 } else {
118 foreach ( $values as $index => $subvalue ) {
119 $sanitized_values[ $index ] = sanitize_text_field( $subvalue );
120 }
121 }
122
123 return $sanitized_values;
124 };
125 }
126 }
127
128 return $args;
129
130 }
131
132 /**
133 * Filter arguments before creating the control.
134 *
135 * @access public
136 * @since 0.1
137 * @param array $args The field arguments.
138 * @param WP_Customize_Manager $wp_customize The customizer instance.
139 * @return array
140 */
141 public function filter_control_args( $args, $wp_customize ) {
142
143 if ( $args['settings'] === $this->args['settings'] ) {
144 $args = parent::filter_control_args( $args, $wp_customize );
145
146 if ( isset( $args['multiple'] ) ) {
147 $multiple_and_max = self::get_multiple_and_max( $args['multiple'] );
148 $args['multiple'] = $multiple_and_max['multiple'];
149 $args['max_selection_number'] = $multiple_and_max['max_selection_number'];
150 }
151
152 $args['type'] = 'kirki-react-select';
153 }
154
155 return $args;
156
157 }
158
159 /**
160 * Get the value of "multiple" and "max_selection_number"
161 * from the provided $multiple parameter.
162 *
163 * @since 1.1
164 *
165 * @param bool|int $multiple The provided $multiple value.
166 * @return array
167 */
168 public static function get_multiple_and_max( $multiple ) {
169
170 $max_selection_number = 999;
171
172 if ( is_numeric( $multiple ) ) {
173 $multiple = (int) $multiple;
174
175 /**
176 * Treat -1 as unlimited just like in WordPress's get_posts (well, in this Kirki case, it's 999 :).
177 * Also treat 0 as "unlimited" because 1 it self will disable the multiple selection.
178 */
179 if ( 0 >= $multiple ) {
180 $max_selection_number = 999;
181 $multiple = true;
182 } else {
183 // If $multiple is > 1.
184 if ( 1 < $multiple ) {
185 $max_selection_number = $multiple;
186 $multiple = true;
187 } else {
188 // Here $multiple === 1, that means, it's single mode select.
189 $multiple = false;
190 }
191 }
192 }
193
194 return [
195 'multiple' => $multiple,
196 'max_selection_number' => $max_selection_number,
197 ];
198
199 }
200
201 }
202