2015-11-13 09:38:27 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
if ( ! defined( 'ABSPATH' ) ) {
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Rating Filter Widget and related functions.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @author WooThemes
|
|
|
|
* @category Widgets
|
|
|
|
* @package WooCommerce/Widgets
|
2016-02-09 15:18:27 +00:00
|
|
|
* @version 2.6.0
|
2015-11-13 09:38:27 +00:00
|
|
|
* @extends WC_Widget
|
|
|
|
*/
|
|
|
|
class WC_Widget_Rating_Filter extends WC_Widget {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor.
|
|
|
|
*/
|
|
|
|
public function __construct() {
|
|
|
|
$this->widget_cssclass = 'woocommerce widget_rating_filter';
|
2016-02-09 15:18:27 +00:00
|
|
|
$this->widget_description = __( 'Filter products by rating when viewing product archives and categories.', 'woocommerce' );
|
2015-11-13 09:38:27 +00:00
|
|
|
$this->widget_id = 'woocommerce_rating_filter';
|
2016-10-12 10:16:30 +00:00
|
|
|
$this->widget_name = __( 'WooCommerce average rating filter', 'woocommerce' );
|
2015-11-13 09:38:27 +00:00
|
|
|
$this->settings = array(
|
|
|
|
'title' => array(
|
|
|
|
'type' => 'text',
|
2016-10-12 10:16:30 +00:00
|
|
|
'std' => __( 'Average rating', 'woocommerce' ),
|
2016-08-27 01:46:45 +00:00
|
|
|
'label' => __( 'Title', 'woocommerce' ),
|
|
|
|
),
|
2015-11-13 09:38:27 +00:00
|
|
|
);
|
|
|
|
parent::__construct();
|
|
|
|
}
|
|
|
|
|
2016-02-09 15:18:27 +00:00
|
|
|
/**
|
|
|
|
* Get current page URL for layered nav items.
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function get_page_base_url() {
|
|
|
|
if ( defined( 'SHOP_IS_ON_FRONT' ) ) {
|
|
|
|
$link = home_url();
|
2016-09-02 01:51:31 +00:00
|
|
|
} elseif ( is_post_type_archive( 'product' ) || is_page( wc_get_page_id( 'shop' ) ) ) {
|
2016-02-09 15:18:27 +00:00
|
|
|
$link = get_post_type_archive_link( 'product' );
|
|
|
|
} else {
|
2016-09-02 01:51:31 +00:00
|
|
|
$link = get_term_link( get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
|
2016-02-09 15:18:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Min/Max
|
|
|
|
if ( isset( $_GET['min_price'] ) ) {
|
|
|
|
$link = add_query_arg( 'min_price', wc_clean( $_GET['min_price'] ), $link );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( isset( $_GET['max_price'] ) ) {
|
|
|
|
$link = add_query_arg( 'max_price', wc_clean( $_GET['max_price'] ), $link );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Orderby
|
|
|
|
if ( isset( $_GET['orderby'] ) ) {
|
|
|
|
$link = add_query_arg( 'orderby', wc_clean( $_GET['orderby'] ), $link );
|
|
|
|
}
|
|
|
|
|
2016-04-27 14:24:10 +00:00
|
|
|
/**
|
|
|
|
* Search Arg.
|
|
|
|
* To support quote characters, first they are decoded from " entities, then URL encoded.
|
|
|
|
*/
|
2016-02-09 15:18:27 +00:00
|
|
|
if ( get_search_query() ) {
|
2016-04-27 14:24:10 +00:00
|
|
|
$link = add_query_arg( 's', rawurlencode( htmlspecialchars_decode( get_search_query() ) ), $link );
|
2016-02-09 15:18:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Post Type Arg
|
|
|
|
if ( isset( $_GET['post_type'] ) ) {
|
|
|
|
$link = add_query_arg( 'post_type', wc_clean( $_GET['post_type'] ), $link );
|
|
|
|
}
|
|
|
|
|
2016-02-10 11:16:49 +00:00
|
|
|
// All current filters
|
|
|
|
if ( $_chosen_attributes = WC_Query::get_layered_nav_chosen_attributes() ) {
|
|
|
|
foreach ( $_chosen_attributes as $name => $data ) {
|
|
|
|
$filter_name = sanitize_title( str_replace( 'pa_', '', $name ) );
|
|
|
|
if ( ! empty( $data['terms'] ) ) {
|
|
|
|
$link = add_query_arg( 'filter_' . $filter_name, implode( ',', $data['terms'] ), $link );
|
|
|
|
}
|
|
|
|
if ( 'or' == $data['query_type'] ) {
|
|
|
|
$link = add_query_arg( 'query_type_' . $filter_name, 'or', $link );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-09 15:18:27 +00:00
|
|
|
return $link;
|
|
|
|
}
|
|
|
|
|
2016-02-10 10:02:50 +00:00
|
|
|
/**
|
2017-03-28 17:58:51 +00:00
|
|
|
* Count products after other filters have occurred by adjusting the main query.
|
2016-02-10 10:02:50 +00:00
|
|
|
* @param int $rating
|
|
|
|
* @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();
|
2016-12-09 15:54:58 +00:00
|
|
|
$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',
|
2016-08-27 01:46:45 +00:00
|
|
|
'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'];
|
|
|
|
|
2016-12-23 14:45:58 +00:00
|
|
|
if ( $search = WC_Query::get_main_search_query_sql() ) {
|
|
|
|
$sql .= ' AND ' . $search;
|
|
|
|
}
|
|
|
|
|
2016-02-10 10:02:50 +00:00
|
|
|
return absint( $wpdb->get_var( $sql ) );
|
|
|
|
}
|
|
|
|
|
2015-11-13 09:38:27 +00:00
|
|
|
/**
|
|
|
|
* widget function.
|
|
|
|
*
|
|
|
|
* @see WP_Widget
|
|
|
|
*
|
|
|
|
* @param array $args
|
|
|
|
* @param array $instance
|
|
|
|
*/
|
|
|
|
public function widget( $args, $instance ) {
|
2016-02-10 10:19:32 +00:00
|
|
|
global $wp_the_query;
|
2015-11-13 09:38:27 +00:00
|
|
|
|
|
|
|
if ( ! is_post_type_archive( 'product' ) && ! is_tax( get_object_taxonomies( 'product' ) ) ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-02-09 21:14:55 +00:00
|
|
|
if ( ! $wp_the_query->post_count ) {
|
|
|
|
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( ',', $_GET['rating_filter'] ) ) ) : array();
|
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;
|
|
|
|
$link = $this->get_page_base_url();
|
2015-11-13 09:38:27 +00:00
|
|
|
|
2016-12-09 15:43:25 +00:00
|
|
|
if ( in_array( $rating, $rating_filter ) ) {
|
|
|
|
$link_ratings = implode( ',', array_diff( $rating_filter, array( $rating ) ) );
|
|
|
|
} else {
|
|
|
|
$link_ratings = implode( ',', array_merge( $rating_filter, array( $rating ) ) );
|
|
|
|
}
|
|
|
|
|
2017-06-15 10:33:09 +00:00
|
|
|
$class = in_array( $rating, $rating_filter ) ? '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
|
|
|
|
2017-06-15 10:33:09 +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 );
|
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();
|
|
|
|
}
|
2015-11-13 09:38:27 +00:00
|
|
|
}
|
2016-02-09 15:18:27 +00:00
|
|
|
}
|