Completed
Push — add/related-posts-customize ( 4a306a...1c5144 )
by
unknown
43:30 queued 35:07
created

Jetpack_Widget_Authors::widget()   D

Complexity

Conditions 16
Paths 138

Size

Total Lines 132
Code Lines 65

Duplication

Lines 4
Ratio 3.03 %

Importance

Changes 0
Metric Value
cc 16
eloc 65
nc 138
nop 2
dl 4
loc 132
rs 4.5663
c 0
b 0
f 0

How to fix   Long Method    Complexity   

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
/*
4
 * Widget to display blog authors with avatars and recent posts.
5
 *
6
 * Configurable parameters include:
7
 * 1. Whether to display authors who haven't written any posts
8
 * 2. The number of posts to be displayed per author (defaults to 0)
9
 * 3. Avatar size
10
 */
11
class Jetpack_Widget_Authors extends WP_Widget {
12
	public function __construct() {
13
		parent::__construct(
14
			'authors',
15
			/** This filter is documented in modules/widgets/facebook-likebox.php */
16
			apply_filters( 'jetpack_widget_name', __( 'Authors', 'jetpack' ) ),
17
			array(
18
				'classname' => 'widget_authors',
19
				'description' => __( 'Display blogs authors with avatars and recent posts.', 'jetpack' ),
20
				'customize_selective_refresh' => true,
21
			),
22
			array( 'width' => 300 )
23
		);
24
25
		add_action( 'publish_post', array( __CLASS__, 'flush_cache' ) );
26
		add_action( 'deleted_post', array( __CLASS__, 'flush_cache' ) );
27
		add_action( 'switch_theme', array( __CLASS__, 'flush_cache' ) );
28
	}
29
30
	public static function flush_cache() {
31
		wp_cache_delete( 'widget_authors', 'widget' );
32
		wp_cache_delete( 'widget_authors_ssl', 'widget' );
33
	}
34
35
	public function widget( $args, $instance ) {
36
		global $wpdb;
37
38
		$cache_bucket = is_ssl() ? 'widget_authors_ssl' : 'widget_authors';
39
40
		if ( '%BEG_OF_TITLE%' != $args['before_title'] ) {
41
			if ( $output = wp_cache_get( $cache_bucket, 'widget') ) {
42
				echo $output;
43
				return;
44
			}
45
46
			ob_start();
47
		}
48
49
		$instance = wp_parse_args( $instance, array( 'title' => __( 'Authors', 'jetpack' ), 'all' => false, 'number' => 5, 'avatar_size' => 48 ) );
50
		$instance['number'] = min( 10, max( 0, (int) $instance['number'] ) );
51
52
		// We need to query at least one post to determine whether an author has written any posts or not
53
		$query_number = max( $instance['number'], 1 );
54
55
		$default_excluded_authors = array();
56
		/**
57
		 * Filter authors from the Widget Authors widget.
58
		 *
59
		 * @module widgets
60
		 *
61
		 * @since 4.5.0
62
		 *
63
		 * @param array $default_excluded_authors Array of user ID's that will be excluded
64
		 */
65
		$excluded_authors = apply_filters( 'jetpack_widget_authors_exclude', $default_excluded_authors );
66
67
		$authors = get_users( array(
68
			'fields' => 'all',
69
			'who' => 'authors',
70
			'exclude' => (array) $excluded_authors,
71
		) );
72
73
		echo $args['before_widget'];
74
		/** This filter is documented in core/src/wp-includes/default-widgets.php */
75
		$title = apply_filters( 'widget_title', $instance['title'] );
76
		echo $args['before_title'] . esc_html( $title ) . $args['after_title'];
77
		echo '<ul>';
78
79
		$default_post_type = 'post';
80
		/**
81
		 * Filter types of posts that will be counted in the widget
82
		 *
83
		 * @module widgets
84
		 *
85
		 * @since 4.5.0
86
		 *
87
		 * @param string|array $default_post_type type(s) of posts to count for the widget.
88
		 */
89
		$post_types = apply_filters( 'jetpack_widget_authors_post_types', $default_post_type );
90
91
		foreach ( $authors as $author ) {
92
			$r = new WP_Query( array(
93
				'author'         => $author->ID,
94
				'posts_per_page' => $query_number,
95
				'post_type'      => $post_types,
96
				'post_status'    => 'publish',
97
				'no_found_rows'  => true,
98
				'has_password'   => false,
99
			) );
100
101
			if ( ! $r->have_posts() && ! $instance['all'] ) {
102
				continue;
103
			}
104
105
			echo '<li>';
106
107
			// Display avatar and author name
108
			if ( $r->have_posts() ) {
109
				echo '<a href="' . get_author_posts_url( $author->ID ) . '">';
110
111 View Code Duplication
				if ( $instance['avatar_size'] > 1 ) {
112
					echo ' ' . get_avatar( $author->ID, $instance['avatar_size'], '', true ) . ' ';
113
				}
114
115
				echo '<strong>' . esc_html( $author->display_name ) . '</strong>';
116
				echo '</a>';
117
			}
118
			else if ( $instance['all'] ) {
119 View Code Duplication
				if ( $instance['avatar_size'] > 1 ) {
120
					echo get_avatar( $author->ID, $instance['avatar_size'], '', true ) . ' ';
121
				}
122
123
				echo '<strong>' . esc_html( $author->display_name ) . '</strong>';
124
			}
125
126
			if ( 0 == $instance['number'] ) {
127
				echo '</li>';
128
				continue;
129
			}
130
131
			// Display a short list of recent posts for this author
132
133
			if ( $r->have_posts() ) {
134
				echo '<ul>';
135
136
				while ( $r->have_posts() ) {
137
					$r->the_post();
138
					echo '<li><a href="' . get_permalink() . '">';
139
140
					if ( get_the_title() ) {
141
						echo get_the_title();
142
					} else {
143
						echo get_the_ID();
144
					}
145
146
					echo '</a></li>';
147
				}
148
149
				echo '</ul>';
150
			}
151
152
			echo '</li>';
153
		}
154
155
		echo '</ul>';
156
		echo $args['after_widget'];
157
158
		wp_reset_postdata();
159
160
		if ( '%BEG_OF_TITLE%' != $args['before_title'] ) {
161
			wp_cache_add( $cache_bucket, ob_get_flush(), 'widget' );
162
		}
163
164
		/** This action is documented in modules/widgets/gravatar-profile.php */
165
		do_action( 'jetpack_stats_extra', 'widget_view', 'authors' );
166
	}
167
168
	public function form( $instance ) {
169
		$instance = wp_parse_args( $instance, array( 'title' => '', 'all' => false, 'avatar_size' => 48, 'number' => 0 ) );
170
171
		?>
172
		<p>
173
			<label>
174
				<?php _e( 'Title:', 'jetpack' ); ?>
175
				<input class="widefat" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $instance['title'] ); ?>" />
176
			</label>
177
		</p>
178
		<p>
179
			<label>
180
				<input class="checkbox" type="checkbox" <?php checked( $instance['all'] ); ?> name="<?php echo $this->get_field_name( 'all' ); ?>" />
181
				<?php _e( 'Display all authors (including those who have not written any posts)', 'jetpack' ); ?>
182
			</label>
183
		</p>
184
		<p>
185
			<label>
186
				<?php _e( 'Number of posts to show for each author:', 'jetpack' ); ?>
187
				<input style="width: 50px; text-align: center;" name="<?php echo $this->get_field_name( 'number' ); ?>" type="text" value="<?php echo esc_attr( $instance['number'] ); ?>" />
188
				<?php _e( '(at most 10)', 'jetpack' ); ?>
189
			</label>
190
		</p>
191
		<p>
192
			<label>
193
				<?php _e( 'Avatar Size (px):', 'jetpack' ); ?>
194
				<select name="<?php echo $this->get_field_name( 'avatar_size' ); ?>">
195
					<?php foreach( array( '1' => __( 'No Avatars', 'jetpack' ), '16' => '16x16', '32' => '32x32', '48' => '48x48', '96' => '96x96', '128' => '128x128' ) as $value => $label ) { ?>
196
						<option value="<?php echo esc_attr( $value ); ?>" <?php selected( $value, $instance['avatar_size'] ); ?>><?php echo esc_html( $label ); ?></option>
197
					<?php } ?>
198
				</select>
199
			</label>
200
		</p>
201
		<?php
202
	}
203
204
	/**
205
	 * Updates the widget on save and flushes cache.
206
	 *
207
	 * @param array $new_instance
208
	 * @param array $old_instance
209
	 * @return array
210
	 */
211
	public function update( $new_instance, $old_instance ) {
212
		$new_instance['title'] = strip_tags( $new_instance['title'] );
213
		$new_instance['all'] = isset( $new_instance['all'] );
214
		$new_instance['number'] = (int) $new_instance['number'];
215
		$new_instance['avatar_size'] = (int) $new_instance['avatar_size'];
216
217
		Jetpack_Widget_Authors::flush_cache();
218
219
		return $new_instance;
220
	}
221
}
222
223
add_action( 'widgets_init', 'jetpack_register_widget_authors' );
224
function jetpack_register_widget_authors() {
225
	register_widget( 'Jetpack_Widget_Authors' );
226
};
227