woocommerce/includes/widgets/class-wc-widget-top-rated-p...

108 lines
2.6 KiB
PHP
Raw Normal View History

2011-08-27 11:53:46 +00:00
<?php
/**
2015-11-03 13:31:20 +00:00
* Top Rated Products Widget.
* Gets and displays top rated products in an unordered list.
2012-08-14 17:37:50 +00:00
*
2020-08-05 16:36:24 +00:00
* @package WooCommerce\Widgets
* @version 3.3.0
*/
defined( 'ABSPATH' ) || exit;
/**
* Widget top rated products class.
2011-08-27 11:53:46 +00:00
*/
class WC_Widget_Top_Rated_Products extends WC_Widget {
2012-08-14 17:37:50 +00:00
/**
2015-11-03 13:31:20 +00:00
* Constructor.
2012-08-14 17:37:50 +00:00
*/
public function __construct() {
$this->widget_cssclass = 'woocommerce widget_top_rated_products';
$this->widget_description = __( "A list of your store's top-rated products.", 'woocommerce' );
$this->widget_id = 'woocommerce_top_rated_products';
$this->widget_name = __( 'Products by Rating', 'woocommerce' );
$this->settings = array(
'title' => array(
'type' => 'text',
'std' => __( 'Top rated products', 'woocommerce' ),
'label' => __( 'Title', 'woocommerce' ),
),
'number' => array(
'type' => 'number',
'step' => 1,
'min' => 1,
'max' => '',
'std' => 5,
'label' => __( 'Number of products to show', 'woocommerce' ),
),
);
parent::__construct();
2011-08-27 11:53:46 +00:00
}
2012-08-14 17:37:50 +00:00
/**
* Output widget.
2012-08-14 17:37:50 +00:00
*
* @see WP_Widget
* @param array $args Arguments.
* @param array $instance Widget instance.
2012-08-14 17:37:50 +00:00
*/
public function widget( $args, $instance ) {
2012-08-14 17:37:50 +00:00
if ( $this->get_cached_widget( $args ) ) {
2011-08-27 11:53:46 +00:00
return;
}
2011-08-27 11:53:46 +00:00
ob_start();
$number = ! empty( $instance['number'] ) ? absint( $instance['number'] ) : $this->settings['number']['std'];
2012-08-14 17:37:50 +00:00
2020-01-21 16:03:01 +00:00
$query_args = apply_filters(
'woocommerce_top_rated_products_widget_args',
array(
'posts_per_page' => $number,
'no_found_rows' => 1,
'post_status' => 'publish',
'post_type' => 'product',
'meta_key' => '_wc_average_rating',
'orderby' => 'meta_value_num',
'order' => 'DESC',
'meta_query' => WC()->query->get_meta_query(),
'tax_query' => WC()->query->get_tax_query(),
2020-01-21 16:03:01 +00:00
)
); // WPCS: slow query ok.
2012-08-14 17:37:50 +00:00
$r = new WP_Query( $query_args );
2012-08-14 17:37:50 +00:00
if ( $r->have_posts() ) {
2011-08-27 11:53:46 +00:00
$this->widget_start( $args, $instance );
2017-10-27 14:35:34 +00:00
echo wp_kses_post( apply_filters( 'woocommerce_before_widget_product_list', '<ul class="product_list_widget">' ) );
$template_args = array(
'widget_id' => isset( $args['widget_id'] ) ? $args['widget_id'] : $this->widget_id,
2017-10-27 14:35:34 +00:00
'show_rating' => true,
);
while ( $r->have_posts() ) {
$r->the_post();
wc_get_template( 'content-widget-product.php', $template_args );
}
2017-10-27 14:35:34 +00:00
echo wp_kses_post( apply_filters( 'woocommerce_after_widget_product_list', '</ul>' ) );
$this->widget_end( $args );
}
2012-08-14 17:37:50 +00:00
wp_reset_postdata();
2012-05-13 21:39:21 +00:00
$content = ob_get_clean();
2012-08-14 17:37:50 +00:00
echo $content; // WPCS: XSS ok.
2012-08-14 17:37:50 +00:00
$this->cache_widget( $args, $content );
2011-08-27 11:53:46 +00:00
}
}