WC_Widget_Recent_Reviews::widget()   B
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 49
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
dl 0
loc 49
rs 8.5906
c 0
b 0
f 0
eloc 25
nc 5
nop 2
1
<?php
2
3
if ( ! defined( 'ABSPATH' ) ) {
4
	exit;
5
}
6
7
/**
8
 * Recent Reviews 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_Recent_Reviews extends WC_Widget {
17
18
	/**
19
	 * Constructor.
20
	 */
21 View Code Duplication
	public function __construct() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
22
		$this->widget_cssclass    = 'woocommerce widget_recent_reviews';
23
		$this->widget_description = __( 'Display a list of your most recent reviews on your site.', 'woocommerce' );
24
		$this->widget_id          = 'woocommerce_recent_reviews';
25
		$this->widget_name        = __( 'WooCommerce Recent Reviews', 'woocommerce' );
26
		$this->settings           = array(
27
			'title'  => array(
28
				'type'  => 'text',
29
				'std'   => __( 'Recent Reviews', '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 reviews 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
		global $comments, $comment;
55
56
		if ( $this->get_cached_widget( $args ) ) {
57
			return;
58
		}
59
60
		ob_start();
61
62
		$number   = ! empty( $instance['number'] ) ? absint( $instance['number'] ) : $this->settings['number']['std'];
63
		$comments = get_comments( array( 'number' => $number, 'status' => 'approve', 'post_status' => 'publish', 'post_type' => 'product' ) );
64
65
		if ( $comments ) {
66
			$this->widget_start( $args, $instance );
67
68
			echo '<ul class="product_list_widget">';
69
70
			foreach ( (array) $comments as $comment ) {
71
72
				$_product = wc_get_product( $comment->comment_post_ID );
73
74
				$rating = intval( get_comment_meta( $comment->comment_ID, 'rating', true ) );
75
76
				$rating_html = $_product->get_rating_html( $rating );
77
78
				echo '<li><a href="' . esc_url( get_comment_link( $comment->comment_ID ) ) . '">';
79
80
				echo $_product->get_image();
81
82
				echo $_product->get_title() . '</a>';
83
84
				echo $rating_html;
85
86
				printf( '<span class="reviewer">' . _x( 'by %1$s', 'by comment author', 'woocommerce' ) . '</span>', get_comment_author() );
87
88
				echo '</li>';
89
			}
90
91
			echo '</ul>';
92
93
			$this->widget_end( $args );
94
		}
95
96
		$content = ob_get_clean();
97
98
		echo $content;
99
100
		$this->cache_widget( $args, $content );
101
	}
102
}
103