class-wc-widget-brand-description.php
1 year ago
class-wc-widget-brand-nav.php
7 months ago
class-wc-widget-brand-thumbnails.php
1 year ago
class-wc-widget-cart.php
3 years ago
class-wc-widget-layered-nav-filters.php
2 years ago
class-wc-widget-layered-nav.php
7 months ago
class-wc-widget-price-filter.php
7 months ago
class-wc-widget-product-categories.php
7 months 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
1 year 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
1 year ago
class-wc-widget-top-rated-products.php
5 years ago
class-wc-widget-top-rated-products.php
108 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Top Rated Products Widget. |
| 4 | * Gets and displays top rated products in an unordered list. |
| 5 | * |
| 6 | * @package WooCommerce\Widgets |
| 7 | * @version 3.3.0 |
| 8 | */ |
| 9 | |
| 10 | defined( 'ABSPATH' ) || exit; |
| 11 | |
| 12 | /** |
| 13 | * Widget top rated products class. |
| 14 | */ |
| 15 | class WC_Widget_Top_Rated_Products extends WC_Widget { |
| 16 | |
| 17 | /** |
| 18 | * Constructor. |
| 19 | */ |
| 20 | public function __construct() { |
| 21 | $this->widget_cssclass = 'woocommerce widget_top_rated_products'; |
| 22 | $this->widget_description = __( "A list of your store's top-rated products.", 'woocommerce' ); |
| 23 | $this->widget_id = 'woocommerce_top_rated_products'; |
| 24 | $this->widget_name = __( 'Products by Rating list', 'woocommerce' ); |
| 25 | $this->settings = array( |
| 26 | 'title' => array( |
| 27 | 'type' => 'text', |
| 28 | 'std' => __( 'Top rated products', 'woocommerce' ), |
| 29 | 'label' => __( 'Title', 'woocommerce' ), |
| 30 | ), |
| 31 | 'number' => array( |
| 32 | 'type' => 'number', |
| 33 | 'step' => 1, |
| 34 | 'min' => 1, |
| 35 | 'max' => '', |
| 36 | 'std' => 5, |
| 37 | 'label' => __( 'Number of products to show', 'woocommerce' ), |
| 38 | ), |
| 39 | ); |
| 40 | |
| 41 | parent::__construct(); |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Output widget. |
| 46 | * |
| 47 | * @see WP_Widget |
| 48 | * @param array $args Arguments. |
| 49 | * @param array $instance Widget instance. |
| 50 | */ |
| 51 | public function widget( $args, $instance ) { |
| 52 | |
| 53 | if ( $this->get_cached_widget( $args ) ) { |
| 54 | return; |
| 55 | } |
| 56 | |
| 57 | ob_start(); |
| 58 | |
| 59 | $number = ! empty( $instance['number'] ) ? absint( $instance['number'] ) : $this->settings['number']['std']; |
| 60 | |
| 61 | $query_args = apply_filters( |
| 62 | 'woocommerce_top_rated_products_widget_args', |
| 63 | array( |
| 64 | 'posts_per_page' => $number, |
| 65 | 'no_found_rows' => 1, |
| 66 | 'post_status' => 'publish', |
| 67 | 'post_type' => 'product', |
| 68 | 'meta_key' => '_wc_average_rating', |
| 69 | 'orderby' => 'meta_value_num', |
| 70 | 'order' => 'DESC', |
| 71 | 'meta_query' => WC()->query->get_meta_query(), |
| 72 | 'tax_query' => WC()->query->get_tax_query(), |
| 73 | ) |
| 74 | ); // WPCS: slow query ok. |
| 75 | |
| 76 | $r = new WP_Query( $query_args ); |
| 77 | |
| 78 | if ( $r->have_posts() ) { |
| 79 | |
| 80 | $this->widget_start( $args, $instance ); |
| 81 | |
| 82 | echo wp_kses_post( apply_filters( 'woocommerce_before_widget_product_list', '<ul class="product_list_widget">' ) ); |
| 83 | |
| 84 | $template_args = array( |
| 85 | 'widget_id' => isset( $args['widget_id'] ) ? $args['widget_id'] : $this->widget_id, |
| 86 | 'show_rating' => true, |
| 87 | ); |
| 88 | |
| 89 | while ( $r->have_posts() ) { |
| 90 | $r->the_post(); |
| 91 | wc_get_template( 'content-widget-product.php', $template_args ); |
| 92 | } |
| 93 | |
| 94 | echo wp_kses_post( apply_filters( 'woocommerce_after_widget_product_list', '</ul>' ) ); |
| 95 | |
| 96 | $this->widget_end( $args ); |
| 97 | } |
| 98 | |
| 99 | wp_reset_postdata(); |
| 100 | |
| 101 | $content = ob_get_clean(); |
| 102 | |
| 103 | echo $content; // WPCS: XSS ok. |
| 104 | |
| 105 | $this->cache_widget( $args, $content ); |
| 106 | } |
| 107 | } |
| 108 |