PluginProbe
Disable Comments & Delete All Comments / 1.3.0
Disable Comments & Delete All Comments v1.3.0
1.3.3 1.3.2 trunk 1.0.0 1.0.4 1.0.5 1.0.8 1.0.9 1.1.1 1.1.3 1.1.5 1.1.6 1.1.7 1.1.8 1.1.9 1.3.0 1.3.1
comments-plus / libs / factory / forms / controls / dropdown.php

dropdown.php in Disable Comments & Delete All Comments 1.3.0, at libs/factory/forms/controls/dropdown.php

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