woocommerce/includes/widgets/class-wc-widget-rating-filt...

140 lines
4.2 KiB
PHP
Raw Normal View History

2015-11-13 09:38:27 +00:00
<?php
/**
* Rating Filter Widget and related functions.
*
* @package WooCommerce/Widgets
* @version 2.6.0
*/
defined( 'ABSPATH' ) || exit;
/**
* Widget rating filter class.
2015-11-13 09:38:27 +00:00
*/
class WC_Widget_Rating_Filter extends WC_Widget {
/**
* Constructor.
*/
public function __construct() {
$this->widget_cssclass = 'woocommerce widget_rating_filter';
$this->widget_description = __( 'Display a list of star ratings to filter products in your store.', 'woocommerce' );
2015-11-13 09:38:27 +00:00
$this->widget_id = 'woocommerce_rating_filter';
$this->widget_name = __( 'Filter Products by Rating', 'woocommerce' );
2015-11-13 09:38:27 +00:00
$this->settings = array(
'title' => array(
2015-11-13 09:38:27 +00:00
'type' => 'text',
'std' => __( 'Average rating', 'woocommerce' ),
'label' => __( 'Title', 'woocommerce' ),
),
2015-11-13 09:38:27 +00:00
);
parent::__construct();
}
2016-02-10 10:02:50 +00:00
/**
* Count products after other filters have occurred by adjusting the main query.
*
* @param int $rating Rating.
2016-02-10 10:02:50 +00:00
* @return int
*/
protected function get_filtered_product_count( $rating ) {
global $wpdb;
$tax_query = WC_Query::get_main_tax_query();
$meta_query = WC_Query::get_main_meta_query();
2016-12-09 15:43:25 +00:00
// Unset current rating filter.
foreach ( $tax_query as $key => $query ) {
2016-02-10 10:02:50 +00:00
if ( ! empty( $query['rating_filter'] ) ) {
2016-12-09 15:43:25 +00:00
unset( $tax_query[ $key ] );
break;
2016-02-10 10:02:50 +00:00
}
}
2016-12-09 15:43:25 +00:00
// Set new rating filter.
2016-12-09 17:04:44 +00:00
$product_visibility_terms = wc_get_product_visibility_term_ids();
$tax_query[] = array(
2016-12-09 15:43:25 +00:00
'taxonomy' => 'product_visibility',
2016-12-09 15:54:58 +00:00
'field' => 'term_taxonomy_id',
2016-12-09 17:04:44 +00:00
'terms' => $product_visibility_terms[ 'rated-' . $rating ],
2016-12-09 15:43:25 +00:00
'operator' => 'IN',
'rating_filter' => true,
2016-02-10 10:02:50 +00:00
);
2016-12-09 15:43:25 +00:00
$meta_query = new WP_Meta_Query( $meta_query );
$tax_query = new WP_Tax_Query( $tax_query );
2016-02-10 10:02:50 +00:00
$meta_query_sql = $meta_query->get_sql( 'post', $wpdb->posts, 'ID' );
$tax_query_sql = $tax_query->get_sql( $wpdb->posts, 'ID' );
2016-12-10 12:51:15 +00:00
$sql = "SELECT COUNT( DISTINCT {$wpdb->posts}.ID ) FROM {$wpdb->posts} ";
2016-02-10 10:02:50 +00:00
$sql .= $tax_query_sql['join'] . $meta_query_sql['join'];
$sql .= " WHERE {$wpdb->posts}.post_type = 'product' AND {$wpdb->posts}.post_status = 'publish' ";
$sql .= $tax_query_sql['where'] . $meta_query_sql['where'];
$search = WC_Query::get_main_search_query_sql();
if ( $search ) {
$sql .= ' AND ' . $search;
}
return absint( $wpdb->get_var( $sql ) ); // WPCS: unprepared SQL ok.
2016-02-10 10:02:50 +00:00
}
2015-11-13 09:38:27 +00:00
/**
* Widget function.
2015-11-13 09:38:27 +00:00
*
* @see WP_Widget
* @param array $args Arguments.
* @param array $instance Widget instance.
2015-11-13 09:38:27 +00:00
*/
public function widget( $args, $instance ) {
2017-11-09 11:16:47 +00:00
if ( ! is_shop() && ! is_product_taxonomy() ) {
2015-11-13 09:38:27 +00:00
return;
}
2017-11-09 12:01:27 +00:00
if ( ! wc()->query->get_main_query()->post_count ) {
2016-02-09 21:14:55 +00:00
return;
2015-11-13 09:38:27 +00:00
}
2016-02-10 10:02:50 +00:00
ob_start();
2016-12-09 15:43:25 +00:00
$found = false;
$rating_filter = isset( $_GET['rating_filter'] ) ? array_filter( array_map( 'absint', explode( ',', wp_unslash( $_GET['rating_filter'] ) ) ) ) : array(); // WPCS: input var ok, CSRF ok, sanitization ok.
2015-11-13 09:38:27 +00:00
$this->widget_start( $args, $instance );
echo '<ul>';
2016-12-09 15:43:25 +00:00
for ( $rating = 5; $rating >= 1; $rating-- ) {
2016-02-10 10:02:50 +00:00
$count = $this->get_filtered_product_count( $rating );
2016-12-09 15:43:25 +00:00
if ( empty( $count ) ) {
2016-02-10 10:02:50 +00:00
continue;
}
$found = true;
2017-12-14 14:37:42 +00:00
$link = $this->get_current_page_url();
2015-11-13 09:38:27 +00:00
if ( in_array( $rating, $rating_filter, true ) ) {
2016-12-09 15:43:25 +00:00
$link_ratings = implode( ',', array_diff( $rating_filter, array( $rating ) ) );
} else {
$link_ratings = implode( ',', array_merge( $rating_filter, array( $rating ) ) );
}
$class = in_array( $rating, $rating_filter, true ) ? 'wc-layered-nav-rating chosen' : 'wc-layered-nav-rating';
$link = apply_filters( 'woocommerce_rating_filter_link', $link_ratings ? add_query_arg( 'rating_filter', $link_ratings ) : remove_query_arg( 'rating_filter' ) );
$rating_html = wc_get_star_rating_html( $rating );
$count_html = esc_html( apply_filters( 'woocommerce_rating_filter_count', "({$count})", $count, $rating ) );
2016-12-09 15:43:25 +00:00
printf( '<li class="%s"><a href="%s"><span class="star-rating">%s</span> %s</a></li>', esc_attr( $class ), esc_url( $link ), $rating_html, $count_html ); // WPCS: XSS ok.
2015-11-13 09:38:27 +00:00
}
echo '</ul>';
$this->widget_end( $args );
2016-02-10 10:02:50 +00:00
if ( ! $found ) {
ob_end_clean();
} else {
echo ob_get_clean(); // WPCS: XSS ok.
2016-02-10 10:02:50 +00:00
}
2015-11-13 09:38:27 +00:00
}
2016-02-09 15:18:27 +00:00
}