Completed
Push — master ( a7cd2a...eabd6c )
by Stephen
38:42
created

WP_Widget_Recent_Comments::widget()   B

Complexity

Conditions 9
Paths 64

Size

Total Lines 56
Code Lines 29

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 56
rs 7.1584
cc 9
eloc 29
nc 64
nop 2

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Widget API: WP_Widget_Recent_Comments class
4
 *
5
 * @package WordPress
6
 * @subpackage Widgets
7
 * @since 4.4.0
8
 */
9
10
/**
11
 * Core class used to implement a Recent Comments widget.
12
 *
13
 * @since 2.8.0
14
 *
15
 * @see WP_Widget
16
 */
17
class WP_Widget_Recent_Comments extends WP_Widget {
18
19
	/**
20
	 * Sets up a new Recent Comments widget instance.
21
	 *
22
	 * @since 2.8.0
23
	 * @access public
24
	 */
25
	public function __construct() {
26
		$widget_ops = array('classname' => 'widget_recent_comments', 'description' => __( 'Your site&#8217;s most recent comments.' ) );
27
		parent::__construct('recent-comments', __('Recent Comments'), $widget_ops);
28
		$this->alt_option_name = 'widget_recent_comments';
0 ignored issues
show
Bug introduced by
The property alt_option_name does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
29
30
		if ( is_active_widget(false, false, $this->id_base) )
31
			add_action( 'wp_head', array($this, 'recent_comments_style') );
32
	}
33
34
 	/**
35
	 * Outputs the default styles for the Recent Comments widget.
36
	 *
37
	 * @since 2.8.0
38
	 * @access public
39
	 */
40
	public function recent_comments_style() {
41
		/**
42
		 * Filter the Recent Comments default widget styles.
43
		 *
44
		 * @since 3.1.0
45
		 *
46
		 * @param bool   $active  Whether the widget is active. Default true.
47
		 * @param string $id_base The widget ID.
48
		 */
49
		if ( ! current_theme_supports( 'widgets' ) // Temp hack #14876
50
			|| ! apply_filters( 'show_recent_comments_widget_style', true, $this->id_base ) )
51
			return;
52
		?>
53
		<style type="text/css">.recentcomments a{display:inline !important;padding:0 !important;margin:0 !important;}</style>
54
		<?php
55
	}
56
57
	/**
58
	 * Outputs the content for the current Recent Comments widget instance.
59
	 *
60
	 * @since 2.8.0
61
	 * @access public
62
	 *
63
	 * @param array $args     Display arguments including 'before_title', 'after_title',
64
	 *                        'before_widget', and 'after_widget'.
65
	 * @param array $instance Settings for the current Recent Comments widget instance.
66
	 */
67
	public function widget( $args, $instance ) {
68
		if ( ! isset( $args['widget_id'] ) )
69
			$args['widget_id'] = $this->id;
70
71
		$output = '';
72
73
		$title = ( ! empty( $instance['title'] ) ) ? $instance['title'] : __( 'Recent Comments' );
74
75
		/** This filter is documented in wp-includes/widgets/class-wp-widget-pages.php */
76
		$title = apply_filters( 'widget_title', $title, $instance, $this->id_base );
77
78
		$number = ( ! empty( $instance['number'] ) ) ? absint( $instance['number'] ) : 5;
79
		if ( ! $number )
80
			$number = 5;
81
82
		/**
83
		 * Filter the arguments for the Recent Comments widget.
84
		 *
85
		 * @since 3.4.0
86
		 *
87
		 * @see WP_Comment_Query::query() for information on accepted arguments.
88
		 *
89
		 * @param array $comment_args An array of arguments used to retrieve the recent comments.
90
		 */
91
		$comments = get_comments( apply_filters( 'widget_comments_args', array(
92
			'number'      => $number,
93
			'status'      => 'approve',
94
			'post_status' => 'publish'
95
		) ) );
96
97
		$output .= $args['before_widget'];
98
		if ( $title ) {
99
			$output .= $args['before_title'] . $title . $args['after_title'];
100
		}
101
102
		$output .= '<ul id="recentcomments">';
103
		if ( is_array( $comments ) && $comments ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $comments of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
104
			// Prime cache for associated posts. (Prime post term cache if we need it for permalinks.)
105
			$post_ids = array_unique( wp_list_pluck( $comments, 'comment_post_ID' ) );
106
			_prime_post_caches( $post_ids, strpos( get_option( 'permalink_structure' ), '%category%' ), false );
0 ignored issues
show
Documentation introduced by
strpos(get_option('perma...ucture'), '%category%') is of type integer, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
107
108
			foreach ( (array) $comments as $comment ) {
109
				$output .= '<li class="recentcomments">';
110
				/* translators: comments widget: 1: comment author, 2: post link */
111
				$output .= sprintf( _x( '%1$s on %2$s', 'widgets' ),
112
					'<span class="comment-author-link">' . get_comment_author_link( $comment ) . '</span>',
113
					'<a href="' . esc_url( get_comment_link( $comment ) ) . '">' . get_the_title( $comment->comment_post_ID ) . '</a>'
114
				);
115
				$output .= '</li>';
116
			}
117
		}
118
		$output .= '</ul>';
119
		$output .= $args['after_widget'];
120
121
		echo $output;
122
	}
123
124
	/**
125
	 * Handles updating settings for the current Recent Comments widget instance.
126
	 *
127
	 * @since 2.8.0
128
	 * @access public
129
	 *
130
	 * @param array $new_instance New settings for this instance as input by the user via
131
	 *                            WP_Widget::form().
132
	 * @param array $old_instance Old settings for this instance.
133
	 * @return array Updated settings to save.
134
	 */
135 View Code Duplication
	public function update( $new_instance, $old_instance ) {
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...
136
		$instance = $old_instance;
137
		$instance['title'] = sanitize_text_field( $new_instance['title'] );
138
		$instance['number'] = absint( $new_instance['number'] );
139
		return $instance;
140
	}
141
142
	/**
143
	 * Outputs the settings form for the Recent Comments widget.
144
	 *
145
	 * @since 2.8.0
146
	 * @access public
147
	 *
148
	 * @param array $instance Current settings.
149
	 */
150
	public function form( $instance ) {
151
		$title = isset( $instance['title'] ) ? $instance['title'] : '';
152
		$number = isset( $instance['number'] ) ? absint( $instance['number'] ) : 5;
153
		?>
154
		<p><label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?></label>
155
		<input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" /></p>
156
157
		<p><label for="<?php echo $this->get_field_id( 'number' ); ?>"><?php _e( 'Number of comments to show:' ); ?></label>
158
		<input class="tiny-text" id="<?php echo $this->get_field_id( 'number' ); ?>" name="<?php echo $this->get_field_name( 'number' ); ?>" type="number" step="1" min="1" value="<?php echo $number; ?>" size="3" /></p>
159
		<?php
160
	}
161
162
	/**
163
	 * Flushes the Recent Comments widget cache.
164
	 *
165
	 * @since 2.8.0
166
	 * @access public
167
	 *
168
	 * @deprecated 4.4.0 Fragment caching was removed in favor of split queries.
169
	 */
170
	public function flush_widget_cache() {
171
		_deprecated_function( __METHOD__, '4.4' );
172
	}
173
}
174