PluginProbe
Post Grid Gutenberg Blocks – PostX / 5.1.1
Post Grid Gutenberg Blocks – PostX v5.1.1
5.1.1 5.1.0 5.0.41 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 All 240 releases
← All changes | blocks/Advanced_Filter.php +67 -256 5.0.36 → 5.1.1 View file →
@@ -9,15 +9,20 @@
9 9 * Class Advanced_Filter
10 10 */
11 11 class Advanced_Filter {
12 12 /**
13 - * Types of pro select filters supported by the advanced filter.
13 + * Select filter types rendered by PostX itself.
14 14 *
15 + * Further types are added through the `ultp_advanced_filter_select_types` filter.
16 + *
15 17 * @var array
16 18 */
17 - private $pro_select_types = array(
18 - 'adv_sort',
19 - 'custom_tax',
19 + private $select_types = array(
20 + 'category',
21 + 'tags',
22 + 'author',
23 + 'order',
24 + 'order_by',
20 25 );
21 26
22 27 /**
23 28 * Order Value for select
@@ -109,90 +114,8 @@
109 114 add_action( 'init', array( $this, 'register' ) ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
110 115 }
111 116
112 117 /**
113 - * Get_adv_filter_options
114 - *
115 - * @return array
116 - */
117 - private function get_adv_filter_options() {
118 - return array(
119 - array(
120 - 'id' => 'popular_post_1_day_view',
121 - 'name' => __( 'Trending Today', 'ultimate-post' ),
122 - ),
123 - array(
124 - 'id' => 'popular_post_7_days_view',
125 - 'name' => __( 'This Week’s Popular Posts', 'ultimate-post' ),
126 - ),
127 - array(
128 - 'id' => 'popular_post_30_days_view',
129 - 'name' => __( 'Top Posts of the Month', 'ultimate-post' ),
130 - ),
131 - array(
132 - 'id' => 'popular_post_all_times_view',
133 - 'name' => __( 'All-Time Favorites', 'ultimate-post' ),
134 - ),
135 - array(
136 - 'id' => 'random_post',
137 - 'name' => __( 'Random Posts', 'ultimate-post' ),
138 - ),
139 - array(
140 - 'id' => 'random_post_7_days',
141 - 'name' => __( 'Random Posts (7 Days)', 'ultimate-post' ),
142 - ),
143 - array(
144 - 'id' => 'random_post_30_days',
145 - 'name' => __( 'Random Posts (30 Days)', 'ultimate-post' ),
146 - ),
147 - array(
148 - 'id' => 'latest_post_published',
149 - 'name' => __( 'Latest Posts - Published Date', 'ultimate-post' ),
150 - ),
151 - array(
152 - 'id' => 'latest_post_modified',
153 - 'name' => __( 'Latest Posts - Last Modified Date', 'ultimate-post' ),
154 - ),
155 - array(
156 - 'id' => 'oldest_post_published',
157 - 'name' => __( 'Oldest Posts - Published Date', 'ultimate-post' ),
158 - ),
159 - array(
160 - 'id' => 'oldest_post_modified',
161 - 'name' => __( 'Oldest Posts - Last Modified Date', 'ultimate-post' ),
162 - ),
163 - array(
164 - 'id' => 'alphabet_asc',
165 - 'name' => __( 'Alphabetical ASC', 'ultimate-post' ),
166 - ),
167 - array(
168 - 'id' => 'alphabet_desc',
169 - 'name' => __( 'Alphabetical DESC', 'ultimate-post' ),
170 - ),
171 - array(
172 - 'id' => 'sticky_posts',
173 - 'name' => __( 'Sticky Post', 'ultimate-post' ),
174 - ),
175 - array(
176 - 'id' => 'most_comment',
177 - 'name' => __( 'Most Comments', 'ultimate-post' ),
178 - ),
179 - array(
180 - 'id' => 'most_comment_1_day',
181 - 'name' => __( 'Most Comments (1 Day)', 'ultimate-post' ),
182 - ),
183 - array(
184 - 'id' => 'most_comment_7_days',
185 - 'name' => __( 'Most Comments (7 Days)', 'ultimate-post' ),
186 - ),
187 - array(
188 - 'id' => 'most_comment_30_days',
189 - 'name' => __( 'Most Comments (30 Days)', 'ultimate-post' ),
190 - ),
191 - );
192 - }
193 -
194 - /**
195 118 * Get_select_attributes
196 119 *
197 120 * @return array
198 121 */
@@ -212,22 +135,8 @@
212 135 );
213 136 }
214 137
215 138 /**
216 - * Get_search_attributes
217 - *
218 - * @return array
219 - */
220 - public function get_search_attributes() {
221 - return array(
222 - 'advanceId' => '',
223 - 'blockId' => '',
224 - 'advanceCss' => '',
225 - 'placeholder' => 'Search...',
226 - );
227 - }
228 -
229 - /**
230 139 * Get_clear_attributes
231 140 *
232 141 * @return array
233 142 */
@@ -293,21 +202,9 @@
293 202 }
294 203
295 204 $ids = implode( ',', $data_ids );
296 205
297 - $adv_sort = $this->get_adv_filter_options();
298 -
299 206 switch ( $type ) {
300 - case 'adv_sort':
301 - $adv_sort_id = explode( ',', $ids );
302 - foreach ( $adv_sort as $adv ) {
303 - foreach ( $adv_sort_id as $id ) {
304 - if ( $adv['id'] === $id ) {
305 - $res[ $id ] = $adv['name'];
306 - }
307 - }
308 - }
309 - break;
310 207 case 'category':
311 208 $args = array(
312 209 'taxonomy' => 'category',
313 210 'hide_empty' => false,
@@ -374,51 +271,23 @@
374 271 }
375 272 }
376 273 }
377 274 break;
378 - case 'custom_tax':
379 - if ( '' === $post_types ) {
380 - break;
381 - }
382 -
383 - foreach ( $post_types as $post_type ) {
384 - $taxonomies = get_object_taxonomies( $post_type );
385 -
386 - foreach ( $taxonomies as $taxonomy ) {
387 - if ( 'category' !== $taxonomy && 'post_tag' !== $taxonomy ) {
388 - $terms = get_terms(
389 - array(
390 - 'taxonomy' => $taxonomy,
391 - 'hide_empty' => false,
392 - )
393 - );
394 - foreach ( $terms as $term ) {
395 - $should_add = true;
396 - if ( 'specific' === $option ) {
397 - $should_add = false;
398 - if ( is_array( $data_ids ) ) {
399 - $should_add = in_array( $term->slug, $data_ids, true );
400 - } elseif ( is_string( $data_ids ) ) {
401 - $data_ids_arr = array_filter( array_map( 'trim', explode( ',', $data_ids ) ), 'strlen' );
402 - $should_add = in_array( $term->slug, $data_ids_arr, true );
403 - }
404 - }
405 - if ( $should_add ) {
406 - $res[ $term->slug ] = array(
407 - 'name' => $term->name,
408 - 'taxonomy' => $taxonomy,
409 - );
410 - }
411 - }
412 - }
413 - }
414 - }
415 -
416 275 default:
276 + /**
277 + * Inline (button) options for a select filter type PostX does not render itself.
278 + *
279 + * @param array $res Options keyed by value. Holds '_all' when that button is enabled.
280 + * @param string $type Filter type.
281 + * @param array $data_ids Values chosen in the block settings.
282 + * @param array|string $post_types Post types the filter applies to, or ''.
283 + * @param string $option 'all' or 'specific'.
284 + */
285 + $res = apply_filters( 'ultp_advanced_filter_button_data', $res, $type, $data_ids, $post_types, $option );
417 286 break;
418 287 }
419 288
420 - return $res;
289 + return is_array( $res ) ? $res : array();
421 290 }
422 291
423 292 /**
424 293 * Inserts all option depending on condition
@@ -455,10 +324,8 @@
455 324 }
456 325 );
457 326 }
458 327
459 - $adv_sort = $this->get_adv_filter_options();
460 -
461 328 switch ( $type ) {
462 329
463 330 case 'category':
464 331 $categories = array();
@@ -552,74 +419,45 @@
552 419 return 'specific' === $mode ?
553 420 $this->filter_select_options( $this->order_by, $filtered_specific )
554 421 : $this->order_by;
555 422
556 - case 'adv_sort':
557 - $data = 'specific' === $mode ?
558 - $this->filter_select_options( $adv_sort, $filtered_specific )
559 - : $adv_sort;
423 + default:
424 + /**
425 + * Dropdown options for a select filter type PostX does not render itself.
426 + *
427 + * Return a list of array( 'id' => ..., 'name' => ... ) items, without the
428 + * '_all' item: PostX inserts that. Return null to render no options at all.
429 + *
430 + * @param array|null $data Options. Null when no callback handles the type.
431 + * @param string $type Filter type.
432 + * @param array|string $post_types Post types the filter applies to, or ''.
433 + * @param array $specific Values chosen in the block settings, without '_all'.
434 + * @param string $mode 'all' or 'specific'.
435 + */
436 + $data = apply_filters( 'ultp_advanced_filter_select_data', null, $type, $post_types, $filtered_specific, $mode );
560 437
561 - $this->maybe_insert_all_option( $data, $all, $all_idx, $mode );
562 -
563 - return $data;
564 -
565 - case 'custom_tax':
566 - if ( '' === $post_types ) {
438 + if ( ! is_array( $data ) ) {
567 439 return array();
568 440 }
569 441
570 - $data = array();
571 -
572 - foreach ( $post_types as $post_type ) {
573 - $taxonomies = get_object_taxonomies( sanitize_text_field( $post_type ) );
574 - foreach ( $taxonomies as $taxonomy ) {
575 - if ( 'category' !== $taxonomy && 'post_tag' !== $taxonomy ) {
576 - $terms = array();
577 -
578 - if ( 'specific' === $mode && count( $filtered_specific ) > 0 ) {
579 - foreach ( $filtered_specific as $s ) {
580 - $res = get_term_by( 'slug', $s, $taxonomy );
581 - if ( ! empty( $res ) ) {
582 - $terms[] = $res;
583 - }
584 - }
585 - } else {
586 - $terms = get_terms(
587 - array(
588 - 'taxonomy' => $taxonomy,
589 - 'hide_empty' => false,
590 - )
591 - );
592 - }
593 -
594 - foreach ( $terms as $term ) {
595 - $existing_ids = array_column( $data, 'id' );
596 - if ( ! in_array( $term->slug, $existing_ids, true ) ) {
597 - $data[] = array(
598 - 'id' => $term->slug,
599 - 'name' => $term->name,
600 - 'taxonomy' => $taxonomy,
601 - );
602 - }
603 - }
604 - }
605 - }
606 - }
607 -
608 442 $this->maybe_insert_all_option( $data, $all, $all_idx, $mode );
609 443
610 444 return $data;
611 - default:
612 - return array();
613 445 }
614 446 }
615 447
616 448 public function select_content( $attr ) {
617 449 $block_name = 'filter-select';
618 - $is_active = ultimate_post()->is_lc_active();
619 450 $attr = wp_parse_args( $attr, $this->get_select_attributes() );
620 451
621 - if ( ! $is_active && in_array( $attr['type'], $this->pro_select_types ) ) {
452 + /**
453 + * Select filter types that render on the front end.
454 + *
455 + * @param array $types Types PostX renders itself.
456 + */
457 + $types = apply_filters( 'ultp_advanced_filter_select_types', $this->select_types );
458 +
459 + if ( ! empty( $attr['type'] ) && ! in_array( $attr['type'], (array) $types, true ) ) {
622 460 return '';
623 461 }
624 462
625 463 $post_types = isset( $attr['postTypes'] ) ? $attr['postTypes'] : '';
@@ -634,10 +472,11 @@
634 472 $post_types = json_decode( $post_types, true );
635 473 }
636 474 }
637 475
638 - $attr['blockId'] = ultimate_post()->sanitize_attr( $attr, 'blockId', 'sanitize_html_class', 'missing_block_id' );
639 - $attr['allText'] = ultimate_post()->sanitize_attr( $attr, 'allText', 'sanitize_text_field', 'missing_block_id' );
476 + $attr['blockId'] = ultimate_post()->sanitize_attr( $attr, 'blockId', 'sanitize_html_class', 'missing_block_id' );
477 + $attr['allText'] = ultimate_post()->sanitize_attr( $attr, 'allText', 'sanitize_text_field', 'missing_block_id' );
478 + $current_page_post_id = ( isset( $attr['currentPostId'] ) && $attr['currentPostId'] ) ? sanitize_html_class( $attr['currentPostId'] ) : ultimate_post()->get_page_post_id( $attr['blockId'] );
640 479
641 480 if ( 'inline' === $attr['filterStyle'] ) {
642 481 $inline_values = json_decode( $attr['filterValues'], true );
643 482
@@ -653,9 +492,9 @@
653 492 'role' => 'button',
654 493 'data-blockId' => $attr['blockId'],
655 494 'data-is-active' => 'false',
656 495 'data-builder' => ultimate_post()->get_builder_attr( 'archiveBuilderFilter' ),
657 - 'data-current-postid' => get_the_ID(),
496 + 'data-current-postid' => $current_page_post_id,
658 497 'data-filter-label' => isset( $attr['allText'] ) ? $attr['allText'] : 'All',
659 498 )
660 499 );
661 500
@@ -669,15 +508,15 @@
669 508 ?>
670 509 <?php
671 510 if ( is_array( $value ) ) {
672 511 $name = $value['name'];
673 - $tax = isset( $value['taxonomy'] ) ? 'data-tax="' . esc_attr( $value['taxonomy'] ) . '"' : '';
512 + $tax = isset( $value['taxonomy'] ) ? $value['taxonomy'] : null;
674 513 } else {
675 514 $name = $value;
676 - $tax = '';
515 + $tax = null;
677 516 }
678 517 ?>
679 - <div <?php echo $btn_wrapper_attrs; ?> <?php echo $tax; ?> data-selected="<?php echo esc_attr( $key ); ?>" data-type="<?php echo esc_attr( $attr['type'] ); ?>">
518 + <div <?php echo $btn_wrapper_attrs; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- get_block_wrapper_attributes() returns pre-escaped attribute HTML; escaping it again would corrupt it. ?> <?php echo null !== $tax ? 'data-tax="' . esc_attr( $tax ) . '"' : ''; ?> data-selected="<?php echo esc_attr( $key ); ?>" data-type="<?php echo esc_attr( $attr['type'] ); ?>">
680 519 <?php echo esc_html( $name ); ?>
681 520 </div>
682 521 <?php endforeach ?>
683 522 </div>
@@ -715,9 +554,9 @@
715 554 'data-blockId' => $attr['blockId'],
716 555 'aria-expanded' => 'false',
717 556 'aria-label' => 'Select Filter (' . $attr['type'] . ')',
718 557 'data-builder' => ultimate_post()->get_builder_attr( 'archiveBuilderFilter' ),
719 - 'data-current-postid' => get_the_ID(),
558 + 'data-current-postid' => $current_page_post_id,
720 559 'data-filter-label' => isset( $def_value['name'] ) ? $def_value['name'] : 'All',
721 560 )
722 561 );
723 562
@@ -722,9 +561,9 @@
722 561 );
723 562
724 563 ob_start();
725 564 ?>
726 - <div <?php echo $wrapper_attrs; ?>>
565 + <div <?php echo $wrapper_attrs; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- get_block_wrapper_attributes() returns pre-escaped attribute HTML; escaping it again would corrupt it. ?>>
727 566 <div class="ultp-filter-select-field ultp-filter-select__parent">
728 567 <span class="ultp-filter-select-field-selected ultp-filter-select__parent-inner">
729 568 <?php echo esc_html( isset( $def_value['name'] ) ? $def_value['name'] : '' ); ?>
730 569 </span>
@@ -744,12 +583,9 @@
744 583 />
745 584 <?php endif; ?>
746 585
747 586 <?php foreach ( $data as $item ) : ?>
748 - <?php
749 - $tax = isset( $item['taxonomy'] ) ? 'data-tax="' . esc_attr( $item['taxonomy'] ) . '"' : '';
750 - ?>
751 - <li class="ultp-filter-select__dropdown-inner" <?php echo $tax; ?> data-id="<?php echo esc_attr( $item['id'] ); ?>">
587 + <li class="ultp-filter-select__dropdown-inner" <?php echo isset( $item['taxonomy'] ) ? 'data-tax="' . esc_attr( $item['taxonomy'] ) . '"' : ''; ?> data-id="<?php echo esc_attr( $item['id'] ); ?>">
752 588 <?php echo esc_html( $item['name'] ); ?>
753 589 </li>
754 590 <?php endforeach; ?>
755 591
@@ -766,48 +602,24 @@
766 602
767 603 /**
768 604 * Search_content
769 605 *
606 + * The search filter is rendered by PostX Pro through the
607 + * `ultp_advanced_filter_search_content` filter; PostX itself outputs nothing.
608 + *
770 609 * @param mixed $attr array.
771 610 * @return string
772 611 */
773 612 public function search_content( $attr ) {
774 - $block_name = 'filter-search-adv';
775 - $is_active = ultimate_post()->is_lc_active();
613 + /**
614 + * Front-end markup for the Advanced Filter search block.
615 + *
616 + * @param string $content Markup. Empty by default.
617 + * @param array $attr Block attributes, unsanitized.
618 + */
619 + $content = apply_filters( 'ultp_advanced_filter_search_content', '', (array) $attr );
776 620
777 - if ( ! $is_active ) {
778 - return '';
779 - }
780 -
781 - $attr = wp_parse_args( $attr, $this->get_search_attributes() );
782 -
783 - $attr['blockId'] = ultimate_post()->sanitize_attr( $attr, 'blockId', 'sanitize_html_class', 'missing_block_id' );
784 - $attr['placeholder'] = ultimate_post()->sanitize_attr( $attr, 'placeholder', 'sanitize_text_field' );
785 -
786 - $wrapper_attrs = get_block_wrapper_attributes(
787 - array(
788 - 'class' => 'ultp-block-' . $attr['blockId'] . ' ultp-filter-search',
789 - 'aria-label' => 'Search Filter',
790 - 'role' => 'searchbox',
791 - )
792 - );
793 -
794 - ob_start();
795 - ?>
796 - <div <?php echo $wrapper_attrs; ?>>
797 - <div class="ultp-filter-search-input">
798 - <input
799 - type="search"
800 - placeholder="<?php echo esc_attr( $attr['placeholder'] ); ?>"
801 - />
802 - <span class="ultp-filter-search-input-icon">
803 - <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 47.05 47.05" fill="currentColor"><path stroke="currentColor" 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>
804 - </span>
805 - </div>
806 - </div>
807 - <?php
808 - $content = ob_get_clean();
809 - return $content;
621 + return is_string( $content ) ? $content : '';
810 622 }
811 623
812 624 /**
813 625 * Clear_content
@@ -816,9 +628,8 @@
816 628 * @return array
817 629 */
818 630 public function clear_content( $attr ) {
819 631 $block_name = 'filter-clear';
820 - // $is_active = ultimate_post()->is_lc_active();
821 632 $attr = wp_parse_args( $attr, $this->get_clear_attributes() );
822 633
823 634 $attr['blockId'] = ultimate_post()->sanitize_attr( $attr, 'blockId', 'sanitize_html_class', 'missing_block_id' );
824 635 $attr['clearButtonText'] = ultimate_post()->sanitize_attr( $attr, 'clearButtonText', 'sanitize_text_field' );
@@ -839,10 +650,10 @@
839 650
840 651 ob_start();
841 652 ?>
842 653
843 - <div class="ultp-block-<?php echo $attr['blockId']; ?>-wrapper">
844 - <div <?php echo $selected_filter_wrapper_attr; ?>>
654 + <div class="ultp-block-<?php echo sanitize_html_class( $attr['blockId'] ); ?>-wrapper">
655 + <div <?php echo $selected_filter_wrapper_attr; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- get_block_wrapper_attributes() returns pre-escaped attribute HTML; escaping it again would corrupt it. ?>>
845 656 <div class="ultp-selected-filter">
846 657 <span class="ultp-selected-filter-icon" role="button">
847 658 <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>
848 659 </span>
@@ -850,9 +661,9 @@
850 661 </span>
851 662 </div>
852 663 </div>
853 664
854 - <div <?php echo $wrapper_attrs; ?>>
665 + <div <?php echo $wrapper_attrs; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- get_block_wrapper_attributes() returns pre-escaped attribute HTML; escaping it again would corrupt it. ?>>
855 666 <button class="ultp-clear-button">
856 667 <?php echo esc_html( $attr['clearButtonText'] ); ?>
857 668 </button>
858 669 </div>