class-wc-widget-recent-reviews.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. /**
  3. * Recent Reviews Widget.
  4. *
  5. * @package WooCommerce/Widgets
  6. * @version 2.3.0
  7. */
  8. defined( 'ABSPATH' ) || exit;
  9. /**
  10. * Widget recent reviews class.
  11. */
  12. class WC_Widget_Recent_Reviews extends WC_Widget {
  13. /**
  14. * Constructor.
  15. */
  16. public function __construct() {
  17. $this->widget_cssclass = 'woocommerce widget_recent_reviews';
  18. $this->widget_description = __( 'Display a list of recent reviews from your store.', 'woocommerce' );
  19. $this->widget_id = 'woocommerce_recent_reviews';
  20. $this->widget_name = __( 'Recent Product Reviews', 'woocommerce' );
  21. $this->settings = array(
  22. 'title' => array(
  23. 'type' => 'text',
  24. 'std' => __( 'Recent reviews', 'woocommerce' ),
  25. 'label' => __( 'Title', 'woocommerce' ),
  26. ),
  27. 'number' => array(
  28. 'type' => 'number',
  29. 'step' => 1,
  30. 'min' => 1,
  31. 'max' => '',
  32. 'std' => 10,
  33. 'label' => __( 'Number of reviews to show', 'woocommerce' ),
  34. ),
  35. );
  36. parent::__construct();
  37. }
  38. /**
  39. * Output widget.
  40. *
  41. * @see WP_Widget
  42. * @param array $args Arguments.
  43. * @param array $instance Widget instance.
  44. */
  45. public function widget( $args, $instance ) {
  46. global $comments, $comment;
  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. $comments = get_comments(
  53. array(
  54. 'number' => $number,
  55. 'status' => 'approve',
  56. 'post_status' => 'publish',
  57. 'post_type' => 'product',
  58. 'parent' => 0,
  59. )
  60. ); // WPCS: override ok.
  61. if ( $comments ) {
  62. $this->widget_start( $args, $instance );
  63. echo wp_kses_post( apply_filters( 'woocommerce_before_widget_product_review_list', '<ul class="product_list_widget">' ) );
  64. foreach ( (array) $comments as $comment ) {
  65. wc_get_template( 'content-widget-reviews.php', array(
  66. 'comment' => $comment,
  67. 'product' => wc_get_product( $comment->comment_post_ID ),
  68. ) );
  69. }
  70. echo wp_kses_post( apply_filters( 'woocommerce_after_widget_product_review_list', '</ul>' ) );
  71. $this->widget_end( $args );
  72. }
  73. $content = ob_get_clean();
  74. echo $content; // WPCS: XSS ok.
  75. $this->cache_widget( $args, $content );
  76. }
  77. }