Completed
Push — widgets/5493-additions ( dc56c0...74466f )
by George
321:30 queued 312:13
created

Widget_Authors   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 166
Duplicated Lines 2.41 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
dl 4
loc 166
rs 10
c 0
b 0
f 0
wmc 21
lcom 0
cbo 0

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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 Widget_Authors extends WP_Widget {
12
	public function __construct() {
13
		parent::__construct(
14
			'authors',
15
			__( 'Authors' ),
16
			array( 'classname' => 'widget_authors', 'description' => __( 'Display blogs authors with avatars and recent posts.' ) ),
17
			array( 'width' => 300 )
18
		);
19
20
		add_action( 'publish_post', array( __CLASS__, 'flush_cache' ) );
21
		add_action( 'deleted_post', array( __CLASS__, 'flush_cache' ) );
22
		add_action( 'switch_theme', array( __CLASS__, 'flush_cache' ) );
23
	}
24
25
	public static function flush_cache() {
26
		wp_cache_delete( 'widget_authors', 'widget' );
27
		wp_cache_delete( 'widget_authors_ssl', 'widget' );
28
	}
29
30
	public function widget( $args, $instance ) {
31
		global $wpdb;
32
33
		$cache_bucket = is_ssl() ? 'widget_authors_ssl' : 'widget_authors';
34
35
		if ( '%BEG_OF_TITLE%' !== $args['before_title'] ) {
36
			if ( $output = wp_cache_get( $cache_bucket, 'widget') ) {
37
				echo $output;
38
				return;
39
			}
40
41
			ob_start();
42
		}
43
44
		$instance = wp_parse_args( $instance, array( 'title' => __( 'Authors' ), 'all' => false, 'number' => 5, 'avatar_size' => 48 ) );
45
		$instance['number'] = min( 10, max( 0, (int) $instance['number'] ) );
46
47
		// We need to query at least one post to determine whether an author has written any posts or not
48
		$query_number = max( $instance['number'], 1 );
49
50
		$authors = get_users( array(
51
			'fields' => 'all',
52
			'who' => 'authors'
53
		) );
54
55
		echo $args['before_widget'];
56
		echo $args['before_title'] . esc_html( $instance['title'] ) . $args['after_title'];
57
		echo '<ul>';
58
59
		foreach ( $authors as $author ) {
60
			$r = new WP_Query( array(
61
				'author'         => $author->ID,
62
				'posts_per_page' => $query_number,
63
				'post_type'      => 'post',
64
				'post_status'    => 'publish',
65
				'no_found_rows'  => true,
66
			) );
67
68
			if ( ! $r->have_posts() && ! $instance['all'] ) {
69
				continue;
70
			}
71
72
			echo '<li>';
73
74
			// Display avatar and author name
75
			if ( $r->have_posts() ) {
76
				echo '<a href="' . get_author_posts_url( $author->ID ) . '">';
77
78
				if ( $instance['avatar_size'] > 1 ) {
79
					echo ' ' . get_avatar( $author->ID, $instance['avatar_size'], '', true ) . ' ';
80
				}
81
82
				echo '<strong>' . esc_html( $author->display_name ) . '</strong>';
83
				echo '</a>';
84
			} elseif ( $instance['all'] ) {
85
				if ( $instance['avatar_size'] > 1 ) {
86
					echo get_avatar( $author->ID, $instance['avatar_size'], '', true ) . ' ';
87
				}
88
89
				echo '<strong>' . esc_html( $author->display_name ) . '</strong>';
90
			}
91
92
			if ( 0 == $instance['number'] ) {
93
				echo '</li>';
94
				continue;
95
			}
96
97
			// Display a short list of recent posts for this author
98
99
100
			if ( $r->have_posts() ){
101
				echo '<ul>';
102
103
				while ( $r->have_posts() ) {
104
					$r->the_post();
105
					echo '<li><a href="' . get_permalink() . '">';
106
107
					if ( get_the_title() ) {
108
						echo get_the_title();
109
					} else {
110
						echo get_the_ID();
111
					}
112
113
					echo '</a></li>';
114
				}
115
116
				echo '</ul>';
117
			}
118
119
			echo '</li>';
120
		}
121
122
		echo '</ul>';
123
		echo $args['after_widget'];
124
125
		wp_reset_postdata();
126
127
		if ( '%BEG_OF_TITLE%' !== $args['before_title'] ) {
128
			wp_cache_add( $cache_bucket, ob_get_flush(), 'widget');
129
		}
130
131
		if ( function_exists( 'stats_extra' ) ) {
132
			stats_extra( 'widget_view', 'authors' );
133
		}
134
	}
135
136
	public function form( $instance ) {
137
		$instance = wp_parse_args( $instance, array( 'title' => '', 'all' => false, 'avatar_size' => 48, 'number' => 0 ) );
138
139
		?>
140
		<p>
141
			<label>
142
				<?php _e( 'Title:' ); ?>
143
				<input class="widefat" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $instance['title'] ); ?>" />
144
			</label>
145
		</p>
146
		<p>
147
			<label>
148
				<input class="checkbox" type="checkbox" <?php checked( $instance['all'] ); ?> name="<?php echo $this->get_field_name( 'all' ); ?>" />
149
				<?php _e( 'Display all authors (including those who have not written any posts)' ); ?>
150
			</label>
151
		</p>
152
		<p>
153
			<label>
154
				<?php _e( 'Number of posts to show for each author:' ); ?>
155
				<input style="width: 50px; text-align: center;" name="<?php echo $this->get_field_name( 'number' ); ?>" type="text" value="<?php echo esc_attr( $instance['number'] ); ?>" />
156
				<?php _e( '(at most 10)' ); ?>
157
			</label>
158
		</p>
159
		<p>
160
			<label>
161
				<?php _e( 'Avatar Size (px):' ); ?>
162
				<select name="<?php echo $this->get_field_name( 'avatar_size' ); ?>">
163
					<?php foreach( array( '1' => __( 'No Avatars' ), '16' => '16x16', '32' => '32x32', '48' => '48x48', '96' => '96x96', '128' => '128x128' ) as $value => $label ) { ?>
164
						<option value="<?php echo esc_attr( $value ); ?>" <?php selected( $value, $instance['avatar_size'] ); ?>><?php echo esc_html( $label ); ?></option>
165
					<?php } ?>
166
				</select>
167
			</label>
168
		</p>
169
		<?php
170
	}
171
172
	public function update( $new_instance, $old_instance ) {
173
		$new_instance['title']       = strip_tags( $new_instance['title'] );
174
		$new_instance['all']         = isset( $new_instance['all'] );
175
		$new_instance['number']      = (int) $new_instance['number'];
176
		$new_instance['avatar_size'] = (int) $new_instance['avatar_size'];
177
178
		self::flush_cache();
179
180
		return $new_instance;
181
	}
182
}
183
184
add_action( 'widgets_init', 'register_authors_widget' );
185
function register_authors_widget() {
186
	register_widget( 'Widget_Authors' );
187
} );
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected ')'
Loading history...
188