class-wc-widget-cart.php
5 years ago
class-wc-widget-layered-nav-filters.php
5 years ago
class-wc-widget-layered-nav.php
5 years ago
class-wc-widget-price-filter.php
5 years ago
class-wc-widget-product-categories.php
5 years ago
class-wc-widget-product-search.php
5 years ago
class-wc-widget-product-tag-cloud.php
5 years ago
class-wc-widget-products.php
5 years ago
class-wc-widget-rating-filter.php
5 years ago
class-wc-widget-recent-reviews.php
5 years ago
class-wc-widget-recently-viewed.php
5 years ago
class-wc-widget-top-rated-products.php
5 years ago
class-wc-widget-layered-nav-filters.php
114 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Layered Navigation Filters Widget. |
| 4 | * |
| 5 | * @package WooCommerce\Widgets |
| 6 | * @version 2.3.0 |
| 7 | */ |
| 8 | |
| 9 | defined( 'ABSPATH' ) || exit; |
| 10 | |
| 11 | /** |
| 12 | * Widget layered nav filters. |
| 13 | */ |
| 14 | class WC_Widget_Layered_Nav_Filters extends WC_Widget { |
| 15 | |
| 16 | /** |
| 17 | * Constructor. |
| 18 | */ |
| 19 | public function __construct() { |
| 20 | $this->widget_cssclass = 'woocommerce widget_layered_nav_filters'; |
| 21 | $this->widget_description = __( 'Display a list of active product filters.', 'woocommerce' ); |
| 22 | $this->widget_id = 'woocommerce_layered_nav_filters'; |
| 23 | $this->widget_name = __( 'Active Product Filters', 'woocommerce' ); |
| 24 | $this->settings = array( |
| 25 | 'title' => array( |
| 26 | 'type' => 'text', |
| 27 | 'std' => __( 'Active filters', 'woocommerce' ), |
| 28 | 'label' => __( 'Title', 'woocommerce' ), |
| 29 | ), |
| 30 | ); |
| 31 | |
| 32 | parent::__construct(); |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * Output widget. |
| 37 | * |
| 38 | * @see WP_Widget |
| 39 | * @param array $args Arguments. |
| 40 | * @param array $instance Widget instance. |
| 41 | */ |
| 42 | public function widget( $args, $instance ) { |
| 43 | if ( ! is_shop() && ! is_product_taxonomy() ) { |
| 44 | return; |
| 45 | } |
| 46 | |
| 47 | $_chosen_attributes = WC_Query::get_layered_nav_chosen_attributes(); |
| 48 | $min_price = isset( $_GET['min_price'] ) ? wc_clean( wp_unslash( $_GET['min_price'] ) ) : 0; // WPCS: input var ok, CSRF ok. |
| 49 | $max_price = isset( $_GET['max_price'] ) ? wc_clean( wp_unslash( $_GET['max_price'] ) ) : 0; // WPCS: input var ok, CSRF ok. |
| 50 | $rating_filter = isset( $_GET['rating_filter'] ) ? array_filter( array_map( 'absint', explode( ',', wp_unslash( $_GET['rating_filter'] ) ) ) ) : array(); // WPCS: sanitization ok, input var ok, CSRF ok. |
| 51 | $base_link = $this->get_current_page_url(); |
| 52 | |
| 53 | if ( 0 < count( $_chosen_attributes ) || 0 < $min_price || 0 < $max_price || ! empty( $rating_filter ) ) { |
| 54 | |
| 55 | $this->widget_start( $args, $instance ); |
| 56 | |
| 57 | echo '<ul>'; |
| 58 | |
| 59 | // Attributes. |
| 60 | if ( ! empty( $_chosen_attributes ) ) { |
| 61 | foreach ( $_chosen_attributes as $taxonomy => $data ) { |
| 62 | foreach ( $data['terms'] as $term_slug ) { |
| 63 | $term = get_term_by( 'slug', $term_slug, $taxonomy ); |
| 64 | if ( ! $term ) { |
| 65 | continue; |
| 66 | } |
| 67 | |
| 68 | $filter_name = 'filter_' . wc_attribute_taxonomy_slug( $taxonomy ); |
| 69 | $current_filter = isset( $_GET[ $filter_name ] ) ? explode( ',', wc_clean( wp_unslash( $_GET[ $filter_name ] ) ) ) : array(); // WPCS: input var ok, CSRF ok. |
| 70 | $current_filter = array_map( 'sanitize_title', $current_filter ); |
| 71 | $new_filter = array_diff( $current_filter, array( $term_slug ) ); |
| 72 | |
| 73 | $link = remove_query_arg( array( 'add-to-cart', $filter_name ), $base_link ); |
| 74 | |
| 75 | if ( count( $new_filter ) > 0 ) { |
| 76 | $link = add_query_arg( $filter_name, implode( ',', $new_filter ), $link ); |
| 77 | } |
| 78 | |
| 79 | $filter_classes = array( 'chosen', 'chosen-' . sanitize_html_class( str_replace( 'pa_', '', $taxonomy ) ), 'chosen-' . sanitize_html_class( str_replace( 'pa_', '', $taxonomy ) . '-' . $term_slug ) ); |
| 80 | |
| 81 | echo '<li class="' . esc_attr( implode( ' ', $filter_classes ) ) . '"><a rel="nofollow" aria-label="' . esc_attr__( 'Remove filter', 'woocommerce' ) . '" href="' . esc_url( $link ) . '">' . esc_html( $term->name ) . '</a></li>'; |
| 82 | } |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | if ( $min_price ) { |
| 87 | $link = remove_query_arg( 'min_price', $base_link ); |
| 88 | /* translators: %s: minimum price */ |
| 89 | echo '<li class="chosen"><a rel="nofollow" aria-label="' . esc_attr__( 'Remove filter', 'woocommerce' ) . '" href="' . esc_url( $link ) . '">' . sprintf( __( 'Min %s', 'woocommerce' ), wc_price( $min_price ) ) . '</a></li>'; // WPCS: XSS ok. |
| 90 | } |
| 91 | |
| 92 | if ( $max_price ) { |
| 93 | $link = remove_query_arg( 'max_price', $base_link ); |
| 94 | /* translators: %s: maximum price */ |
| 95 | echo '<li class="chosen"><a rel="nofollow" aria-label="' . esc_attr__( 'Remove filter', 'woocommerce' ) . '" href="' . esc_url( $link ) . '">' . sprintf( __( 'Max %s', 'woocommerce' ), wc_price( $max_price ) ) . '</a></li>'; // WPCS: XSS ok. |
| 96 | } |
| 97 | |
| 98 | if ( ! empty( $rating_filter ) ) { |
| 99 | foreach ( $rating_filter as $rating ) { |
| 100 | $link_ratings = implode( ',', array_diff( $rating_filter, array( $rating ) ) ); |
| 101 | $link = $link_ratings ? add_query_arg( 'rating_filter', $link_ratings ) : remove_query_arg( 'rating_filter', $base_link ); |
| 102 | |
| 103 | /* translators: %s: rating */ |
| 104 | echo '<li class="chosen"><a rel="nofollow" aria-label="' . esc_attr__( 'Remove filter', 'woocommerce' ) . '" href="' . esc_url( $link ) . '">' . sprintf( esc_html__( 'Rated %s out of 5', 'woocommerce' ), esc_html( $rating ) ) . '</a></li>'; |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | echo '</ul>'; |
| 109 | |
| 110 | $this->widget_end( $args ); |
| 111 | } |
| 112 | } |
| 113 | } |
| 114 |