PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 9.6.1
Jetpack – WP Security, Backup, Speed, & Growth v9.6.1
16.2-beta 12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 13.7.2 13.8.3 13.9.2 14.0.1 14.1.1 14.2.2 14.3.1 All 501 releases
jetpack / modules / widgets / authors.php

authors.php in Jetpack – WP Security, Backup, Speed, & Growth 9.6.1, at modules/widgets/authors.php

285 lines 8.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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(
69 $instance, array(
70 'title' => __( 'Authors', 'jetpack' ),
71 'all' => false,
72 'number' => 5,
73 'avatar_size' => 48,
74 )
75 );
76 $instance['number'] = min( 10, max( 0, (int) $instance['number'] ) );
77
78 // We need to query at least one post to determine whether an author has written any posts or not
79 $query_number = max( $instance['number'], 1 );
80
81 /**
82 * Filter authors from the Widget Authors widget.
83 *
84 * @module widgets
85 *
86 * @deprecated 7.7.0 Use jetpack_widget_authors_params instead.
87 *
88 * @since 4.5.0
89 *
90 * @param array $default_excluded_authors Array of user ID's that will be excluded
91 */
92 $excluded_authors = apply_filters( 'jetpack_widget_authors_exclude', array() );
93
94 /**
95 * Filter the parameters of `get_users` call in the Widget Authors widget.
96 *
97 * See the following for `get_users` default arguments:
98 * https://codex.wordpress.org/Function_Reference/get_users
99 *
100 * @module widgets
101 *
102 * @since 7.7.0
103 *
104 * @param array $get_author_params Array of params used in `get_user`
105 */
106 $get_author_params = apply_filters(
107 'jetpack_widget_authors_params',
108 array(
109 'who' => 'authors',
110 'exclude' => (array) $excluded_authors,
111 )
112 );
113
114 $authors = get_users( $get_author_params );
115
116 echo $args['before_widget'];
117 /** This filter is documented in core/src/wp-includes/default-widgets.php */
118 $title = apply_filters( 'widget_title', $instance['title'] );
119 echo $args['before_title'] . esc_html( $title ) . $args['after_title'];
120 echo '<ul>';
121
122 $default_post_type = 'post';
123 /**
124 * Filter types of posts that will be counted in the widget
125 *
126 * @module widgets
127 *
128 * @since 4.5.0
129 *
130 * @param string|array $default_post_type type(s) of posts to count for the widget.
131 */
132 $post_types = apply_filters( 'jetpack_widget_authors_post_types', $default_post_type );
133
134 foreach ( $authors as $author ) {
135 $r = new WP_Query(
136 array(
137 'author' => $author->ID,
138 'posts_per_page' => $query_number,
139 'post_type' => $post_types,
140 'post_status' => 'publish',
141 'no_found_rows' => true,
142 'has_password' => false,
143 )
144 );
145
146 if ( ! $r->have_posts() && ! $instance['all'] ) {
147 continue;
148 }
149
150 echo '<li>';
151
152 // Display avatar and author name
153 if ( $r->have_posts() ) {
154 echo '<a href="' . get_author_posts_url( $author->ID ) . '">';
155
156 if ( $instance['avatar_size'] > 1 ) {
157 echo ' ' . get_avatar( $author->ID, $instance['avatar_size'], '', true ) . ' ';
158 }
159
160 echo '<strong>' . esc_html( $author->display_name ) . '</strong>';
161 echo '</a>';
162 } elseif ( $instance['all'] ) {
163 if ( $instance['avatar_size'] > 1 ) {
164 echo get_avatar( $author->ID, $instance['avatar_size'], '', true ) . ' ';
165 }
166
167 echo '<strong>' . esc_html( $author->display_name ) . '</strong>';
168 }
169
170 if ( 0 == $instance['number'] ) {
171 echo '</li>';
172 continue;
173 }
174
175 // Display a short list of recent posts for this author.
176 if ( $r->have_posts() ) {
177 echo '<ul>';
178
179 while ( $r->have_posts() ) {
180 $r->the_post();
181
182 printf(
183 '<li><a href="%1$s" title="%2$s"%3$s>%4$s</a></li>',
184 esc_url( get_permalink() ),
185 esc_attr( wp_kses( get_the_title(), array() ) ),
186 ( get_queried_object_id() === get_the_ID() ? ' aria-current="page"' : '' ),
187 esc_html( wp_kses( get_the_title(), array() ) )
188 );
189 }
190
191 echo '</ul>';
192 }
193
194 echo '</li>';
195 }
196
197 echo '</ul>';
198 echo $args['after_widget'];
199
200 wp_reset_postdata();
201
202 if ( '%BEG_OF_TITLE%' != $args['before_title'] ) {
203 wp_cache_add( $cache_bucket, ob_get_flush(), 'widget' );
204 }
205
206 /** This action is documented in modules/widgets/gravatar-profile.php */
207 do_action( 'jetpack_stats_extra', 'widget_view', 'authors' );
208 }
209
210 public function form( $instance ) {
211 $instance = wp_parse_args(
212 $instance, array(
213 'title' => '',
214 'all' => false,
215 'avatar_size' => 48,
216 'number' => 5,
217 )
218 );
219
220 ?>
221 <p>
222 <label>
223 <?php _e( 'Title:', 'jetpack' ); ?>
224 <input class="widefat" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $instance['title'] ); ?>" />
225 </label>
226 </p>
227 <p>
228 <label>
229 <input class="checkbox" type="checkbox" <?php checked( $instance['all'] ); ?> name="<?php echo $this->get_field_name( 'all' ); ?>" />
230 <?php _e( 'Display all authors (including those who have not written any posts)', 'jetpack' ); ?>
231 </label>
232 </p>
233 <p>
234 <label>
235 <?php _e( 'Number of posts to show for each author:', 'jetpack' ); ?>
236 <input style="width: 50px; text-align: center;" name="<?php echo $this->get_field_name( 'number' ); ?>" type="text" value="<?php echo esc_attr( $instance['number'] ); ?>" />
237 <?php _e( '(at most 10)', 'jetpack' ); ?>
238 </label>
239 </p>
240 <p>
241 <label>
242 <?php _e( 'Avatar Size (px):', 'jetpack' ); ?>
243 <select name="<?php echo $this->get_field_name( 'avatar_size' ); ?>">
244 <?php
245 foreach ( array(
246 '1' => __( 'No Avatars', 'jetpack' ),
247 '16' => '16x16',
248 '32' => '32x32',
249 '48' => '48x48',
250 '96' => '96x96',
251 '128' => '128x128',
252 ) as $value => $label ) {
253 ?>
254 <option value="<?php echo esc_attr( $value ); ?>" <?php selected( $value, $instance['avatar_size'] ); ?>><?php echo esc_html( $label ); ?></option>
255 <?php } ?>
256 </select>
257 </label>
258 </p>
259 <?php
260 }
261
262 /**
263 * Updates the widget on save and flushes cache.
264 *
265 * @param array $new_instance
266 * @param array $old_instance
267 * @return array
268 */
269 public function update( $new_instance, $old_instance ) {
270 $new_instance['title'] = strip_tags( $new_instance['title'] );
271 $new_instance['all'] = isset( $new_instance['all'] );
272 $new_instance['number'] = (int) $new_instance['number'];
273 $new_instance['avatar_size'] = (int) $new_instance['avatar_size'];
274
275 Jetpack_Widget_Authors::flush_cache();
276
277 return $new_instance;
278 }
279 }
280
281 add_action( 'widgets_init', 'jetpack_register_widget_authors' );
282 function jetpack_register_widget_authors() {
283 register_widget( 'Jetpack_Widget_Authors' );
284 };
285