WC_Widget_Recently_Viewed::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 23
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 18
nc 1
nop 0
dl 0
loc 23
rs 9.0856
c 0
b 0
f 0
1
<?php
2
3
if ( ! defined( 'ABSPATH' ) ) {
4
	exit;
5
}
6
7
/**
8
 * Recent Products Widget.
9
 *
10
 * @author   WooThemes
11
 * @category Widgets
12
 * @package  WooCommerce/Widgets
13
 * @version  2.3.0
14
 * @extends  WC_Widget
15
 */
16
class WC_Widget_Recently_Viewed extends WC_Widget {
17
18
	/**
19
	 * Constructor.
20
	 */
21
	public function __construct() {
22
		$this->widget_cssclass    = 'woocommerce widget_recently_viewed_products';
23
		$this->widget_description = __( 'Display a list of recently viewed products.', 'woocommerce' );
24
		$this->widget_id          = 'woocommerce_recently_viewed_products';
25
		$this->widget_name        = __( 'WooCommerce Recently Viewed', 'woocommerce' );
26
		$this->settings           = array(
27
			'title'  => array(
28
				'type'  => 'text',
29
				'std'   => __( 'Recently Viewed Products', 'woocommerce' ),
30
				'label' => __( 'Title', 'woocommerce' )
31
			),
32
			'number' => array(
33
				'type'  => 'number',
34
				'step'  => 1,
35
				'min'   => 1,
36
				'max'   => '',
37
				'std'   => 10,
38
				'label' => __( 'Number of products to show', 'woocommerce' )
39
			)
40
		);
41
42
		parent::__construct();
43
	}
44
45
	/**
46
	 * Output widget.
47
	 *
48
	 * @see WP_Widget
49
	 *
50
	 * @param array $args
51
	 * @param array $instance
52
	 */
53
	public function widget( $args, $instance ) {
54
55
		$viewed_products = ! empty( $_COOKIE['woocommerce_recently_viewed'] ) ? (array) explode( '|', $_COOKIE['woocommerce_recently_viewed'] ) : array();
56
		$viewed_products = array_filter( array_map( 'absint', $viewed_products ) );
57
58
		if ( empty( $viewed_products ) ) {
59
			return;
60
		}
61
62
		ob_start();
63
64
		$number = ! empty( $instance['number'] ) ? absint( $instance['number'] ) : $this->settings['number']['std'];
65
66
	    $query_args = array( 'posts_per_page' => $number, 'no_found_rows' => 1, 'post_status' => 'publish', 'post_type' => 'product', 'post__in' => $viewed_products, 'orderby' => 'rand' );
67
68
		$query_args['meta_query']   = array();
69
	    $query_args['meta_query'][] = WC()->query->stock_status_meta_query();
70
	    $query_args['meta_query']   = array_filter( $query_args['meta_query'] );
71
72
		$r = new WP_Query( $query_args );
73
74 View Code Duplication
		if ( $r->have_posts() ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
75
76
			$this->widget_start( $args, $instance );
77
78
			echo '<ul class="product_list_widget">';
79
80
			while ( $r->have_posts() ) {
81
				$r->the_post();
82
				wc_get_template( 'content-widget-product.php' );
83
			}
84
85
			echo '</ul>';
86
87
			$this->widget_end( $args );
88
		}
89
90
		wp_reset_postdata();
91
92
		$content = ob_get_clean();
93
94
		echo $content;
95
	}
96
}
97