woocommerce/includes/widgets/class-wc-widget-layered-nav...

112 lines
4.3 KiB
PHP
Raw Normal View History

<?php
/**
2015-12-17 07:23:58 +00:00
* Layered Navigation Filters Widget.
*
* @package WooCommerce/Widgets
* @version 2.3.0
*/
defined( 'ABSPATH' ) || exit;
/**
* Widget layered nav filters.
*/
class WC_Widget_Layered_Nav_Filters extends WC_Widget {
/**
2015-11-03 13:31:20 +00:00
* Constructor.
*/
public function __construct() {
$this->widget_cssclass = 'woocommerce widget_layered_nav_filters';
$this->widget_description = __( 'Display a list of active product filters.', 'woocommerce' );
$this->widget_id = 'woocommerce_layered_nav_filters';
$this->widget_name = __( 'Active Product Filters', 'woocommerce' );
$this->settings = array(
'title' => array(
'type' => 'text',
'std' => __( 'Active filters', 'woocommerce' ),
'label' => __( 'Title', 'woocommerce' ),
),
);
parent::__construct();
}
/**
* Output widget.
*
* @see WP_Widget
* @param array $args Arguments.
* @param array $instance Widget instance.
*/
public function widget( $args, $instance ) {
2017-11-09 11:16:47 +00:00
if ( ! is_shop() && ! is_product_taxonomy() ) {
return;
}
2012-11-27 16:22:47 +00:00
2016-02-10 10:19:32 +00:00
$_chosen_attributes = WC_Query::get_layered_nav_chosen_attributes();
$min_price = isset( $_GET['min_price'] ) ? wc_clean( wp_unslash( $_GET['min_price'] ) ) : 0; // WPCS: input var ok, CSRF ok.
$max_price = isset( $_GET['max_price'] ) ? wc_clean( wp_unslash( $_GET['max_price'] ) ) : 0; // WPCS: input var ok, CSRF ok.
$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.
2017-12-14 14:37:42 +00:00
$base_link = $this->get_current_page_url();
2015-11-13 09:47:27 +00:00
2016-12-09 15:43:25 +00:00
if ( 0 < count( $_chosen_attributes ) || 0 < $min_price || 0 < $max_price || ! empty( $rating_filter ) ) {
2012-11-27 16:22:47 +00:00
$this->widget_start( $args, $instance );
echo '<ul>';
2012-11-27 16:22:47 +00:00
// Attributes.
2016-06-06 18:39:23 +00:00
if ( ! empty( $_chosen_attributes ) ) {
2013-08-04 03:59:26 +00:00
foreach ( $_chosen_attributes as $taxonomy => $data ) {
foreach ( $data['terms'] as $term_slug ) {
$term = get_term_by( 'slug', $term_slug, $taxonomy );
if ( ! $term ) {
continue;
}
$filter_name = 'filter_' . sanitize_title( str_replace( 'pa_', '', $taxonomy ) );
$current_filter = isset( $_GET[ $filter_name ] ) ? explode( ',', wc_clean( wp_unslash( $_GET[ $filter_name ] ) ) ) : array(); // WPCS: input var ok, CSRF ok.
$current_filter = array_map( 'sanitize_title', $current_filter );
$new_filter = array_diff( $current_filter, array( $term_slug ) );
2012-11-27 16:22:47 +00:00
2016-11-21 12:14:57 +00:00
$link = remove_query_arg( array( 'add-to-cart', $filter_name ), $base_link );
2012-11-27 16:22:47 +00:00
if ( count( $new_filter ) > 0 ) {
$link = add_query_arg( $filter_name, implode( ',', $new_filter ), $link );
}
2012-11-27 16:22:47 +00:00
echo '<li class="chosen"><a rel="nofollow" aria-label="' . esc_attr__( 'Remove filter', 'woocommerce' ) . '" href="' . esc_url( $link ) . '">' . esc_html( $term->name ) . '</a></li>';
2013-08-04 03:59:26 +00:00
}
}
}
2012-11-27 16:22:47 +00:00
2013-01-12 13:03:19 +00:00
if ( $min_price ) {
2016-11-21 12:14:57 +00:00
$link = remove_query_arg( 'min_price', $base_link );
/* translators: %s: minimum price */
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.
}
2012-11-27 16:22:47 +00:00
2013-01-12 13:03:19 +00:00
if ( $max_price ) {
2016-11-21 12:14:57 +00:00
$link = remove_query_arg( 'max_price', $base_link );
/* translators: %s: maximum price */
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.
}
2012-11-27 16:22:47 +00:00
2016-12-09 15:43:25 +00:00
if ( ! empty( $rating_filter ) ) {
foreach ( $rating_filter as $rating ) {
$link_ratings = implode( ',', array_diff( $rating_filter, array( $rating ) ) );
$link = $link_ratings ? add_query_arg( 'rating_filter', $link_ratings ) : remove_query_arg( 'rating_filter', $base_link );
/* translators: %s: rating */
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>';
2016-12-09 15:43:25 +00:00
}
2015-11-13 09:47:27 +00:00
}
echo '</ul>';
$this->widget_end( $args );
}
}
}