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
|
|
|
echo '<li>'; |
72
|
|
|
|
73
|
|
|
// Display avatar and author name |
74
|
|
|
if ( $r->have_posts() ) { |
75
|
|
|
echo '<a href="' . get_author_posts_url( $author->ID ) . '">'; |
76
|
|
|
|
77
|
|
View Code Duplication |
if ( $instance['avatar_size'] > 1 ) |
78
|
|
|
echo ' ' . get_avatar( $author->ID, $instance['avatar_size'], '', true ) . ' '; |
79
|
|
|
|
80
|
|
|
echo '<strong>' . esc_html( $author->display_name ) . '</strong>'; |
81
|
|
|
echo '</a>'; |
82
|
|
|
} |
83
|
|
|
else if ( $instance['all'] ) { |
84
|
|
View Code Duplication |
if ( $instance['avatar_size'] > 1 ) |
85
|
|
|
echo get_avatar( $author->ID, $instance['avatar_size'], '', true ) . ' '; |
86
|
|
|
|
87
|
|
|
echo '<strong>' . esc_html( $author->display_name ) . '</strong>'; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
if ( 0 == $instance['number'] ) { |
91
|
|
|
echo '</li>'; |
92
|
|
|
continue; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
// Display a short list of recent posts for this author |
96
|
|
|
|
97
|
|
|
|
98
|
|
|
if ( $r->have_posts() ){ |
99
|
|
|
echo '<ul>'; |
100
|
|
|
|
101
|
|
|
while ( $r->have_posts() ) { |
102
|
|
|
$r->the_post(); |
103
|
|
|
echo '<li><a href="' . get_permalink() . '">'; |
104
|
|
|
|
105
|
|
|
if ( get_the_title() ) |
106
|
|
|
echo get_the_title(); |
107
|
|
|
else |
108
|
|
|
echo get_the_ID(); |
109
|
|
|
|
110
|
|
|
echo '</a></li>'; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
echo '</ul>'; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
echo '</li>'; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
echo '</ul>'; |
120
|
|
|
echo $args['after_widget']; |
121
|
|
|
|
122
|
|
|
wp_reset_postdata(); |
123
|
|
|
|
124
|
|
|
if ( '%BEG_OF_TITLE%' != $args['before_title'] ) |
125
|
|
|
wp_cache_add( $cache_bucket, ob_get_flush(), 'widget'); |
126
|
|
|
|
127
|
|
|
stats_extra( 'widget_view', 'authors' ); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
public function form( $instance ) { |
131
|
|
|
$instance = wp_parse_args( $instance, array( 'title' => '', 'all' => false, 'avatar_size' => 48, 'number' => 0 ) ); |
132
|
|
|
|
133
|
|
|
?> |
134
|
|
|
<p> |
135
|
|
|
<label> |
136
|
|
|
<?php _e( 'Title:' ); ?> |
137
|
|
|
<input class="widefat" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $instance['title'] ); ?>" /> |
138
|
|
|
</label> |
139
|
|
|
</p> |
140
|
|
|
<p> |
141
|
|
|
<label> |
142
|
|
|
<input class="checkbox" type="checkbox" <?php checked( $instance['all'] ); ?> name="<?php echo $this->get_field_name( 'all' ); ?>" /> |
143
|
|
|
<?php _e( 'Display all authors (including those who have not written any posts)' ); ?> |
144
|
|
|
</label> |
145
|
|
|
</p> |
146
|
|
|
<p> |
147
|
|
|
<label> |
148
|
|
|
<?php _e( 'Number of posts to show for each author:' ); ?> |
149
|
|
|
<input style="width: 50px; text-align: center;" name="<?php echo $this->get_field_name( 'number' ); ?>" type="text" value="<?php echo esc_attr( $instance['number'] ); ?>" /> |
150
|
|
|
<?php _e( '(at most 10)' ); ?> |
151
|
|
|
</label> |
152
|
|
|
</p> |
153
|
|
|
<p> |
154
|
|
|
<label> |
155
|
|
|
<?php _e( 'Avatar Size (px):' ); ?> |
156
|
|
|
<select name="<?php echo $this->get_field_name( 'avatar_size' ); ?>"> |
157
|
|
|
<?php foreach( array( '1' => __( 'No Avatars' ), '16' => '16x16', '32' => '32x32', '48' => '48x48', '96' => '96x96', '128' => '128x128' ) as $value => $label ) { ?> |
158
|
|
|
<option value="<?php echo esc_attr( $value ); ?>" <?php selected( $value, $instance['avatar_size'] ); ?>><?php echo esc_html( $label ); ?></option> |
159
|
|
|
<?php } ?> |
160
|
|
|
</select> |
161
|
|
|
</label> |
162
|
|
|
</p> |
163
|
|
|
<?php |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
public function update( $new_instance, $old_instance ) { |
167
|
|
|
$new_instance['title'] = strip_tags( $new_instance['title'] ); |
168
|
|
|
$new_instance['all'] = isset( $new_instance['all'] ); |
169
|
|
|
$new_instance['number'] = (int) $new_instance['number']; |
170
|
|
|
$new_instance['avatar_size'] = (int) $new_instance['avatar_size']; |
171
|
|
|
|
172
|
|
|
Widget_Authors::flush_cache(); |
173
|
|
|
|
174
|
|
|
return $new_instance; |
175
|
|
|
} |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
add_action( 'widgets_init', function () { |
179
|
|
|
register_widget( 'Widget_Authors' ); |
180
|
|
|
} ); |
181
|
|
|
|