PluginProbe ʕ •ᴥ•ʔ
Robin Image Optimizer – Unlimited Image Optimization, WebP & AVIF / trunk
Robin Image Optimizer – Unlimited Image Optimization, WebP & AVIF vtrunk
2.0.5 trunk 1.3.7 1.4.0 1.4.1 1.4.2 1.4.6 1.5.0 1.5.3 1.5.6 1.5.8 1.6.5 1.6.6 1.6.9 1.7.0 1.7.4 1.8.1 1.8.2 1.9.0 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4
robin-image-optimizer / libs / factory / forms / controls / dropdown.php
robin-image-optimizer / libs / factory / forms / controls Last commit date
customs 5 months ago holders 5 months ago checkbox.php 5 months ago color-and-opacity.php 5 months ago color.php 5 months ago datepicker-range.php 5 months ago dropdown-and-colors.php 5 months ago dropdown.php 5 months ago font.php 5 months ago google-font.php 5 months ago gradient.php 5 months ago hidden.php 5 months ago index.php 5 months ago integer.php 5 months ago list.php 5 months ago multiple-textbox.php 5 months ago paddings-editor.php 5 months ago pattern.php 5 months ago radio-colors.php 5 months ago radio.php 5 months ago textarea.php 5 months ago textbox.php 5 months ago url.php 5 months ago wp-editor.php 5 months ago
dropdown.php
406 lines
1 <?php
2
3 /**
4 * Dropdown List Control
5 *
6 * Main options:
7 * name => a name of the control
8 * value => a value to show in the control
9 * default => a default value of the control if the "value" option is not specified
10 * items => a callback to return items or an array of items to select
11 *
12 * @package factory-forms
13 * @since 1.0.0
14 */
15
16 // Exit if accessed directly
17 if ( ! defined( 'ABSPATH' ) ) {
18 exit;
19 }
20
21 if ( ! class_exists( 'Wbcr_FactoryForms600_DropdownControl' ) ) {
22
23 class Wbcr_FactoryForms600_DropdownControl extends Wbcr_FactoryForms600_Control {
24
25 public $type = 'dropdown';
26
27 /**
28 * Returns a set of available items for the list.
29 *
30 * @since 1.0.0
31 * @return mixed[]
32 */
33 private function getItems() {
34 $data = $this->getOption( 'data', [] );
35
36 // if the data options is a valid callback for an object method
37 if ( ( is_array( $data ) && count( $data ) == 2 && is_object( $data[0] ) ) || is_string( $data ) ) {
38
39 return call_user_func( $data );
40 }
41
42 // if the data options is an array of values
43 return $data;
44 }
45
46 /**
47 * Returns true, if the data should be loaded via ajax.
48 *
49 * @since 1.0.0
50 * @return bool
51 */
52 protected function isAjax() {
53
54 $data = $this->getOption( 'data', [] );
55
56 return is_array( $data ) && isset( $data['ajax'] );
57 }
58
59 /**
60 * Shows the html markup of the control.
61 *
62 * @since 1.0.0
63 * @return void
64 */
65 public function html() {
66
67 $way = $this->getOption( 'way', 'default' );
68 $this->addHtmlData( 'way', $way );
69
70 $events_data = $this->getOption( 'events', [] );
71
72 if ( ! empty( $events_data ) ) {
73 $events_string_data = json_encode( $events_data );
74 $name_on_form = $this->getNameOnForm();
75
76 $value = $this->getValue();
77
78 if ( empty( $value ) || ( is_array( $value ) && empty( $value[0] ) ) ) {
79 $value = null;
80 }
81
82 if ( ! empty( $value ) && isset( $events_data[ $value ] ) && is_array( $events_data[ $value ] ) ) {
83 $print_styles = '';
84 foreach ( $events_data[ $value ] as $eventName => $selectors ) {
85 if ( $eventName == 'hide' ) {
86 $print_styles .= $selectors . '{display:none;}';
87 } elseif ( $eventName == 'show' ) {
88 $print_styles .= $selectors . '{display:block;}';
89 }
90 }
91
92 echo '<style>' . esc_html( $print_styles ) . '</style>';
93 }
94 ?>
95 <script>
96 // Onepress factory dropdown control events
97 if( void 0 === window.factory_dropdown_control_events_data ) {
98 window.factory_dropdown_control_events_data = {};
99 }
100 window.factory_dropdown_control_events_data['<?php echo esc_attr( $name_on_form ); ?>'] = <?php echo $events_string_data; ?>;
101 </script>
102 <?php
103 }
104 if ( $this->isAjax() ) {
105
106 $data = $this->getOption( 'data', [] );
107 $ajax_id = 'factory-dropdown-' . rand( 1000000, 9999999 );
108
109 $value = $this->getValue();
110
111 if ( empty( $value ) || ( is_array( $value ) && empty( $value[0] ) ) ) {
112 $value = null;
113 }
114
115 ?>
116 <div class="factory-ajax-loader <?php echo esc_attr( $ajax_id ) . '-loader'; ?>"></div>
117 <script>
118 window['<?php echo $ajax_id; ?>'] = {
119 'loader': '.<?php echo esc_attr( $ajax_id ) . '-loader'; ?>',
120 'url': '<?php echo esc_url( $data['url'] ); ?>',
121 'data': <?php echo json_encode( $data['data'] ); ?>,
122 'selected': '<?php echo esc_attr( $value ); ?>',
123 'empty_list': '<?php echo esc_attr( $this->getOption( 'empty', __( 'The list is empty.', 'robin-image-optimizer' ) ) ); ?>'
124 };
125 </script>
126 <?php
127
128 $this->addHtmlData( 'ajax', true );
129 $this->addHtmlData( 'ajax-data-id', $ajax_id );
130 $this->addCssClass( 'factory-hidden' );
131 }
132
133 if ( 'buttons' == $way ) {
134 $this->buttonsHtml();
135 } elseif ( 'ddslick' == $way ) {
136 $this->ddslickHtml();
137 } else {
138 $this->defaultHtml();
139 }
140 }
141
142 /**
143 * Shows the Buttons Dropdown.
144 *
145 * @since 1.0.0
146 * @return void
147 */
148 protected function buttonsHtml() {
149 $items = $this->getItems();
150 $value = $this->getValue();
151
152 $name_on_form = $this->getNameOnForm();
153
154 $this->addCssClass( 'factory-buttons-way' );
155
156 ?>
157 <div <?php $this->attrs(); ?>>
158 <div class="btn-group factory-buttons-group">
159 <?php foreach ( $items as $item ) { ?>
160 <button type="button" class="btn btn-default btn-small factory-<?php echo esc_attr( $item[0] ); ?>
161 <?php
162 if ( $value == $item[0] ) {
163 echo 'active';
164 }
165 ?>
166 " data-value="<?php echo esc_attr( $item[0] ); ?>"><?php echo esc_attr( $item[1] ); ?></button>
167 <?php } ?>
168 <input type="hidden" id="<?php echo esc_attr( $name_on_form ); ?>" class="factory-result" name="<?php echo esc_attr( $name_on_form ); ?>" value="<?php echo esc_attr( $value ); ?>"/>
169 </div>
170 <div class="factory-hints">
171 <?php foreach ( $items as $item ) { ?>
172 <?php if ( isset( $item[2] ) ) { ?>
173 <div class="factory-hint factory-hint-<?php echo esc_attr( $item[0] ); ?>"
174 <?php
175 if ( $value !== $item[0] ) {
176 echo 'style="display: none;"';
177 }
178 ?>
179 ><?php echo wp_kses( $item[2], 'default' ); ?></div>
180 <?php } ?>
181 <?php } ?>
182 </div>
183 </div>
184 <?php
185 }
186
187 /**
188 * Shows the ddSlick dropbox.
189 *
190 * @since 3.2.8
191 * @return void
192 */
193 protected function ddslickHtml() {
194 $items = $this->getItems();
195 $value = $this->getValue();
196
197 $name_on_form = $this->getNameOnForm();
198
199 $this->addCssClass( 'factory-ddslick-way' );
200 $this->addHtmlData( 'name', $name_on_form );
201
202 $this->addHtmlData( 'width', $this->getOption( 'width', 300 ) );
203 $this->addHtmlData( 'align', $this->getOption( 'imagePosition', 'right' ) );
204
205 ?>
206 <div <?php $this->attrs(); ?>>
207 <script>
208 //Dropdown plugin data
209 var factory_<?php echo esc_attr( $name_on_form ); ?>_data = [
210 <?php foreach ( $items as $item ) { ?>
211 {
212 text: "<?php echo esc_html( $item['title'] ); ?>",
213 value: "<?php echo esc_html( $item['value'] ); ?>",
214 selected:
215 <?php
216 if ( $value == $item['value'] ) {
217 echo 'true';
218 } else {
219 echo 'false';
220 }
221 ?>
222 ,
223 description: "<?php echo( isset( $item['hint'] ) ? wp_kses( $item['hint'], 'default' ) : '' ); ?>",
224 imageSrc: "<?php echo( isset( $item['image'] ) ? esc_url( $item['image'] ) : '' ); ?>",
225 imageHoverSrc: "<?php echo( isset( $item['hover'] ) ? esc_url( $item['hover'] ) : '' ); ?>"
226 },
227 <?php } ?>
228 ];
229 </script>
230 <div class="factory-ddslick"></div>
231 <input type="hidden" class="factory-result" id="<?php echo esc_attr( $name_on_form ); ?>" name="<?php echo esc_attr( $name_on_form ); ?>" value="<?php echo esc_attr( $value ); ?>"/>
232 </div>
233 <?php
234 }
235
236 /**
237 * Shows the standart dropdown.
238 *
239 * @since 1.3.1
240 * @return void
241 */
242 protected function defaultHtml() {
243
244 $items = $this->getItems();
245 $value = esc_attr( $this->getValue() );
246
247 $name_on_form = $this->getNameOnForm();
248
249 $this->addHtmlAttr( 'id', $name_on_form );
250 $this->addHtmlAttr( 'name', $name_on_form );
251 $this->addCssClass( 'form-control' );
252
253 $hasGroups = $this->getOption( 'hasGroups', true );
254 $has_hints = $this->getOption( 'hasHints', false );
255
256 foreach ( $items as $item ) {
257 if ( isset( $item['type'] ) && $item['type'] == 'group' && ! empty( $item['items'] ) ) {
258 foreach ( (array) $item['items'] as $group_item ) {
259 $is_hint = ( isset( $group_item['hint'] ) && ! empty( $group_item['hint'] ) ) || ( isset( $group_item[2] ) && ! empty( $group_item[2] ) );
260 if ( ! $is_hint ) {
261 continue;
262 }
263 $has_hints = true;
264 break;
265 }
266 if ( $has_hints ) {
267 break;
268 }
269 } else {
270 $is_hint = ( isset( $item['hint'] ) && ! empty( $item['hint'] ) ) || ( isset( $item[2] ) && ! $item[2] );
271 if ( ! $is_hint ) {
272 continue;
273 }
274 $has_hints = true;
275 break;
276 }
277 }
278
279 $is_empty = $this->isAjax() || empty( $items );
280 // translators: empty is used as a placeholder for empty dropdown list.
281 $empty_list = $this->getOption( 'empty', '- ' . __( 'empty', 'robin-image-optimizer' ) . ' -' );
282
283 ?>
284 <select <?php $this->attrs(); ?>>
285 <?php if ( $is_empty ) { ?>
286 <option value='' class="factory-empty-option">
287 <?php echo $empty_list; ?>
288 </option>
289 <?php } else { ?>
290 <?php $this->printItems( $items, $value ); ?>
291 <?php } ?>
292 </select>
293 <?php if ( $has_hints ) { ?>
294 <div class="factory-hints">
295 <?php
296 foreach ( $items as $item ) {
297 if ( isset( $item['type'] ) && $item['type'] == 'group' && ! empty( $item['items'] ) ) {
298 foreach ( (array) $item['items'] as $group_item ) {
299
300 $hint = isset( $group_item[2] ) ? wp_kses( $group_item[2], 'default' ) : null;
301 $hint = isset( $group_item['hint'] ) ? wp_kses( $group_item['hint'], 'default' ) : $hint;
302
303 $value = isset( $group_item[0] ) ? esc_attr( $group_item[0] ) : null;
304 $value = isset( $group_item['value'] ) ? esc_attr( $group_item['value'] ) : $value;
305
306 $this->printHint( $hint, $value, $value !== $value );
307 }
308 } else {
309 $hint = isset( $item[2] ) ? esc_attr( $item[2] ) : null;
310 $hint = isset( $item['hint'] ) ? esc_attr( $item['hint'] ) : $hint;
311
312 $value = isset( $item[0] ) ? esc_attr( $item[0] ) : null;
313 $value = isset( $item['value'] ) ? esc_attr( $item['value'] ) : $value;
314
315 $this->printHint( $hint, $value, $value !== $value );
316 }
317 }
318 ?>
319 </div>
320 <?php } ?>
321 <?php
322 }
323
324 /**
325 * Print single hint markup
326 *
327 * @since 4.1.0
328 *
329 * @param string $hint
330 *
331 * @return void
332 */
333 protected function printHint( $hint, $name, $is_visible = false ) {
334
335 if ( ! empty( $hint ) ) {
336 $styles = ( $is_visible ) ? 'style="display: none;"' : '';
337
338 ?>
339 <div style="display: none;" class="factory-hint factory-hint-<?php echo esc_attr( $name ); ?>"<?php echo $styles; ?>><?php echo $hint; ?></div>
340 <?php
341 }
342 }
343
344 /**
345 * @param array $items
346 * @param null $selected
347 */
348 protected function printItems( $items, $selected = null ) {
349
350 foreach ( (array) $items as $item ) {
351
352 $subitems = [];
353 $data = null;
354
355 // this item is an associative array
356 if ( isset( $item['type'] ) || isset( $item['value'] ) ) {
357
358 $type = isset( $item['type'] ) ? $item['type'] : 'option';
359
360 if ( 'group' === $type ) {
361 $subitems = isset( $item['items'] ) ? $item['items'] : [];
362 }
363
364 $value = isset( $item['value'] ) ? $item['value'] : '';
365 $title = isset( $item['title'] ) ? $item['title'] : '- ' . __( 'empty', 'robin-image-optimizer' ) . ' -';
366
367 $data = isset( $item['data'] ) ? $item['data'] : null;
368 } else {
369
370 $type = ( count( $item ) == 3 && $item[0] === 'group' ) ? 'group' : 'option';
371 if ( 'group' === $type ) {
372 $subitems = $item[2];
373 }
374
375 $title = $item[1];
376 $value = esc_attr( $item[0] );
377 }
378
379 if ( 'group' === $type ) {
380 ?>
381 <optgroup label="<?php echo $title; ?>">
382 <?php $this->printItems( $subitems, $selected ); ?>
383 </optgroup>
384 <?php
385 } else {
386
387 $attr = ( $selected == $value ) ? 'selected="selected"' : '';
388
389 $strData = '';
390 if ( ! empty( $data ) ) {
391
392 foreach ( $data as $key => $values ) {
393 $strData = $strData . ' data-' . $key . '="' . ( is_array( $values ) ? implode( ',', $values ) : $values ) . '"';
394 }
395 }
396
397 ?>
398 <option value='<?php echo $value; ?>' <?php echo $attr; ?> <?php echo $strData; ?>>
399 <?php echo $title; ?>
400 </option>
401 <?php
402 }
403 }
404 }
405 }
406 }