Completed
Push — add/authors-widget ( c543b9 )
by
unknown
53:47 queued 45:05
created

Widget_Authors::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

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