class-wc-widget-top-rated-products.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. defined( 'ABSPATH' ) || exit;
  10. /**
  11. * Widget top rated products class.
  12. */
  13. class WC_Widget_Top_Rated_Products extends WC_Widget {
  14. /**
  15. * Constructor.
  16. */
  17. public function __construct() {
  18. $this->widget_cssclass = 'woocommerce widget_top_rated_products';
  19. $this->widget_description = __( "A list of your store's top-rated products.", 'woocommerce' );
  20. $this->widget_id = 'woocommerce_top_rated_products';
  21. $this->widget_name = __( 'Products by Rating', 'woocommerce' );
  22. $this->settings = array(
  23. 'title' => array(
  24. 'type' => 'text',
  25. 'std' => __( 'Top rated products', 'woocommerce' ),
  26. 'label' => __( 'Title', 'woocommerce' ),
  27. ),
  28. 'number' => array(
  29. 'type' => 'number',
  30. 'step' => 1,
  31. 'min' => 1,
  32. 'max' => '',
  33. 'std' => 5,
  34. 'label' => __( 'Number of products to show', 'woocommerce' ),
  35. ),
  36. );
  37. parent::__construct();
  38. }
  39. /**
  40. * Output widget.
  41. *
  42. * @see WP_Widget
  43. * @param array $args Arguments.
  44. * @param array $instance Widget instance.
  45. */
  46. public function widget( $args, $instance ) {
  47. if ( $this->get_cached_widget( $args ) ) {
  48. return;
  49. }
  50. ob_start();
  51. $number = ! empty( $instance['number'] ) ? absint( $instance['number'] ) : $this->settings['number']['std'];
  52. $query_args = array(
  53. 'posts_per_page' => $number,
  54. 'no_found_rows' => 1,
  55. 'post_status' => 'publish',
  56. 'post_type' => 'product',
  57. 'meta_key' => '_wc_average_rating',
  58. 'orderby' => 'meta_value_num',
  59. 'order' => 'DESC',
  60. 'meta_query' => WC()->query->get_meta_query(),
  61. 'tax_query' => WC()->query->get_tax_query(),
  62. ); // WPCS: slow query ok.
  63. $r = new WP_Query( $query_args );
  64. if ( $r->have_posts() ) {
  65. $this->widget_start( $args, $instance );
  66. echo wp_kses_post( apply_filters( 'woocommerce_before_widget_product_list', '<ul class="product_list_widget">' ) );
  67. $template_args = array(
  68. 'widget_id' => $args['widget_id'],
  69. 'show_rating' => true,
  70. );
  71. while ( $r->have_posts() ) {
  72. $r->the_post();
  73. wc_get_template( 'content-widget-product.php', $template_args );
  74. }
  75. echo wp_kses_post( apply_filters( 'woocommerce_after_widget_product_list', '</ul>' ) );
  76. $this->widget_end( $args );
  77. }
  78. wp_reset_postdata();
  79. $content = ob_get_clean();
  80. echo $content; // WPCS: XSS ok.
  81. $this->cache_widget( $args, $content );
  82. }
  83. }