PluginProbe
Post Grid Gutenberg Blocks – PostX / 4.1.3
Post Grid Gutenberg Blocks – PostX v4.1.3
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.3, at blocks/Advanced_Filter.php

640 lines 15.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace ULTP\blocks;
4
5 use WP_Taxonomy;
6
7 defined( 'ABSPATH' ) || exit;
8
9 /**
10 * Class Advanced_Filter
11 */
12 class Advanced_Filter {
13
14 private $pro_select_types = array(
15 'adv_sort',
16 'custom_tax'
17 );
18
19 public $order = array(
20 array(
21 'id' => 'DESC',
22 'name' => 'DESC',
23 ),
24 array(
25 'id' => 'ASC',
26 'name' => 'ASC',
27 ),
28 );
29
30 public $order_by = array(
31 array(
32 'id' => 'date',
33 'name' => 'Created Date',
34 ),
35 array(
36 'id' => 'modified',
37 'name' => 'Date Modified',
38 ),
39 array(
40 'id' => 'title',
41 'name' => 'Title',
42 ),
43 array(
44 'id' => 'menu_order',
45 'name' => 'Menu Order',
46 ),
47 array(
48 'id' => 'rand',
49 'name' => 'Random',
50 ),
51 // array(
52 // 'id' => 'post__in',
53 // 'name' => 'Post In',
54 // ),
55 array(
56 'id' => 'comment_count',
57 'name' => 'Number of Comments',
58 ),
59 );
60
61 public $adv_sort = array(
62 array(
63 'id' => 'popular_post_1_day_view',
64 'name' => 'Popular Posts (1 Day - Views)',
65 ),
66 array(
67 'id' => 'popular_post_7_days_view',
68 'name' => 'Popular Posts (7 Days - Views)',
69 ),
70 array(
71 'id' => 'popular_post_30_days_view',
72 'name' => 'Popular Posts (30 Days - Views)',
73 ),
74 array(
75 'id' => 'popular_post_all_times_view',
76 'name' => 'Popular Posts (All Time - Views)',
77 ),
78 array(
79 'id' => 'random_post',
80 'name' => 'Random Posts',
81 ),
82 array(
83 'id' => 'random_post_7_days',
84 'name' => 'Random Posts (7 Days)',
85 ),
86 array(
87 'id' => 'random_post_30_days',
88 'name' => 'Random Posts (30 Days)',
89 ),
90 array(
91 'id' => 'latest_post_published',
92 'name' => 'Latest Posts - Published Date',
93 ),
94 array(
95 'id' => 'latest_post_modified',
96 'name' => 'Latest Posts - Last Modified Date',
97 ),
98 array(
99 'id' => 'oldest_post_published',
100 'name' => 'Oldest Posts - Published Date',
101 ),
102 array(
103 'id' => 'oldest_post_modified',
104 'name' => 'Oldest Posts - Last Modified Date',
105 ),
106 array(
107 'id' => 'alphabet_asc',
108 'name' => 'Alphabetical ASC',
109 ),
110 array(
111 'id' => 'alphabet_desc',
112 'name' => 'Alphabetical DESC',
113 ),
114 array(
115 'id' => 'sticky_posts',
116 'name' => 'Sticky Post',
117 ),
118 array(
119 'id' => 'most_comment',
120 'name' => 'Most Comments',
121 ),
122 array(
123 'id' => 'most_comment_1_day',
124 'name' => 'Most Comments (1 Day)',
125 ),
126 array(
127 'id' => 'most_comment_7_days',
128 'name' => 'Most Comments (7 Days)',
129 ),
130 array(
131 'id' => 'most_comment_30_days',
132 'name' => 'Most Comments (30 Days)',
133 ),
134 );
135
136 /**
137 * Advanced_Filter constructor.
138 */
139 public function __construct() {
140 add_action( 'init', array( $this, 'register' ) );
141 }
142
143 public function get_select_attributes() {
144 return array(
145 'advanceId' => '',
146 'blockId' => '',
147 'advanceCss' => '',
148 'filterStyle' => 'dropdown',
149 'filterValues' => '["_all"]',
150 'allText' => 'All',
151 );
152 }
153
154 public function get_search_attributes() {
155 return array(
156 'advanceId' => '',
157 'blockId' => '',
158 'advanceCss' => '',
159 'placeholder' => 'Search...',
160 );
161 }
162
163 public function get_clear_attributes() {
164 return array(
165 'advanceId' => '',
166 'blockId' => '',
167 'clearButtonText' => 'Clear Filters',
168 'clearButtonPosition' => 'normal',
169 'cAlign' => 'margin-bottom: inherit',
170 );
171 }
172
173 public function register() {
174 register_block_type(
175 'ultimate-post/filter-select',
176 array(
177 'editor_script' => 'ultp-blocks-editor-script',
178 'editor_style' => 'ultp-blocks-editor-css',
179 'render_callback' => array( $this, 'select_content' ),
180 )
181 );
182
183 register_block_type(
184 'ultimate-post/filter-search-adv',
185 array(
186 'editor_script' => 'ultp-blocks-editor-script',
187 'editor_style' => 'ultp-blocks-editor-css',
188 'render_callback' => array( $this, 'search_content' ),
189 )
190 );
191
192 register_block_type(
193 'ultimate-post/filter-clear',
194 array(
195 'editor_script' => 'ultp-blocks-editor-script',
196 'editor_style' => 'ultp-blocks-editor-css',
197 'render_callback' => array( $this, 'clear_content' ),
198 )
199 );
200 }
201
202 public function get_button_data( $type, $ids, $post_types = '', $allText = 'All' ) {
203 $res = array();
204
205 if (in_array('_all', $ids)) {
206 $res['_all'] = $allText;
207 // $ids = array_filter($ids, function ($item) {
208 // return $item != '_all';
209 // });
210 }
211
212 $ids = implode(',', $ids);
213
214 switch ( $type ) {
215 case 'adv_sort':
216 $adv_sort_id = explode(',', $ids);
217 foreach($this->adv_sort as $adv) {
218 foreach($adv_sort_id as $id) {
219 if ($adv['id'] === $id) {
220 $res[$id] = $adv['name'];
221 }
222 }
223 }
224 break;
225 case 'category':
226 if ( !empty( $ids ) && $ids !== "_all" ) {
227 $categories = get_categories(
228 array(
229 'per_page' => -1,
230 'include' => $ids,
231 ),
232 );
233
234 foreach ( $categories as $category ) {
235 $res[ $category->slug ] = $category->name;
236 }
237 }
238 break;
239 case 'tags':
240 if ( !empty( $ids ) && $ids !== "_all" ) {
241 $tags = get_tags(
242 array(
243 'per_page' => -1,
244 'include' => $ids,
245 ),
246 );
247
248 foreach ( $tags as $tag ) {
249 $res[ $tag->slug ] = $tag->name;
250 }
251 }
252 break;
253 case 'author':
254 if ( !empty( $ids ) && $ids !== "_all" ) {
255 $authors = get_users(
256 array(
257 'per_page' => -1,
258 'role__in' => array( 'author' ),
259 'include' => $ids,
260 ),
261 );
262
263 foreach ( $authors as $author ) {
264 $res[ $author->ID ] = $author->display_name;
265 }
266 }
267
268 break;
269 case 'order':
270 foreach ( $this->order as $order ) {
271 $res[ $order['id'] ] = $order['name'];
272 }
273 break;
274 case 'order_by':
275 $orders = explode(',', $ids);
276 foreach ( $this->order_by as $order_by ) {
277 foreach($orders as $order) {
278 if ($order_by['id'] === $order) {
279 $res[ $order ] = $order_by['name'];
280 }
281 }
282 }
283 break;
284 case 'custom_tax':
285 if ( $post_types == '' ) {
286 break;
287 }
288
289 $post_types = json_decode( $post_types );
290
291 foreach ( $post_types as $post_type ) {
292 $taxonomies = get_object_taxonomies( $post_type );
293 foreach ( $taxonomies as $taxonomy ) {
294 $terms = get_terms(
295 array(
296 'taxonomy' => $taxonomy,
297 'hide_empty' => false,
298 )
299 );
300 foreach ( $terms as $term ) {
301 $res[ $term->slug ] = array(
302 'name' => $term->name,
303 'taxonomy' => $taxonomy,
304 );
305 }
306 }
307 }
308
309 default:
310 break;
311 }
312
313 return $res;
314 }
315
316 public function get_select_data( $type, $all_text, $post_types = '' ) {
317 switch ( $type ) {
318 case 'category':
319 $categories = get_categories(
320 array(
321 'per_page' => -1,
322 )
323 );
324 $categories = array_map(
325 function ( $category ) {
326 return array(
327 'id' => $category->slug,
328 'name' => $category->name,
329 );
330 },
331 $categories
332 );
333 $categories = array_merge(
334 array(
335 array(
336 'id' => '_all',
337 'name' => $all_text,
338 ),
339 ),
340 $categories
341 );
342 return $categories;
343 case 'tags':
344 $tags = get_tags(
345 array(
346 'per_page' => -1,
347
348 )
349 );
350 $tags = array_map(
351 function ( \WP_Term $tag ) {
352 return array(
353 'id' => $tag->slug,
354 'name' => $tag->name,
355 );
356 },
357 $tags
358 );
359 $tags = array_merge(
360 array(
361 array(
362 'id' => '_all',
363 'name' => $all_text,
364 ),
365 ),
366 $tags
367 );
368 return $tags;
369 case 'author':
370 $authors = get_users(
371 array(
372 'per_page' => -1,
373 // 'role__in' => array( 'author' ),
374 )
375 );
376 $authors = array_map(
377 function ( $author ) {
378 return array(
379 'id' => $author->ID,
380 'name' => $author->display_name,
381 );
382 },
383 $authors
384 );
385 $authors = array_merge(
386 array(
387 array(
388 'id' => '_all',
389 'name' => $all_text,
390 ),
391 ),
392 $authors
393 );
394 return $authors;
395 case 'order':
396 return $this->order;
397 case 'order_by':
398 return $this->order_by;
399 case 'adv_sort':
400 return array_merge(
401 array(
402 array(
403 'id' => '_all',
404 'name' => $all_text,
405 ),
406 ),
407 $this->adv_sort
408 );
409 case 'custom_tax':
410 $data = array(
411 array(
412 'id' => '_all',
413 'name' => __( 'All', 'ultimate-post' ),
414 ),
415 );
416
417 if ( $post_types == '' ) :
418 return $data;
419 endif;
420
421 $post_types = json_decode( $post_types );
422
423 foreach ( $post_types as $post_type ) {
424 $taxonomies = get_object_taxonomies( $post_type );
425 foreach ( $taxonomies as $taxonomy ) {
426 $terms = get_terms(
427 array(
428 'taxonomy' => $taxonomy,
429 'hide_empty' => false,
430 )
431 );
432 foreach ( $terms as $term ) {
433 $data[] = array(
434 'id' => $term->slug,
435 'name' => $term->name,
436 'taxonomy' => $taxonomy,
437 );
438 }
439 }
440 }
441
442 return $data;
443 default:
444 return array();
445 }
446 }
447
448 public function select_content( $attr ) {
449 $block_name = 'filter-select';
450 $is_active = ultimate_post()->is_lc_active();
451 $attr = wp_parse_args( $attr, $this->get_select_attributes() );
452
453 if (! $is_active && in_array( $attr['type'], $this->pro_select_types ) ) {
454 return '';
455 }
456
457 $post_types = isset( $attr['postTypes'] ) ? $attr['postTypes'] : '';
458
459 $attr['blockId'] = ultimate_post()->sanitize_attr($attr, "blockId", 'sanitize_html_class', 'missing_block_id');
460 $attr['allText'] = ultimate_post()->sanitize_attr($attr, "allText", 'sanitize_text_field', 'missing_block_id');
461
462
463 if ( 'inline' === $attr['filterStyle'] ) {
464 $inline_values = json_decode( $attr['filterValues'], true );
465
466 if ( ! is_array( $inline_values ) ) {
467 return '';
468 }
469
470 $data = $this->get_button_data( $attr['type'], $inline_values, $post_types, $attr['allText'] );
471
472 $btn_wrapper_attrs = get_block_wrapper_attributes(
473 array(
474 'class' => 'ultp-block-' . $attr['blockId'] . ' ultp-filter-button',
475 'role' => 'button',
476 'data-blockId' => $attr['blockId'],
477 'data-is-active' => 'false',
478 )
479 );
480
481 ob_start();
482 ?>
483
484 <div class="ultp-block-<?php echo $attr['blockId']; ?>-wrapper">
485 <?php foreach ( $data as $key => $value ) : ?>
486 <?php
487 if ( is_array( $value ) ) {
488 $name = $value['name'];
489 $tax = isset( $value['taxonomy'] ) ? 'data-tax="' . $value['taxonomy'] . '"' : '';
490 } else {
491 $name = $value;
492 $tax = '';
493 }
494 ?>
495 <div <?php echo $btn_wrapper_attrs; ?> <?php echo $tax; ?> data-selected="<?php echo $key; ?>" data-type="<?php echo $attr['type']; ?>">
496 <?php echo $name; ?>
497 </div>
498 <?php endforeach ?>
499 </div>
500 <?php
501
502 $content = ob_get_clean();
503 return $content;
504 } elseif ( 'dropdown' === $attr['filterStyle'] ) {
505 $data = $this->get_select_data( $attr['type'], $attr['allText'], $post_types );
506
507 $wrapper_attrs = get_block_wrapper_attributes(
508 array(
509 'class' => 'ultp-block-' . $attr['blockId'] . ' ultp-filter-select',
510 'data-selected' => array_key_exists( 0, $data ) ? $data[0]['id'] : 0,
511 'data-type' => $attr['type'],
512 'data-blockId' => $attr['blockId'],
513 'aria-expanded' => 'false',
514 'aria-label' => 'Select Filter (' . $attr['type'] . ')',
515 )
516 );
517
518 ob_start();
519 ?>
520 <div <?php echo $wrapper_attrs; ?>>
521 <div class="ultp-filter-select-field ultp-filter-select__parent">
522 <span class="ultp-filter-select-field-selected ultp-filter-select__parent-inner">
523 <?php echo esc_html( $data[0]['name'] ); ?>
524 </span>
525 <span class="ultp-filter-select-field-icon">
526 <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>
527 </span>
528 </div>
529 <ul style="display: none;" class="ultp-filter-select-options ultp-filter-select__dropdown">
530
531 <?php foreach ( $data as $item ) : ?>
532 <?php
533 $tax = isset( $item['taxonomy'] ) ? 'data-tax="' . $item['taxonomy'] . '"' : '';
534 ?>
535 <li class="ultp-filter-select__dropdown-inner" <?php echo $tax; ?> data-id="<?php echo $item['id']; ?>">
536 <?php echo esc_html( $item['name'] ); ?>
537 </li>
538 <?php endforeach; ?>
539
540 </ul>
541 </div>
542
543 <?php
544 $content = ob_get_clean();
545 return $content;
546 }
547
548 return 'Unknown filter style';
549 }
550
551 public function search_content( $attr ) {
552 $block_name = 'filter-search-adv';
553 $is_active = ultimate_post()->is_lc_active();
554
555 if ( ! $is_active ) {
556 return '';
557 }
558
559 $attr = wp_parse_args( $attr, $this->get_search_attributes() );
560
561 $attr['blockId'] = ultimate_post()->sanitize_attr($attr, "blockId", 'sanitize_html_class', 'missing_block_id');
562 $attr['placeholder'] = ultimate_post()->sanitize_attr($attr, "placeholder", 'sanitize_text_field');
563
564
565 $wrapper_attrs = get_block_wrapper_attributes(
566 array(
567 'class' => 'ultp-block-' . $attr['blockId'] . ' ultp-filter-search',
568 'aria-label' => 'Search Filter',
569 'role' => 'searchbox',
570 )
571 );
572
573 ob_start();
574 ?>
575 <div <?php echo $wrapper_attrs; ?>>
576 <div class="ultp-filter-search-input">
577 <input
578 type="search"
579 placeholder="<?php echo $attr['placeholder']; ?>"
580 />
581 <span class="ultp-filter-search-input-icon">
582 <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>
583 </span>
584 </div>
585 </div>
586 <?php
587 $content = ob_get_clean();
588 return $content;
589 }
590
591 public function clear_content( $attr ) {
592 $block_name = 'filter-clear';
593 // $is_active = ultimate_post()->is_lc_active();
594 $attr = wp_parse_args( $attr, $this->get_clear_attributes() );
595
596 $attr['blockId'] = ultimate_post()->sanitize_attr($attr, "blockId", 'sanitize_html_class', 'missing_block_id');
597 $attr['clearButtonText'] = ultimate_post()->sanitize_attr($attr, "clearButtonText", 'sanitize_text_field');
598
599 $wrapper_attrs = get_block_wrapper_attributes(
600 array(
601 'class' => 'ultp-block-' . $attr['blockId'] . ' ultp-filter-clear ultp-filter-clear-button ',
602 'data-blockid' => $attr['blockId']
603 )
604 );
605
606 $selected_filter_wrapper_attr = get_block_wrapper_attributes(
607 array(
608 'class' => 'ultp-block-' . $attr['blockId'] . ' ultp-filter-clear ultp-filter-clear-template',
609 'style' => 'display: none;',
610 )
611 );
612
613 ob_start();
614 ?>
615
616
617 <div class="ultp-block-<?php echo $attr['blockId']; ?>-wrapper">
618 <div <?php echo $selected_filter_wrapper_attr; ?>>
619 <div class="ultp-selected-filter">
620 <span class="ultp-selected-filter-icon" role="button">
621 <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>
622 </span>
623 <span class="ultp-selected-filter-text">
624 </span>
625 </div>
626 </div>
627
628 <div <?php echo $wrapper_attrs; ?>>
629 <button class="ultp-clear-button">
630 <?php echo esc_html( $attr['clearButtonText'] ); ?>
631 </button>
632 </div>
633 </div>
634
635 <?php
636 $content = ob_get_clean();
637 return $content;
638 }
639 }
640