PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 11.0.2
Jetpack – WP Security, Backup, Speed, & Growth v11.0.2
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 11.0.2, at modules/widgets/authors.php

308 lines 9.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
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 /**
21 * Jetpack_Widget_Authors contructor.
22 */
23 public function __construct() {
24 parent::__construct(
25 'authors',
26 /** This filter is documented in modules/widgets/facebook-likebox.php */
27 apply_filters( 'jetpack_widget_name', __( 'Authors', 'jetpack' ) ),
28 array(
29 'classname' => 'widget_authors',
30 'description' => __( 'Display blogs authors with avatars and recent posts.', 'jetpack' ),
31 'customize_selective_refresh' => true,
32 )
33 );
34
35 if ( is_active_widget( false, false, $this->id_base ) || is_active_widget( false, false, 'monster' ) || is_customize_preview() ) {
36 add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_style' ) );
37 }
38
39 add_action( 'publish_post', array( __CLASS__, 'flush_cache' ) );
40 add_action( 'deleted_post', array( __CLASS__, 'flush_cache' ) );
41 add_action( 'switch_theme', array( __CLASS__, 'flush_cache' ) );
42 }
43
44 /**
45 * Enqueue stylesheet to adapt the widget to various themes.
46 *
47 * @since 4.5.0
48 */
49 public function enqueue_style() {
50 wp_register_style( 'jetpack-authors-widget', plugins_url( 'authors/style.css', __FILE__ ), array(), '20161228' );
51 wp_enqueue_style( 'jetpack-authors-widget' );
52 }
53
54 /**
55 * Flush Authors widget cached data.
56 */
57 public static function flush_cache() {
58 wp_cache_delete( 'widget_authors', 'widget' );
59 wp_cache_delete( 'widget_authors_ssl', 'widget' );
60 }
61
62 /**
63 * Echoes the widget content.
64 *
65 * @param array $args Display arguments.
66 * @param array $instance Widget settings for the instance.
67 */
68 public function widget( $args, $instance ) {
69 $cache_bucket = is_ssl() ? 'widget_authors_ssl' : 'widget_authors';
70
71 if ( '%BEG_OF_TITLE%' !== $args['before_title'] ) {
72 $output = wp_cache_get( $cache_bucket, 'widget' );
73 if ( $output ) {
74 echo $output; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Cached widget display.
75 return;
76 }
77
78 ob_start();
79 }
80
81 $instance = wp_parse_args(
82 $instance,
83 array(
84 'title' => __( 'Authors', 'jetpack' ),
85 'all' => false,
86 'number' => 5,
87 'avatar_size' => 48,
88 )
89 );
90 $instance['number'] = min( 10, max( 0, (int) $instance['number'] ) );
91
92 // We need to query at least one post to determine whether an author has written any posts or not.
93 $query_number = max( $instance['number'], 1 );
94
95 /**
96 * Filter authors from the Widget Authors widget.
97 *
98 * @module widgets
99 *
100 * @deprecated 7.7.0 Use jetpack_widget_authors_params instead.
101 *
102 * @since 4.5.0
103 *
104 * @param array $default_excluded_authors Array of user ID's that will be excluded
105 */
106 $excluded_authors = apply_filters( 'jetpack_widget_authors_exclude', array() );
107
108 /**
109 * Filter the parameters of `get_users` call in the Widget Authors widget.
110 *
111 * See the following for `get_users` default arguments:
112 * https://codex.wordpress.org/Function_Reference/get_users
113 *
114 * @module widgets
115 *
116 * @since 7.7.0
117 *
118 * @param array $get_author_params Array of params used in `get_user`
119 */
120 $get_author_params = apply_filters(
121 'jetpack_widget_authors_params',
122 array(
123 'capability' => array( 'edit_posts' ),
124 'exclude' => (array) $excluded_authors,
125 )
126 );
127
128 $authors = get_users( $get_author_params );
129
130 echo $args['before_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
131 /** This filter is documented in core/src/wp-includes/default-widgets.php */
132 $title = apply_filters( 'widget_title', $instance['title'] );
133 echo $args['before_title'] . $title . $args['after_title']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
134 echo '<ul>';
135
136 $default_post_type = 'post';
137 /**
138 * Filter types of posts that will be counted in the widget
139 *
140 * @module widgets
141 *
142 * @since 4.5.0
143 *
144 * @param string|array $default_post_type type(s) of posts to count for the widget.
145 */
146 $post_types = apply_filters( 'jetpack_widget_authors_post_types', $default_post_type );
147
148 foreach ( $authors as $author ) {
149 $r = new WP_Query(
150 array(
151 'author' => $author->ID,
152 'posts_per_page' => $query_number,
153 'post_type' => $post_types,
154 'post_status' => 'publish',
155 'no_found_rows' => true,
156 'has_password' => false,
157 )
158 );
159
160 if ( ! $r->have_posts() && ! $instance['all'] ) {
161 continue;
162 }
163
164 echo '<li>';
165
166 // Display avatar and author name.
167 if ( $r->have_posts() ) {
168 echo '<a href="' . esc_url( get_author_posts_url( $author->ID ) ) . '">';
169
170 if ( $instance['avatar_size'] > 1 ) {
171 echo ' ' . get_avatar( $author->ID, $instance['avatar_size'], '', true ) . ' ';
172 }
173
174 echo '<strong>' . esc_html( $author->display_name ) . '</strong>';
175 echo '</a>';
176 } elseif ( $instance['all'] ) {
177 if ( $instance['avatar_size'] > 1 ) {
178 echo get_avatar( $author->ID, $instance['avatar_size'], '', true ) . ' ';
179 }
180
181 echo '<strong>' . esc_html( $author->display_name ) . '</strong>';
182 }
183
184 if ( 0 === (int) $instance['number'] ) {
185 echo '</li>';
186 continue;
187 }
188
189 // Display a short list of recent posts for this author.
190 if ( $r->have_posts() ) {
191 echo '<ul>';
192
193 while ( $r->have_posts() ) {
194 $r->the_post();
195
196 printf(
197 '<li><a href="%1$s" title="%2$s"%3$s>%4$s</a></li>',
198 esc_url( get_permalink() ),
199 esc_attr( wp_kses( get_the_title(), array() ) ),
200 ( get_queried_object_id() === get_the_ID() ? ' aria-current="page"' : '' ),
201 esc_html( wp_kses( get_the_title(), array() ) )
202 );
203 }
204
205 echo '</ul>';
206 }
207
208 echo '</li>';
209 }
210
211 echo '</ul>';
212 echo $args['after_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
213
214 wp_reset_postdata();
215
216 if ( '%BEG_OF_TITLE%' !== $args['before_title'] ) {
217 wp_cache_add( $cache_bucket, ob_get_flush(), 'widget' );
218 }
219
220 /** This action is documented in modules/widgets/gravatar-profile.php */
221 do_action( 'jetpack_stats_extra', 'widget_view', 'authors' );
222 }
223
224 /**
225 * Outputs the widget settings form.
226 *
227 * @param array $instance Current settings.
228 */
229 public function form( $instance ) {
230 $instance = wp_parse_args(
231 $instance,
232 array(
233 'title' => '',
234 'all' => false,
235 'avatar_size' => 48,
236 'number' => 5,
237 )
238 );
239
240 ?>
241 <p>
242 <label>
243 <?php esc_html_e( 'Title:', 'jetpack' ); ?>
244 <input class="widefat" name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" type="text" value="<?php echo esc_attr( $instance['title'] ); ?>" />
245 </label>
246 </p>
247 <p>
248 <label>
249 <input class="checkbox" type="checkbox" <?php checked( $instance['all'] ); ?> name="<?php echo esc_attr( $this->get_field_name( 'all' ) ); ?>" />
250 <?php esc_html_e( 'Display all authors (including those who have not written any posts)', 'jetpack' ); ?>
251 </label>
252 </p>
253 <p>
254 <label>
255 <?php esc_html_e( 'Number of posts to show for each author:', 'jetpack' ); ?>
256 <input style="width: 50px; text-align: center;" name="<?php echo esc_attr( $this->get_field_name( 'number' ) ); ?>" type="text" value="<?php echo esc_attr( $instance['number'] ); ?>" />
257 <?php esc_html_e( '(at most 10)', 'jetpack' ); ?>
258 </label>
259 </p>
260 <p>
261 <label>
262 <?php esc_html_e( 'Avatar Size (px):', 'jetpack' ); ?>
263 <select name="<?php echo esc_attr( $this->get_field_name( 'avatar_size' ) ); ?>">
264 <?php
265 foreach ( array(
266 '1' => __( 'No Avatars', 'jetpack' ),
267 '16' => '16x16',
268 '32' => '32x32',
269 '48' => '48x48',
270 '96' => '96x96',
271 '128' => '128x128',
272 ) as $value => $label ) {
273 ?>
274 <option value="<?php echo esc_attr( $value ); ?>" <?php selected( $value, $instance['avatar_size'] ); ?>><?php echo esc_html( $label ); ?></option>
275 <?php } ?>
276 </select>
277 </label>
278 </p>
279 <?php
280 }
281
282 /**
283 * Updates the widget on save and flushes cache.
284 *
285 * @param array $new_instance New widget instance data.
286 * @param array $old_instance Old widget instance data.
287 * @return array
288 */
289 public function update( $new_instance, $old_instance ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
290 $new_instance['title'] = wp_strip_all_tags( $new_instance['title'] );
291 $new_instance['all'] = isset( $new_instance['all'] );
292 $new_instance['number'] = (int) $new_instance['number'];
293 $new_instance['avatar_size'] = (int) $new_instance['avatar_size'];
294
295 self::flush_cache();
296
297 return $new_instance;
298 }
299 }
300
301 add_action( 'widgets_init', 'jetpack_register_widget_authors' );
302 /**
303 * Register the Authors widget.
304 */
305 function jetpack_register_widget_authors() {
306 register_widget( 'Jetpack_Widget_Authors' );
307 }
308