class-wc-widget-recently-viewed.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. /**
  3. * Recent Products Widget.
  4. *
  5. * @package WooCommerce/Widgets
  6. * @version 3.3.0
  7. */
  8. defined( 'ABSPATH' ) || exit;
  9. /**
  10. * Widget recently viewed.
  11. */
  12. class WC_Widget_Recently_Viewed extends WC_Widget {
  13. /**
  14. * Constructor.
  15. */
  16. public function __construct() {
  17. $this->widget_cssclass = 'woocommerce widget_recently_viewed_products';
  18. $this->widget_description = __( "Display a list of a customer's recently viewed products.", 'woocommerce' );
  19. $this->widget_id = 'woocommerce_recently_viewed_products';
  20. $this->widget_name = __( 'Recent Viewed Products', 'woocommerce' );
  21. $this->settings = array(
  22. 'title' => array(
  23. 'type' => 'text',
  24. 'std' => __( 'Recently Viewed Products', 'woocommerce' ),
  25. 'label' => __( 'Title', 'woocommerce' ),
  26. ),
  27. 'number' => array(
  28. 'type' => 'number',
  29. 'step' => 1,
  30. 'min' => 1,
  31. 'max' => 15,
  32. 'std' => 10,
  33. 'label' => __( 'Number of products 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. $viewed_products = ! empty( $_COOKIE['woocommerce_recently_viewed'] ) ? (array) explode( '|', wp_unslash( $_COOKIE['woocommerce_recently_viewed'] ) ) : array(); // @codingStandardsIgnoreLine
  47. $viewed_products = array_reverse( array_filter( array_map( 'absint', $viewed_products ) ) );
  48. if ( empty( $viewed_products ) ) {
  49. return;
  50. }
  51. ob_start();
  52. $number = ! empty( $instance['number'] ) ? absint( $instance['number'] ) : $this->settings['number']['std'];
  53. $query_args = array(
  54. 'posts_per_page' => $number,
  55. 'no_found_rows' => 1,
  56. 'post_status' => 'publish',
  57. 'post_type' => 'product',
  58. 'post__in' => $viewed_products,
  59. 'orderby' => 'post__in',
  60. );
  61. if ( 'yes' === get_option( 'woocommerce_hide_out_of_stock_items' ) ) {
  62. $query_args['tax_query'] = array(
  63. array(
  64. 'taxonomy' => 'product_visibility',
  65. 'field' => 'name',
  66. 'terms' => 'outofstock',
  67. 'operator' => 'NOT IN',
  68. ),
  69. ); // WPCS: slow query ok.
  70. }
  71. $r = new WP_Query( apply_filters( 'woocommerce_recently_viewed_products_widget_query_args', $query_args ) );
  72. if ( $r->have_posts() ) {
  73. $this->widget_start( $args, $instance );
  74. echo wp_kses_post( apply_filters( 'woocommerce_before_widget_product_list', '<ul class="product_list_widget">' ) );
  75. $template_args = array(
  76. 'widget_id' => $args['widget_id'],
  77. );
  78. while ( $r->have_posts() ) {
  79. $r->the_post();
  80. wc_get_template( 'content-widget-product.php', $template_args );
  81. }
  82. echo wp_kses_post( apply_filters( 'woocommerce_after_widget_product_list', '</ul>' ) );
  83. $this->widget_end( $args );
  84. }
  85. wp_reset_postdata();
  86. $content = ob_get_clean();
  87. echo $content; // WPCS: XSS ok.
  88. }
  89. }