PluginProbe ʕ •ᴥ•ʔ
Strong Testimonials / 3.3.2
Strong Testimonials v3.3.2
3.3.2 3.3.1 trunk 1.0.1 2.30.9 2.31.10 2.32 2.32.1 2.32.2 2.32.3 2.32.4 2.33 2.34 2.35 2.36 2.37 2.38 2.38.1 2.39 2.39.1 2.39.2 2.39.3 2.40.0 2.40.1 2.40.2 2.40.3 2.40.4 2.40.5 2.40.6 2.40.7 2.41.0 2.41.1 2.50.0 2.50.1 2.50.2 2.50.3 2.50.4 2.51.0 2.51.1 2.51.2 2.51.3 2.51.4 2.51.5 2.51.6 2.51.7 2.51.8 2.51.9 3.0.0 3.0.1 3.0.2 3.0.3 3.1.0 3.1.1 3.1.10 3.1.11 3.1.12 3.1.13 3.1.14 3.1.15 3.1.16 3.1.17 3.1.18 3.1.19 3.1.2 3.1.20 3.1.3 3.1.4 3.1.5 3.1.6 3.1.7 3.1.8 3.1.9 3.2.0 3.2.1 3.2.10 3.2.11 3.2.12 3.2.13 3.2.14 3.2.15 3.2.16 3.2.17 3.2.18 3.2.19 3.2.2 3.2.20 3.2.21 3.2.22 3.2.3 3.2.4 3.2.5 3.2.6 3.2.7 3.2.8 3.2.9 3.3.0
strong-testimonials / includes / functions-template-form.php
strong-testimonials / includes Last commit date
elementor 1 year ago logs 1 month ago strong-testimonials-beaver-block 1 year ago submodules 8 months ago class-strong-gutemberg.php 1 year ago class-strong-log.php 1 year ago class-strong-mail.php 1 year ago class-strong-testimonials-average-shortcode.php 1 year ago class-strong-testimonials-count-shortcode.php 1 year ago class-strong-testimonials-defaults.php 1 month ago class-strong-testimonials-form.php 1 month ago class-strong-testimonials-order.php 1 year ago class-strong-testimonials-privacy.php 1 year ago class-strong-testimonials-render.php 1 month ago class-strong-testimonials-templates.php 1 year ago class-strong-testimonials-view-shortcode.php 1 week ago class-strong-testimonials-view-widget.php 1 year ago class-strong-view-display.php 1 day ago class-strong-view-form.php 1 day ago class-strong-view-slideshow.php 1 day ago class-strong-view.php 1 day ago class-walker-strong-category-checklist-front.php 1 year ago deprecated.php 1 year ago filters.php 1 month ago functions-activation.php 1 month ago functions-content.php 11 months ago functions-image.php 5 months ago functions-rating.php 1 year ago functions-template-form.php 1 week ago functions-template.php 4 months ago functions-views.php 1 year ago functions.php 1 month ago l10n-polylang.php 1 year ago l10n-wpml.php 1 year ago post-types.php 1 week ago retro.php 1 year ago scripts.php 1 month ago
functions-template-form.php
674 lines
1 <?php
2 /**
3 * Form template functions.
4 */
5
6 function wpmtst_form_info() {
7 // Assemble list of field properties for validation script.
8 $form_id = ( WPMST()->atts( 'form_id' ) ) ? WPMST()->atts( 'form_id' ) : 1;
9 $form_fields = wpmtst_get_form_fields( $form_id );
10 $fields = array();
11
12 foreach ( $form_fields as $field ) {
13 $fields[] = array(
14 'name' => $field['name'],
15 'type' => $field['input_type'],
16 'required' => $field['required'] ?? null,
17 );
18
19 // Load rating stylesheet if necessary.
20 if ( isset( $field['input_type'] ) && 'rating' === $field['input_type'] ) {
21 WPMST()->render->add_style( 'wpmtst-rating-form' );
22 }
23 }
24 $form_options = get_option( 'wpmtst_form_options' );
25
26 // Assemble script variable.
27 // Remember: top level is converted to strings!
28 $scroll = array(
29 'onError' => $form_options['scrolltop_error'] ? true : false,
30 'onErrorOffset' => $form_options['scrolltop_error_offset'],
31 'onSuccess' => $form_options['scrolltop_success'] ? true : false,
32 'onSuccessOffset' => $form_options['scrolltop_success_offset'],
33 );
34
35 $args = array(
36 'scroll' => $scroll,
37 'fields' => $fields,
38 );
39
40 if ( WPMST()->atts( 'form_ajax' ) ) {
41 $args['ajaxUrl'] = admin_url( 'admin-ajax.php' );
42 }
43
44 echo 'class="wpmtst-submission-form" method="post" enctype="multipart/form-data" autocomplete="off" data-config="' . esc_attr( json_encode( $args ) ) . '" data-formid = "' . esc_attr( WPMST()->atts( 'form_id' ) ) . '"';
45 }
46
47 function wpmtst_form_setup() {
48 $form_values = WPMST()->form->get_form_values();
49 $cats = (array) $form_values['category'];
50
51 echo '<div style="display: none;">';
52 wp_nonce_field( 'wpmtst_form_action', 'wpmtst_form_nonce', true, true );
53 echo '<input type="hidden" name="action" value="wpmtst_form">';
54 echo '<input type="hidden" name="form_id" value="' . esc_attr( WPMST()->atts( 'form_id' ) ) . '">';
55 echo '<input type="hidden" name="default_category" value="' . esc_attr( WPMST()->atts( 'category' ) ) . '">';
56 echo '<input type="hidden" name="view_id" value="' . esc_attr( WPMST()->atts( 'view' ) ) . '">';
57 echo '<input type="hidden" name="category" value="' . implode( ',', array_map( 'esc_html', $cats ) ) . '">';
58 echo '</div>';
59 }
60
61 function wpmtst_form_message( $part ) {
62 echo wp_kses_post( wpmtst_get_form_message( $part ) );
63 }
64
65 function wpmtst_get_form_message( $part ) {
66 $form_options = get_option( 'wpmtst_form_options' );
67 $messages = $form_options['messages'];
68 if ( isset( $messages[ $part ]['text'] ) ) {
69 return apply_filters( 'wpmtst_form_message', $messages[ $part ]['text'], $messages[ $part ] );
70 }
71
72 return '';
73 }
74
75 function wpmtst_form_message_l10n( $text, $message ) {
76 $text = apply_filters( 'wpmtst_l10n', $text, 'strong-testimonials-form-messages', $message['description'] );
77
78 return $text;
79 }
80 add_filter( 'wpmtst_form_message', 'wpmtst_form_message_l10n', 10, 2 );
81
82
83 function wpmtst_all_form_fields( $fields = null ) {
84 if ( ! $fields ) {
85 $fields = wpmtst_get_form_fields( WPMST()->atts( 'form_id' ) );
86 }
87
88 foreach ( $fields as $key => $field ) {
89 wpmtst_single_form_field( $field );
90 }
91 }
92
93 function wpmtst_form_field( $field_name ) {
94 $fields = wpmtst_get_form_fields( WPMST()->atts( 'form_id' ) );
95
96 foreach ( $fields as $key => $field ) {
97 if ( $field['name'] === $field_name ) {
98 wpmtst_single_form_field( $field );
99 }
100 }
101 }
102
103 function wpmtst_single_form_field( $field ) {
104 $form_values = WPMST()->form->get_form_values();
105 $form_values = apply_filters( 'get_predefined_values', $form_values, $field );
106
107 echo '<div class="' . esc_attr( wpmtst_field_group_classes( $field['input_type'], $field['name'] ) ) . '">';
108
109 if ( 'checkbox' !== $field['input_type'] ) {
110 if ( ! isset( $field['show_label'] ) || $field['show_label'] ) {
111 printf( '<label for="wpmtst_%s" class="%s">%s</label>', esc_html( $field['name'] ), esc_attr( wpmtst_field_label_classes( $field['input_type'], $field['name'] ) ), wp_kses_post( wpmtst_form_field_meta_l10n( $field['label'], $field, 'label' ) ) );
112
113 if ( isset( $field['required'] ) && $field['required'] ) {
114 wpmtst_field_required_symbol();
115 }
116 }
117 wpmtst_field_before( $field );
118 }
119
120 // Check for callback first.
121 if ( isset( $field['action_input'] ) && $field['action_input'] ) {
122 $value = ( isset( $form_values[ $field['name'] ] ) && $form_values[ $field['name'] ] ) ? $form_values[ $field['name'] ] : '';
123 do_action( $field['action_input'], $field, $value );
124 } else {
125
126 // Check field type.
127 switch ( $field['input_type'] ) {
128 case 'category-selector':
129 $value = isset( $form_values[ $field['name'] ] ) ? (array) $form_values[ $field['name'] ] : array();
130
131 echo '<div class="field-wrap">';
132 printf(
133 '<select id="wpmtst_%s" name="%s" class="%s" %s tabindex="0">',
134 esc_attr( $field['name'] ),
135 esc_attr( $field['name'] ),
136 esc_attr( wpmtst_field_classes( $field['input_type'], $field['name'] ) ),
137 esc_attr( wpmtst_field_required_tag( $field ) )
138 );
139
140 echo '<option value="">&mdash;</option>';
141 wpmtst_nested_cats( $value );
142 echo '</select>';
143 echo '</div>';
144 break;
145
146 case 'category-checklist':
147 $value = isset( $form_values[ $field['name'] ] ) ? (array) $form_values[ $field['name'] ] : array();
148 echo '<div class="field-wrap">';
149 wpmtst_form_category_checklist_frontend( $value );
150 echo '</div>';
151 break;
152
153 case 'textarea':
154 $value = ( isset( $form_values[ $field['name'] ] ) && $form_values[ $field['name'] ] ) ? $form_values[ $field['name'] ] : '';
155 // textarea tags must be on same line for placeholder to work
156 $max_length = wpmtst_field_length( $field );
157 if ( isset( $max_length ) && ! empty( $max_length ) ) {
158 printf( '<span class="after max-length-counter" align="right">0 characters out of %s</span>', absint( $field['max_length'] ) );
159 }
160 printf(
161 '<textarea id="wpmtst_%s" name="%s" class="%s" %s placeholder="%s" %s tabindex="0">%s</textarea>',
162 esc_html( $field['name'] ),
163 esc_html( $field['name'] ),
164 esc_attr( wpmtst_field_classes( $field['input_type'], $field['name'], $max_length ) ),
165 esc_attr( wpmtst_field_required_tag( $field ) ),
166 esc_attr( wpmtst_field_placeholder( $field ) ),
167 wpmtst_field_length( $field ),
168 esc_textarea( $value )
169 );
170 break;
171
172 case 'file':
173 echo '<div class="field-wrap">';
174 echo '<input id="wpmtst_' . esc_attr( $field['name'] ) . '" type="file" name="' . esc_attr( $field['name'] ) . '"' . esc_attr( wpmtst_field_required_tag( $field ) ) . ' tabindex="0">';
175 echo '</div>';
176 break;
177
178 case 'shortcode':
179 if ( isset( $field['shortcode_on_form'] ) && $field['shortcode_on_form'] ) {
180 echo do_shortcode( $field['shortcode_on_form'], true );
181 }
182 break;
183
184 case 'rating':
185 if ( isset( $form_values[ $field['name'] ] ) && ! empty( $form_values[ $field['name'] ] ) ) {
186 $field['default_form_value'] = $form_values[ $field['name'] ];
187 }
188 wpmtst_star_rating_form( $field, $field['default_form_value'], 'in-form' );
189 break;
190
191 case 'checkbox':
192 if ( isset( $form_values[ $field['name'] ] ) && ! empty( $form_values[ $field['name'] ] ) ) {
193 $field['default_form_value'] = $form_values[ $field['name'] ];
194 }
195
196 if ( ! isset( $field['show_label'] ) || $field['show_label'] ) {
197 printf(
198 '<label for="wpmtst_%s" class="%s">%s</label>',
199 esc_attr( $field['name'] ),
200 esc_attr( wpmtst_field_label_classes( $field['input_type'], $field['name'] ) ),
201 esc_html( wpmtst_form_field_meta_l10n( $field['label'], $field, 'label' ) )
202 );
203 }
204
205 wpmtst_field_before( $field );
206
207 echo '<div class="field-wrap">';
208
209 printf(
210 '<input id="wpmtst_%s" type="%s" class="%s" name="%s" %s %s tabindex="0">',
211 esc_attr( $field['name'] ),
212 esc_attr( $field['input_type'] ),
213 esc_attr( wpmtst_field_classes( $field['input_type'], $field['name'] ) ),
214 esc_attr( $field['name'] ),
215 esc_attr( wpmtst_field_required_tag( $field ) ),
216 checked( $field['default_form_value'], 1, false )
217 );
218
219 if ( isset( $field['text'] ) ) {
220 echo '<label for="wpmtst_' . esc_attr( $field['name'] ) . '" class="checkbox-label">' . wp_kses_post( wpmtst_form_field_meta_l10n( $field['text'], $field, 'text' ) ) . '</label>';
221 if ( isset( $field['required'] ) && $field['required'] ) {
222 wpmtst_field_required_symbol();
223 }
224 }
225
226 echo '</div>';
227 break;
228
229 default: // text, email, url
230 $max_length = wpmtst_field_length( $field );
231 if ( isset( $max_length ) && ! empty( $max_length ) ) {
232 printf( '<span class="after max-length-counter" align="right">0 characters out of %s</span>', absint( $field['max_length'] ) );
233 }
234 printf(
235 '<input id="wpmtst_%s" type="%s" class="%s" name="%s" %s placeholder="%s" %s %s tabindex="0">',
236 esc_html( $field['name'] ),
237 esc_attr( $field['input_type'] ),
238 esc_attr( wpmtst_field_classes( $field['input_type'], $field['name'], $max_length ) ),
239 esc_html( $field['name'] ),
240 wpmtst_field_value( $field, $form_values ),
241 esc_attr( wpmtst_field_placeholder( $field ) ),
242 wpmtst_field_length( $field ),
243 esc_attr( wpmtst_field_required_tag( $field ) )
244 );
245 }
246 }
247
248 wpmtst_field_after( $field );
249 wpmtst_field_error( $field );
250 echo '</div>' . "\n";
251 }
252
253 /**
254 * Assemble form field group CSS classes.
255 *
256 * @param null $type
257 * @param null $name
258 * @since 2.32.0
259 *
260 * @return string
261 */
262 function wpmtst_field_group_classes( $type, $name ) {
263 $class_list = array(
264 'form-field',
265 );
266
267 if ( $name ) {
268 $class_list[] = "field-$name";
269 }
270
271 return apply_filters( 'wpmtst_form_field_group_class', implode( ' ', $class_list ), $type, $name );
272 }
273
274 /**
275 * Assemble form field label CSS classes.
276 *
277 * @param null $type
278 * @param null $name
279 * @since 2.32.0
280 *
281 * @return string
282 */
283 function wpmtst_field_label_classes( $type, $name ) {
284 $class_list = array();
285
286 if ( $name ) {
287 $class_list[] = "field-$name";
288 }
289
290 return apply_filters( 'wpmtst_form_field_label_class', implode( ' ', $class_list ), $type, $name );
291 }
292
293 /**
294 * Assemble form field CSS classes.
295 *
296 * @param null $type
297 * @param null $name
298 *
299 * @return string
300 */
301 function wpmtst_field_classes( $type = null, $name = null, $max_length = null ) {
302 $errors = WPMST()->form->get_form_errors();
303 $class_list = array();
304 switch ( $type ) {
305 case 'email':
306 $class_list[] = 'text';
307 $class_list[] = 'email';
308 break;
309 case 'url':
310 $class_list[] = 'text';
311 $class_list[] = 'url';
312 break;
313 case 'text':
314 $class_list[] = 'text';
315 break;
316 case 'textarea':
317 $class_list[] = 'textarea';
318 break;
319 default:
320 break;
321 }
322
323 if ( isset( $max_length ) && ! empty( $max_length ) ) {
324 $class_list[] = 'max-length';
325 }
326
327 if ( isset( $errors[ $name ] ) ) {
328 $class_list[] = 'error';
329 }
330
331 return apply_filters( 'wpmtst_form_field_class', implode( ' ', $class_list ), $type, $name );
332 }
333
334 /**
335 * Display default value if no value submitted.
336 *
337 * @param $field
338 * @param $form_values
339 *
340 * @since 2.19.1 wpmtst_field_value filter
341 *
342 * @return string
343 */
344 function wpmtst_field_value( $field, $form_values ) {
345 $value = '';
346 if ( isset( $form_values[ $field['name'] ] ) && $form_values[ $field['name'] ] ) {
347 $value = $form_values[ $field['name'] ];
348 } elseif ( isset( $field['default_form_value'] ) && $field['default_form_value'] ) {
349 $value = $field['default_form_value'];
350 }
351
352 $value = wpmtst_do_user_placeholders( $value, $field );
353
354 $value = apply_filters( 'wpmtst_field_value', $value, $field, $form_values );
355
356 return ' value="' . esc_attr( $value ) . '"';
357 }
358
359 function wpmtst_do_user_placeholders( $text, $field = false ) {
360 if ( is_user_logged_in() ) {
361 $current_user = wp_get_current_user();
362
363 if ( $field && isset( $field['input_type'] ) && 'email' === $field['input_type'] && apply_filters( 'wpmtst_add_default_email', true, $field ) ) {
364 return $current_user->user_email;
365 }
366
367 $placeholders = array(
368 '{user_name}' => $current_user->user_login,
369 '{user_first_name}' => $current_user->user_firstname,
370 '{user_last_name}' => $current_user->user_lastname,
371 '{user_email}' => $current_user->user_email,
372 );
373 } else {
374 $placeholders = array(
375 '{user_name}' => '',
376 '{user_first_name}' => '',
377 '{user_last_name}' => '',
378 '{user_email}' => '',
379 );
380 }
381
382 return strtr( $text, $placeholders );
383 }
384
385
386 /**
387 * Print placeholder tag.
388 *
389 * @param $field
390 *
391 * @return string
392 */
393 function wpmtst_field_placeholder( $field ) {
394 if ( isset( $field['placeholder'] ) && $field['placeholder'] ) {
395 return esc_attr( wpmtst_form_field_meta_l10n( $field['placeholder'], $field, 'placeholder' ) );
396 }
397
398 return '';
399 }
400
401 /**
402 * Print placeholder tag.
403 *
404 * @param $field
405 *
406 * @return string
407 */
408 function wpmtst_field_length( $field ) {
409 if ( isset( $field['max_length'] ) && $field['max_length'] ) {
410 return 'maxlength="' . esc_attr( $field['max_length'] ) . '"';
411 }
412
413 return '';
414 }
415
416 /**
417 * HTML tag: required
418 *
419 * @param $field
420 * @return string
421 */
422 function wpmtst_field_required_tag( $field ) {
423 if ( isset( $field['required'] ) && apply_filters( 'wpmtst_field_required_tag', $field['required'] ) ) {
424 return ' required';
425 }
426
427 return '';
428 }
429
430 /**
431 * Print "Required" notice.
432 *
433 * @since 2.23.0
434 * @since 2.24.1 Print only if enabled.
435 */
436 function wpmtst_field_required_notice() {
437 $html = '';
438 $form_options = get_option( 'wpmtst_form_options' );
439 $notice = $form_options['messages']['required-field'];
440 if ( isset( $notice['enabled'] ) && $notice['enabled'] ) {
441 ob_start();
442 ?>
443 <p class="required-notice">
444 <?php wpmtst_field_required_symbol(); ?><?php wpmtst_form_message( 'required-field' ); ?>
445 </p>
446 <?php
447 $html = ob_get_clean();
448 }
449 // Echo even if disabled to allow a custom notice.
450 echo( apply_filters( 'wpmtst_field_required', $html ) );
451 }
452
453 /**
454 * Print required field symbol.
455 */
456 function wpmtst_field_required_symbol() {
457 echo wp_kses_post( apply_filters( 'wpmtst_field_required_symbol', '<span class="required symbol"></span>' ) );
458 }
459
460 /**
461 * Print form field "before" value.
462 *
463 * @param $field
464 */
465 function wpmtst_field_before( $field ) {
466 $before = wpmtst_get_form_field_meta( $field, 'before' );
467 if ( $before ) {
468 echo '<span class="before">' . wp_kses_post( $before ) . '</span>';
469 }
470 }
471
472 /**
473 * Print form field "after" value.
474 *
475 * @param $field
476 */
477 function wpmtst_field_after( $field ) {
478 $after = wpmtst_get_form_field_meta( $field, 'after' );
479 echo '<span class="after">' . wp_kses_post( $after ) . '</span>';
480 }
481
482 /**
483 * Get form field meta value.
484 *
485 * @param $field
486 * @param $meta
487 *
488 * @return mixed|string
489 */
490 function wpmtst_get_form_field_meta( $field, $meta ) {
491 if ( isset( $field[ $meta ] ) && $field[ $meta ] ) {
492 return apply_filters( 'wpmtst_form_field_meta', $field[ $meta ], $field, $meta );
493 }
494
495 return '';
496 }
497
498 /**
499 * Return localized form field meta value.
500 *
501 * @param $field_meta
502 * @param $field
503 * @param $meta
504 *
505 * @return mixed
506 */
507 function wpmtst_form_field_meta_l10n( $field_meta, $field, $meta ) {
508 return apply_filters( 'wpmtst_l10n', $field_meta, 'strong-testimonials-form-fields', $field['name'] . ' : ' . $meta );
509 }
510 add_filter( 'wpmtst_form_field_meta', 'wpmtst_form_field_meta_l10n', 10, 3 );
511 add_filter( 'wpmtst_form_field_meta', 'do_shortcode' );
512
513
514 function wpmtst_field_error( $field ) {
515 $errors = WPMST()->form->get_form_errors();
516 if ( isset( $errors[ $field['name'] ] ) ) {
517 echo '<span class="error">' . esc_html( $errors[ $field['name'] ] ) . '</span>';
518 }
519 }
520
521
522 /**
523 * Print the submit button.
524 *
525 * @param bool $preview
526 */
527 function wpmtst_form_submit_button( $preview = false ) {
528 ?>
529 <div class="form-field wpmtst-submit">
530 <label><input type="<?php echo $preview ? 'button' : 'submit'; ?>" class="wpmtst_submit_testimonial" name="wpmtst_submit_testimonial" value="<?php echo esc_attr( wpmtst_get_form_message( 'form-submit-button' ) ); ?>" class="<?php echo esc_attr( apply_filters( 'wpmtst_submit_button_class', 'button' ) ); ?>" tabindex="0"></label>
531 </div>
532 <?php
533 }
534
535 /**
536 * Print a category checklist.
537 *
538 * @since 2.17.0
539 * @param array $default_cats
540 */
541 function wpmtst_form_category_checklist_frontend( $default_cats = array() ) {
542 ?>
543 <div class="strong-category-list-panel">
544 <ul class="strong-category-list">
545 <?php
546 $args = array(
547 'selected_cats' => $default_cats,
548 'checked_ontop' => false,
549 );
550 ?>
551 <?php wpmtst_terms_checklist( $args ); ?>
552 </ul>
553 </div>
554 <?php
555 }
556
557 /**
558 * Output an unordered list of checkbox input elements labelled with term names.
559 *
560 * Copied wp_terms_checklist().
561 *
562 * @since 2.16.4
563 *
564 * @param array|string $args {
565 * Optional. Array or string of arguments for generating a terms checklist. Default empty array.
566 *
567 * @type int $descendants_and_self ID of the category to output along with its descendants.
568 * Default 0.
569 * @type array $selected_cats List of categories to mark as checked. Default false.
570 * @type array $popular_cats List of categories to receive the "popular-category" class.
571 * Default false.
572 * @type object $walker Walker object to use to build the output.
573 * Default is a Walker_Strong_Category_Checklist_Front instance.
574 * @type string $taxonomy Taxonomy to generate the checklist for. Default 'wpm-testimonial-category'.
575 * @type bool $checked_ontop Whether to move checked items out of the hierarchy and to
576 * the top of the list. Default true.
577 * @type bool $echo Whether to echo the generated markup. False to return the markup instead
578 * of echoing it. Default true.
579 * }
580 *
581 * @return string
582 */
583 function wpmtst_terms_checklist( $args = array() ) {
584 $defaults = array(
585 'descendants_and_self' => 0,
586 'selected_cats' => false,
587 'popular_cats' => false,
588 'walker' => null,
589 'taxonomy' => 'wpm-testimonial-category',
590 'checked_ontop' => true,
591 'echo' => true,
592 );
593
594 $params = apply_filters( 'wpmtst_terms_checklist_args', $args );
595
596 $r = wp_parse_args( $params, $defaults );
597
598 if ( empty( $r['walker'] ) || ! ( $r['walker'] instanceof Walker ) ) {
599 $walker = new Walker_Strong_Category_Checklist_Front();
600 } else {
601 $walker = $r['walker'];
602 }
603
604 $taxonomy = $r['taxonomy'];
605 $descendants_and_self = (int) $r['descendants_and_self'];
606
607 $args = array( 'taxonomy' => $taxonomy );
608
609 if ( is_array( $r['selected_cats'] ) ) {
610 $args['selected_cats'] = $r['selected_cats'];
611 } else {
612 $args['selected_cats'] = array();
613 }
614
615 if ( is_array( $r['popular_cats'] ) ) {
616 $args['popular_cats'] = $r['popular_cats'];
617 } else {
618 $args['popular_cats'] = get_terms(
619 $taxonomy,
620 array(
621 'fields' => 'ids',
622 'orderby' => 'count',
623 'order' => 'DESC',
624 'number' => 10,
625 'hierarchical' => false,
626 )
627 );
628 }
629
630 // Select a _single_ sibling and its descendants.
631 // Assembling a list of _multiple_ siblings would go here.
632 if ( $descendants_and_self ) {
633 $categories = (array) get_terms(
634 $taxonomy,
635 array(
636 'child_of' => $descendants_and_self,
637 'hierarchical' => 0,
638 'hide_empty' => 0,
639 )
640 );
641 $self = get_term( $descendants_and_self, $taxonomy );
642 array_unshift( $categories, $self );
643 } else {
644 $categories = (array) get_terms( $taxonomy, array( 'get' => 'all' ) );
645 }
646
647 $output = '';
648
649 if ( $r['checked_ontop'] ) {
650 // Post-process $categories rather than adding an exclude to the get_terms() query
651 // to keep the query the same across all posts (for any query cache)
652 $checked_categories = array();
653 $keys = array_keys( $categories );
654
655 foreach ( $keys as $k ) {
656 if ( in_array( $categories[ $k ]->term_id, $args['selected_cats'], true ) ) {
657 $checked_categories[] = $categories[ $k ];
658 unset( $categories[ $k ] );
659 }
660 }
661
662 // Put checked cats on top
663 $output .= call_user_func_array( array( $walker, 'walk' ), array( $checked_categories, 0, $args ) );
664 }
665 // Then the rest of them
666 $output .= call_user_func_array( array( $walker, 'walk' ), array( $categories, 0, $args ) );
667
668 if ( $r['echo'] ) {
669 echo $output;
670 }
671
672 return $output;
673 }
674