| @@ -88,12 +88,13 @@ | ||
| 88 | 88 | // Product Filter Widget Hooks. |
| 89 | 89 | add_action( 'rtsb/before/archive/default/filter/items', [ __CLASS__,'default_filter_header' ], 10 ); |
| 90 | 90 | add_action( 'rtsb/before/archive/default/filter/items', [ __CLASS__,'default_filter_search' ], 15 ); |
| 91 | 91 | |
| 92 | - // WC Query modifier. | |
| 93 | - if ( ! rtsb()->has_pro() ) { | |
| 94 | - add_action( 'woocommerce_product_query', [ __CLASS__, 'query_modifier' ], 15 ); | |
| 95 | - } | |
| 92 | + // WC Query modifier. Always registered so the free `rtsb-product-filters` | |
| 93 | + // widget's URL params (product_cat, product_tag, product_brand, rating_filter, | |
| 94 | + // sale_filter) are applied to the main WooCommerce product query regardless | |
| 95 | + // of whether the Pro add-on is active. | |
| 96 | + add_action( 'woocommerce_product_query', [ __CLASS__, 'query_modifier' ], 15 ); | |
| 96 | 97 | |
| 97 | 98 | add_action( 'switch_theme', [ __CLASS__, 'clear_cache' ] ); |
| 98 | 99 | } |
| 99 | 100 | /** |
| @@ -465,9 +466,12 @@ | ||
| 465 | 466 | * @param object $query Query. |
| 466 | 467 | * @return object |
| 467 | 468 | */ |
| 468 | 469 | public static function query_modifier( $query ) { |
| 469 | - $onsale_filter = isset( $_GET['sale_filter'] ) ? sanitize_text_field( wp_unslash( $_GET['sale_filter'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended | |
| 470 | + // phpcs:disable WordPress.Security.NonceVerification.Recommended, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized | |
| 471 | + | |
| 472 | + // Sale filter. | |
| 473 | + $onsale_filter = isset( $_GET['sale_filter'] ) ? sanitize_text_field( wp_unslash( $_GET['sale_filter'] ) ) : ''; | |
| 470 | 474 | $on_sale_products = wc_get_product_ids_on_sale(); |
| 471 | 475 | |
| 472 | 476 | if ( 'onsale' === $onsale_filter ) { |
| 473 | 477 | $query->set( 'post__in', ! empty( $on_sale_products ) ? $on_sale_products : [ 0 ] ); |
| @@ -473,8 +477,63 @@ | ||
| 473 | 477 | $query->set( 'post__in', ! empty( $on_sale_products ) ? $on_sale_products : [ 0 ] ); |
| 474 | 478 | } elseif ( 'regular' === $onsale_filter ) { |
| 475 | 479 | $query->set( 'post__not_in', $on_sale_products ); |
| 476 | 480 | } |
| 481 | + | |
| 482 | + // Taxonomy filters (product_cat, product_tag, product_brand) from URL. | |
| 483 | + $tax_query = (array) $query->get( 'tax_query' ); | |
| 484 | + | |
| 485 | + $taxonomy_map = [ | |
| 486 | + 'product_cat' => 'product_cat', | |
| 487 | + 'product_tag' => 'product_tag', | |
| 488 | + 'product_brand' => 'product_brand', | |
| 489 | + ]; | |
| 490 | + | |
| 491 | + foreach ( $taxonomy_map as $param => $taxonomy ) { | |
| 492 | + if ( empty( $_GET[ $param ] ) || ! taxonomy_exists( $taxonomy ) ) { | |
| 493 | + continue; | |
| 494 | + } | |
| 495 | + | |
| 496 | + $raw_values = wc_clean( wp_unslash( $_GET[ $param ] ) ); | |
| 497 | + $slugs = array_filter( array_map( 'urldecode', explode( ',', $raw_values ) ) ); | |
| 498 | + | |
| 499 | + if ( empty( $slugs ) ) { | |
| 500 | + continue; | |
| 501 | + } | |
| 502 | + | |
| 503 | + $tax_query[] = [ | |
| 504 | + 'taxonomy' => $taxonomy, | |
| 505 | + 'field' => 'slug', | |
| 506 | + 'terms' => $slugs, | |
| 507 | + 'operator' => 'IN', | |
| 508 | + ]; | |
| 509 | + } | |
| 510 | + | |
| 511 | + // Rating filter (rating_filter=4,5). | |
| 512 | + if ( ! empty( $_GET['rating_filter'] ) ) { | |
| 513 | + $raw_ratings = wc_clean( wp_unslash( $_GET['rating_filter'] ) ); | |
| 514 | + $ratings = array_filter( array_map( 'intval', explode( ',', $raw_ratings ) ) ); | |
| 515 | + | |
| 516 | + if ( ! empty( $ratings ) ) { | |
| 517 | + $rating_terms = []; | |
| 518 | + foreach ( $ratings as $rating ) { | |
| 519 | + $rating_terms[] = 'rated-' . $rating; | |
| 520 | + } | |
| 521 | + | |
| 522 | + $tax_query[] = [ | |
| 523 | + 'taxonomy' => 'product_visibility', | |
| 524 | + 'field' => 'name', | |
| 525 | + 'terms' => $rating_terms, | |
| 526 | + 'operator' => 'IN', | |
| 527 | + ]; | |
| 528 | + } | |
| 529 | + } | |
| 530 | + | |
| 531 | + if ( ! empty( $tax_query ) ) { | |
| 532 | + $query->set( 'tax_query', $tax_query ); | |
| 533 | + } | |
| 534 | + | |
| 535 | + // phpcs:enable WordPress.Security.NonceVerification.Recommended, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized | |
| 477 | 536 | |
| 478 | 537 | return $query; |
| 479 | 538 | } |
| 480 | 539 | |