PluginProbe
Post Grid Gutenberg Blocks – PostX / 4.1.34
Post Grid Gutenberg Blocks – PostX v4.1.34
5.0.39 5.0.38 5.0.37 5.0.36 5.0.35 5.0.34 5.0.33 5.0.32 5.0.31 5.0.30 5.0.29 5.0.28 5.0.27 5.0.26 5.0.25 5.0.23 5.0.24 5.0.22 5.0.21 5.0.20 5.0.19 5.0.18 5.0.17 2.1.1 2.1.2 All 237 releases
ultimate-post / blocks / Advanced_Filter.php

Advanced_Filter.php in Post Grid Gutenberg Blocks – PostX 4.1.34, at blocks/Advanced_Filter.php

755 lines 19.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace ULTP\blocks;
3
4 use WP_Taxonomy;
5
6 defined( 'ABSPATH' ) || exit;
7
8 /**
9 * Class Advanced_Filter
10 */
11 class Advanced_Filter {
12
13 private $pro_select_types = array(
14 'adv_sort',
15 'custom_tax',
16 );
17
18 public $order = array(
19 array(
20 'id' => 'DESC',
21 'name' => 'DESC',
22 ),
23 array(
24 'id' => 'ASC',
25 'name' => 'ASC',
26 ),
27 );
28
29 public $order_by = array(
30 array(
31 'id' => 'date',
32 'name' => 'Created Date',
33 ),
34 array(
35 'id' => 'modified',
36 'name' => 'Date Modified',
37 ),
38 array(
39 'id' => 'title',
40 'name' => 'Title',
41 ),
42 array(
43 'id' => 'menu_order',
44 'name' => 'Menu Order',
45 ),
46 array(
47 'id' => 'rand',
48 'name' => 'Random',
49 ),
50 // array(
51 // 'id' => 'post__in',
52 // 'name' => 'Post In',
53 // ),
54 array(
55 'id' => 'comment_count',
56 'name' => 'Number of Comments',
57 ),
58 );
59
60
61 private function filter_select_options( $options, $filter ) {
62 $res = array();
63 $map = array();
64
65 foreach ( $options as $option ) {
66 $map[ $option['id'] ] = $option['name'];
67 }
68
69 foreach ( $filter as $f ) {
70
71 if ( isset( $map[ $f ] ) ) {
72 $res[] = array(
73 'id' => strtolower( $f ),
74 'name' => $map[ $f ],
75 );
76 }
77 }
78
79 return $res;
80 }
81
82 /**
83 * Advanced_Filter constructor.
84 */
85 public function __construct() {
86
87 add_action( 'init', array( $this, 'register' ) );
88 }
89
90 private function get_adv_filter_options() {
91 return array(
92 array(
93 'id' => 'popular_post_1_day_view',
94 'name' => __('Trending Today', 'ultimate-post'),
95 ),
96 array(
97 'id' => 'popular_post_7_days_view',
98 'name' => __('This Week’s Popular Posts', 'ultimate-post'),
99 ),
100 array(
101 'id' => 'popular_post_30_days_view',
102 'name' => __('Top Posts of the Month', 'ultimate-post'),
103 ),
104 array(
105 'id' => 'popular_post_all_times_view',
106 'name' => __('All-Time Favorites', 'ultimate-post'),
107 ),
108 array(
109 'id' => 'random_post',
110 'name' => __('Random Posts', 'ultimate-post'),
111 ),
112 array(
113 'id' => 'random_post_7_days',
114 'name' => __('Random Posts (7 Days)', 'ultimate-post'),
115 ),
116 array(
117 'id' => 'random_post_30_days',
118 'name' => __('Random Posts (30 Days)', 'ultimate-post'),
119 ),
120 array(
121 'id' => 'latest_post_published',
122 'name' => __('Latest Posts - Published Date', 'ultimate-post'),
123 ),
124 array(
125 'id' => 'latest_post_modified',
126 'name' => __('Latest Posts - Last Modified Date', 'ultimate-post'),
127 ),
128 array(
129 'id' => 'oldest_post_published',
130 'name' => __('Oldest Posts - Published Date', 'ultimate-post'),
131 ),
132 array(
133 'id' => 'oldest_post_modified',
134 'name' => __('Oldest Posts - Last Modified Date', 'ultimate-post'),
135 ),
136 array(
137 'id' => 'alphabet_asc',
138 'name' => __('Alphabetical ASC', 'ultimate-post'),
139 ),
140 array(
141 'id' => 'alphabet_desc',
142 'name' => __('Alphabetical DESC', 'ultimate-post'),
143 ),
144 array(
145 'id' => 'sticky_posts',
146 'name' => __('Sticky Post', 'ultimate-post'),
147 ),
148 array(
149 'id' => 'most_comment',
150 'name' => __('Most Comments', 'ultimate-post'),
151 ),
152 array(
153 'id' => 'most_comment_1_day',
154 'name' => __('Most Comments (1 Day)', 'ultimate-post'),
155 ),
156 array(
157 'id' => 'most_comment_7_days',
158 'name' => __('Most Comments (7 Days)', 'ultimate-post'),
159 ),
160 array(
161 'id' => 'most_comment_30_days',
162 'name' => __('Most Comments (30 Days)', 'ultimate-post'),
163 ),
164 );
165 }
166
167 public function get_select_attributes() {
168 return array(
169 'advanceId' => '',
170 'blockId' => '',
171 'advanceCss' => '',
172 'filterStyle' => 'dropdown',
173 'filterValues' => '["_all"]',
174 'dropdownOptionsType' => 'all',
175 'dropdownOptions' => '["_all"]',
176 'allText' => 'All',
177 'sPlaceholderText' => 'Search...',
178 'searchEnabled' => false,
179 );
180 }
181
182 public function get_search_attributes() {
183 return array(
184 'advanceId' => '',
185 'blockId' => '',
186 'advanceCss' => '',
187 'placeholder' => 'Search...',
188 );
189 }
190
191 public function get_clear_attributes() {
192 return array(
193 'advanceId' => '',
194 'blockId' => '',
195 'clearButtonText' => 'Clear Filters',
196 'clearButtonPosition' => 'normal',
197 'cAlign' => 'margin-bottom: inherit',
198 );
199 }
200
201 public function register() {
202 register_block_type(
203 'ultimate-post/filter-select',
204 array(
205 'editor_script' => 'ultp-blocks-editor-script',
206 'editor_style' => 'ultp-blocks-editor-css',
207 'render_callback' => array( $this, 'select_content' ),
208 )
209 );
210
211 register_block_type(
212 'ultimate-post/filter-search-adv',
213 array(
214 'editor_script' => 'ultp-blocks-editor-script',
215 'editor_style' => 'ultp-blocks-editor-css',
216 'render_callback' => array( $this, 'search_content' ),
217 )
218 );
219
220 register_block_type(
221 'ultimate-post/filter-clear',
222 array(
223 'editor_script' => 'ultp-blocks-editor-script',
224 'editor_style' => 'ultp-blocks-editor-css',
225 'render_callback' => array( $this, 'clear_content' ),
226 )
227 );
228 }
229
230 public function get_button_data( $type, $ids, $post_types = '', $allText = 'All' ) {
231 $res = array();
232
233 if ( in_array( '_all', $ids ) ) {
234 $res['_all'] = $allText;
235 // $ids = array_filter($ids, function ($item) {
236 // return $item != '_all';
237 // });
238 }
239
240 $ids = implode( ',', $ids );
241
242 $adv_sort = $this->get_adv_filter_options();
243
244 switch ( $type ) {
245 case 'adv_sort':
246 $adv_sort_id = explode( ',', $ids );
247 foreach ( $adv_sort as $adv ) {
248 foreach ( $adv_sort_id as $id ) {
249 if ( $adv['id'] === $id ) {
250 $res[ $id ] = $adv['name'];
251 }
252 }
253 }
254 break;
255 case 'category':
256 if ( ! empty( $ids ) && $ids !== '_all' ) {
257 $categories = get_categories(
258 array(
259 'per_page' => -1,
260 'include' => $ids,
261 )
262 );
263
264 foreach ( $categories as $category ) {
265 $res[ $category->slug ] = $category->name;
266 }
267 }
268 break;
269 case 'tags':
270 if ( ! empty( $ids ) && $ids !== '_all' ) {
271 $tags = get_tags(
272 array(
273 'per_page' => -1,
274 'include' => $ids,
275 )
276 );
277
278 foreach ( $tags as $tag ) {
279 $res[ $tag->slug ] = $tag->name;
280 }
281 }
282 break;
283 case 'author':
284 if ( ! empty( $ids ) && $ids !== '_all' ) {
285 $authors = get_users(
286 array(
287 'per_page' => -1,
288 'role__in' => array( 'author' ),
289 'include' => $ids,
290 )
291 );
292
293 foreach ( $authors as $author ) {
294 $res[ $author->ID ] = $author->display_name;
295 }
296 }
297
298 break;
299 case 'order':
300 foreach ( $this->order as $order ) {
301 $res[ $order['id'] ] = $order['name'];
302 }
303 break;
304 case 'order_by':
305 $orders = explode( ',', $ids );
306 foreach ( $this->order_by as $order_by ) {
307 foreach ( $orders as $order ) {
308 if ( $order_by['id'] === $order ) {
309 $res[ $order ] = $order_by['name'];
310 }
311 }
312 }
313 break;
314 case 'custom_tax':
315 if ( $post_types == '' ) {
316 break;
317 }
318
319 $post_types = json_decode( $post_types );
320
321 foreach ( $post_types as $post_type ) {
322 $taxonomies = get_object_taxonomies( $post_type );
323 foreach ( $taxonomies as $taxonomy ) {
324 $terms = get_terms(
325 array(
326 'taxonomy' => $taxonomy,
327 'hide_empty' => false,
328 )
329 );
330 foreach ( $terms as $term ) {
331 $res[ $term->slug ] = array(
332 'name' => $term->name,
333 'taxonomy' => $taxonomy,
334 );
335 }
336 }
337 }
338
339 default:
340 break;
341 }
342
343 return $res;
344 }
345
346 /**
347 * Inserts all option depending on condition
348 *
349 * @param array $arr array.
350 * @param array $item all item.
351 * @param int $idx all item idx.
352 * @param string $mode mode.
353 * @return void
354 */
355 private function maybe_insert_all_option( &$arr, &$item, $idx, $mode ) {
356 if ( 'all' === $mode ) {
357 array_unshift( $arr, $item );
358 } elseif ( ! empty( $item ) && false !== $idx ) {
359 array_splice( $arr, $idx, 0, array( $item ) );
360 }
361 }
362
363 public function get_select_data( $type, $all_text, $post_types = '', $specific = array(), $mode = 'all' ) {
364
365 $all = null;
366 $all_idx = array_search( '_all', $specific, true );
367 $filtered_specific = $specific;
368
369 if ( false !== $all_idx || 'all' === $mode ) {
370 $all = array(
371 'id' => '_all',
372 'name' => $all_text,
373 );
374
375 $filtered_specific = array_filter(
376 $specific,
377 function ( $item ) {
378 return '_all' !== $item;
379 }
380 );
381 }
382
383 $adv_sort = $this->get_adv_filter_options();
384
385 switch ( $type ) {
386
387 case 'category':
388 $categories = array();
389
390 if ( 'all' === $mode || ( 'specific' === $mode && count( $filtered_specific ) > 0 ) ) {
391 $categories = get_categories(
392 array(
393 'per_page' => -1,
394
395 'include' => $filtered_specific,
396 'orderby' => 'include',
397 )
398 );
399
400 $categories = array_map(
401 function ( $category ) {
402 return array(
403 'id' => $category->slug,
404 'name' => $category->name,
405 );
406 },
407 $categories
408 );
409 }
410
411 $this->maybe_insert_all_option( $categories, $all, $all_idx, $mode );
412
413 return $categories;
414
415 case 'tags':
416 $tags = array();
417
418 if ( 'all' === $mode || ( 'specific' === $mode && count( $filtered_specific ) > 0 ) ) {
419 $tags = get_tags(
420 array(
421 'per_page' => -1,
422 'include' => $filtered_specific,
423 )
424 );
425 $tags = array_map(
426 function ( \WP_Term $tag ) {
427 return array(
428 'id' => $tag->slug,
429 'name' => $tag->name,
430 );
431 },
432 $tags
433 );
434 }
435
436 $this->maybe_insert_all_option( $tags, $all, $all_idx, $mode );
437
438 return $tags;
439
440 case 'author':
441 $authors = array();
442
443 if ( 'all' === $mode || ( 'specific' === $mode && count( $filtered_specific ) > 0 ) ) {
444
445 $authors = get_users(
446 array(
447 'per_page' => -1,
448 'include' => $filtered_specific,
449 'role__not_in' => ['subscriber', 'translator'],
450 )
451 );
452
453 $authors = array_map(
454 function ( $author ) {
455 return array(
456 'id' => $author->ID,
457 'name' => $author->display_name,
458 );
459 },
460 $authors
461 );
462 }
463
464 $this->maybe_insert_all_option( $authors, $all, $all_idx, $mode );
465
466 return $authors;
467
468 case 'order':
469 $filtered_specific = array_map( 'strtoupper', $filtered_specific );
470
471 return 'specific' === $mode ?
472 $this->filter_select_options( $this->order, $filtered_specific )
473 : $this->order;
474
475 case 'order_by':
476 return 'specific' === $mode ?
477 $this->filter_select_options( $this->order_by, $filtered_specific )
478 : $this->order_by;
479
480 case 'adv_sort':
481 $data = 'specific' === $mode ?
482 $this->filter_select_options( $adv_sort, $filtered_specific )
483 : $adv_sort;
484
485 $this->maybe_insert_all_option( $data, $all, $all_idx, $mode );
486
487 return $data;
488
489 case 'custom_tax':
490 if ( '' == $post_types ) {
491 return array();
492 }
493
494 $post_types = json_decode( $post_types );
495
496 $data = array();
497
498 foreach ( $post_types as $post_type ) {
499 $taxonomies = get_object_taxonomies( sanitize_text_field( $post_type ) );
500 foreach ( $taxonomies as $taxonomy ) {
501 $terms = array();
502
503 if ( 'specific' === $mode && count( $filtered_specific ) > 0 ) {
504 foreach ( $filtered_specific as $s ) {
505 $res = get_term_by( 'slug', $s, $taxonomy );
506 if ( ! empty( $res ) ) {
507 $terms[] = $res;
508 }
509 }
510 } else {
511 $terms = get_terms(
512 array(
513 'taxonomy' => $taxonomy,
514 'hide_empty' => false,
515 )
516 );
517 }
518
519 foreach ( $terms as $term ) {
520 $data[] = array(
521 'id' => $term->slug,
522 'name' => $term->name,
523 'taxonomy' => $taxonomy,
524 );
525 }
526 }
527 }
528
529 $this->maybe_insert_all_option( $data, $all, $all_idx, $mode );
530
531 return $data;
532 default:
533 return array();
534 }
535 }
536
537 public function select_content( $attr ) {
538 $block_name = 'filter-select';
539 $is_active = ultimate_post()->is_lc_active();
540 $attr = wp_parse_args( $attr, $this->get_select_attributes() );
541
542 if ( ! $is_active && in_array( $attr['type'], $this->pro_select_types ) ) {
543 return '';
544 }
545
546 $post_types = isset( $attr['postTypes'] ) ? $attr['postTypes'] : '';
547
548 $attr['blockId'] = ultimate_post()->sanitize_attr( $attr, 'blockId', 'sanitize_html_class', 'missing_block_id' );
549 $attr['allText'] = ultimate_post()->sanitize_attr( $attr, 'allText', 'sanitize_text_field', 'missing_block_id' );
550
551 if ( 'inline' === $attr['filterStyle'] ) {
552 $inline_values = json_decode( $attr['filterValues'], true );
553
554 if ( ! is_array( $inline_values ) ) {
555 return '';
556 }
557
558 $data = $this->get_button_data( $attr['type'], $inline_values, $post_types, $attr['allText'] );
559
560 $btn_wrapper_attrs = get_block_wrapper_attributes(
561 array(
562 'class' => 'ultp-block-' . $attr['blockId'] . ' ultp-filter-button',
563 'role' => 'button',
564 'data-blockId' => $attr['blockId'],
565 'data-is-active' => 'false',
566 )
567 );
568
569 ob_start();
570 ?>
571
572 <div class="ultp-block-<?php echo esc_attr( $attr['blockId'] ); ?>-wrapper">
573 <?php foreach ( $data as $key => $value ) : ?>
574 <?php
575 if ( is_array( $value ) ) {
576 $name = $value['name'];
577 $tax = isset( $value['taxonomy'] ) ? 'data-tax="' . esc_attr( $value['taxonomy'] ) . '"' : '';
578 } else {
579 $name = $value;
580 $tax = '';
581 }
582 ?>
583 <div <?php echo $btn_wrapper_attrs; ?> <?php echo $tax; ?> data-selected="<?php echo esc_attr( $key ); ?>" data-type="<?php echo esc_attr( $attr['type'] ); ?>">
584 <?php echo $name; ?>
585 </div>
586 <?php endforeach ?>
587 </div>
588 <?php
589
590 $content = ob_get_clean();
591 return $content;
592 } elseif ( 'dropdown' === $attr['filterStyle'] ) {
593
594 $mode = 'all';
595 $specific = array();
596
597 if ( 'specific' === $attr['dropdownOptionsType'] && isset( $attr['dropdownOptions'] ) ) {
598 $specific_data = json_decode( stripslashes( $attr['dropdownOptions'] ) );
599 $mode = 'specific';
600 if ( is_array( $specific_data ) ) {
601 $specific = array_map(
602 function ( $item ) {
603 return sanitize_text_field( $item );
604 },
605 $specific_data
606 );
607 }
608 }
609
610 $data = $this->get_select_data( $attr['type'], $attr['allText'], $post_types, $specific, $mode );
611
612 $def_value = ! empty( current( $data ) ) ? current( $data ) : null;
613
614 $wrapper_attrs = get_block_wrapper_attributes(
615 array(
616 'class' => 'ultp-block-' . $attr['blockId'] . ' ultp-filter-select',
617 'data-selected' => isset( $def_value['id'] ) ? $def_value['id'] : 0,
618 'data-type' => $attr['type'],
619 'data-blockId' => $attr['blockId'],
620 'aria-expanded' => 'false',
621 'aria-label' => 'Select Filter (' . $attr['type'] . ')',
622 )
623 );
624
625 ob_start();
626 ?>
627 <div <?php echo $wrapper_attrs; ?>>
628 <div class="ultp-filter-select-field ultp-filter-select__parent">
629 <span class="ultp-filter-select-field-selected ultp-filter-select__parent-inner">
630 <?php echo esc_html( isset( $def_value['name'] ) ? $def_value['name'] : '' ); ?>
631 </span>
632 <span class="ultp-filter-select-field-icon">
633 <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 34.1 19.95"><path d="M17.05 19.949.601 3.499a2.05 2.05 0 0 1 2.9-2.9l13.551 13.55L30.603.599a2.05 2.05 0 0 1 2.9 2.9Z"/></svg>
634 </span>
635 </div>
636 <ul style="display: none;" class="ultp-filter-select-options ultp-filter-select__dropdown">
637
638 <?php
639 if ( isset( $attr['searchEnabled'] ) && $attr['searchEnabled'] ) :
640 ?>
641 <input
642 type="search"
643 class="ultp-filter-select-search"
644 placeholder="<?php echo esc_attr( isset( $attr['sPlaceholderText'] ) ? $attr['sPlaceholderText'] : '' ); ?>"
645 />
646 <?php endif; ?>
647
648 <?php foreach ( $data as $item ) : ?>
649 <?php
650 $tax = isset( $item['taxonomy'] ) ? 'data-tax="' . esc_attr( $item['taxonomy'] ) . '"' : '';
651 ?>
652 <li class="ultp-filter-select__dropdown-inner" <?php echo $tax; ?> data-id="<?php echo esc_attr( $item['id'] ); ?>">
653 <?php echo esc_html( $item['name'] ); ?>
654 </li>
655 <?php endforeach; ?>
656
657 </ul>
658 </div>
659
660 <?php
661 $content = ob_get_clean();
662 return $content;
663 }
664
665 return '';
666 }
667
668 public function search_content( $attr ) {
669 $block_name = 'filter-search-adv';
670 $is_active = ultimate_post()->is_lc_active();
671
672 if ( ! $is_active ) {
673 return '';
674 }
675
676 $attr = wp_parse_args( $attr, $this->get_search_attributes() );
677
678 $attr['blockId'] = ultimate_post()->sanitize_attr( $attr, 'blockId', 'sanitize_html_class', 'missing_block_id' );
679 $attr['placeholder'] = ultimate_post()->sanitize_attr( $attr, 'placeholder', 'sanitize_text_field' );
680
681 $wrapper_attrs = get_block_wrapper_attributes(
682 array(
683 'class' => 'ultp-block-' . $attr['blockId'] . ' ultp-filter-search',
684 'aria-label' => 'Search Filter',
685 'role' => 'searchbox',
686 )
687 );
688
689 ob_start();
690 ?>
691 <div <?php echo $wrapper_attrs; ?>>
692 <div class="ultp-filter-search-input">
693 <input
694 type="search"
695 placeholder="<?php echo esc_attr( $attr['placeholder'] ); ?>"
696 />
697 <span class="ultp-filter-search-input-icon">
698 <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 47.05 47.05"><path stroke="rgba(0,0,0,0)" strokeMiterlimit="10" d="m43.051 45.948-9.618-9.616a20.183 20.183 0 1 1 2.9-2.9l9.617 9.616a2.05 2.05 0 1 1-2.9 2.9Zm-22.367-9.179A16.084 16.084 0 1 0 4.6 20.684a16.1 16.1 0 0 0 16.084 16.085Z"/></svg>
699 </span>
700 </div>
701 </div>
702 <?php
703 $content = ob_get_clean();
704 return $content;
705 }
706
707 public function clear_content( $attr ) {
708 $block_name = 'filter-clear';
709 // $is_active = ultimate_post()->is_lc_active();
710 $attr = wp_parse_args( $attr, $this->get_clear_attributes() );
711
712 $attr['blockId'] = ultimate_post()->sanitize_attr( $attr, 'blockId', 'sanitize_html_class', 'missing_block_id' );
713 $attr['clearButtonText'] = ultimate_post()->sanitize_attr( $attr, 'clearButtonText', 'sanitize_text_field' );
714
715 $wrapper_attrs = get_block_wrapper_attributes(
716 array(
717 'class' => 'ultp-block-' . $attr['blockId'] . ' ultp-filter-clear ultp-filter-clear-button ',
718 'data-blockid' => $attr['blockId'],
719 )
720 );
721
722 $selected_filter_wrapper_attr = get_block_wrapper_attributes(
723 array(
724 'class' => 'ultp-block-' . $attr['blockId'] . ' ultp-filter-clear ultp-filter-clear-template',
725 'style' => 'display: none;',
726 )
727 );
728
729 ob_start();
730 ?>
731
732 <div class="ultp-block-<?php echo $attr['blockId']; ?>-wrapper">
733 <div <?php echo $selected_filter_wrapper_attr; ?>>
734 <div class="ultp-selected-filter">
735 <span class="ultp-selected-filter-icon" role="button">
736 <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 31.1 31.25"><path stroke="rgba(0,0,0,0)" strokeMiterlimit="10" d="M27.1 30.153 15.549 18.601 4 30.153a2.05 2.05 0 0 1-2.9-2.9l11.551-11.55L1.1 4.153a2.05 2.05 0 0 1 2.9-2.9l11.549 11.552L27.1 1.253a2.05 2.05 0 0 1 2.9 2.9l-11.553 11.55L30 27.253a2.05 2.05 0 1 1-2.9 2.9Z"/></svg>
737 </span>
738 <span class="ultp-selected-filter-text">
739 </span>
740 </div>
741 </div>
742
743 <div <?php echo $wrapper_attrs; ?>>
744 <button class="ultp-clear-button">
745 <?php echo esc_html( $attr['clearButtonText'] ); ?>
746 </button>
747 </div>
748 </div>
749
750 <?php
751 $content = ob_get_clean();
752 return $content;
753 }
754 }
755