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