PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 6.4.4
Jetpack – WP Security, Backup, Speed, & Growth v6.4.4
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 / top-posts.php
top-posts.php
668 lines 21.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /*
4 * Currently, this widget depends on the Stats Module. To not load this file
5 * when the Stats Module is not active would potentially bypass Jetpack's
6 * fatal error detection on module activation, so we always load this file.
7 * Instead, we don't register the widget if the Stats Module isn't active.
8 */
9
10 /**
11 * Register the widget for use in Appearance -> Widgets
12 */
13 add_action( 'widgets_init', 'jetpack_top_posts_widget_init' );
14
15 function jetpack_top_posts_widget_init() {
16 // Currently, this widget depends on the Stats Module
17 if (
18 ( ! defined( 'IS_WPCOM' ) || ! IS_WPCOM )
19 &&
20 ! function_exists( 'stats_get_from_restapi' )
21 ) {
22 return;
23 }
24
25 register_widget( 'Jetpack_Top_Posts_Widget' );
26 }
27
28 class Jetpack_Top_Posts_Widget extends WP_Widget {
29 public $alt_option_name = 'widget_stats_topposts';
30 public $default_title = '';
31
32 function __construct() {
33 parent::__construct(
34 'top-posts',
35 /** This filter is documented in modules/widgets/facebook-likebox.php */
36 apply_filters( 'jetpack_widget_name', __( 'Top Posts &amp; Pages', 'jetpack' ) ),
37 array(
38 'description' => __( 'Shows your most viewed posts and pages.', 'jetpack' ),
39 'customize_selective_refresh' => true,
40 )
41 );
42
43 $this->default_title = __( 'Top Posts &amp; Pages', 'jetpack' );
44
45 if ( is_active_widget( false, false, $this->id_base ) || is_customize_preview() ) {
46 add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_style' ) );
47 }
48
49 /**
50 * Add explanation about how the statistics are calculated.
51 *
52 * @module widgets
53 *
54 * @since 3.9.3
55 */
56 add_action( 'jetpack_widget_top_posts_after_fields', array( $this, 'stats_explanation' ) );
57 }
58
59 function enqueue_style() {
60 wp_register_style( 'jetpack-top-posts-widget', plugins_url( 'top-posts/style.css', __FILE__ ), array(), '20141013' );
61 wp_enqueue_style( 'jetpack-top-posts-widget' );
62 }
63
64 function form( $instance ) {
65 $instance = wp_parse_args( (array) $instance, $this->defaults() );
66
67 if ( false === $instance['title'] ) {
68 $instance['title'] = $this->default_title;
69 }
70 $title = stripslashes( $instance['title'] );
71
72 $count = isset( $instance['count'] ) ? (int) $instance['count'] : 10;
73 if ( $count < 1 || 10 < $count ) {
74 $count = 10;
75 }
76
77 $allowed_post_types = array_values( get_post_types( array( 'public' => true ) ) );
78 $types = isset( $instance['types'] ) ? (array) $instance['types'] : array( 'post', 'page' );
79
80 // 'likes' are not available in Jetpack
81 $ordering = isset( $instance['ordering'] ) && 'likes' === $instance['ordering'] ? 'likes' : 'views';
82
83 if ( isset( $instance['display'] ) && in_array( $instance['display'], array( 'grid', 'list', 'text' ) ) ) {
84 $display = $instance['display'];
85 } else {
86 $display = 'text';
87 }
88
89 ?>
90
91 <p>
92 <label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php esc_html_e( 'Title:', 'jetpack' ); ?></label>
93 <input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" />
94 </p>
95
96 <p>
97 <label for="<?php echo $this->get_field_id( 'count' ); ?>"><?php esc_html_e( 'Maximum number of posts to show (no more than 10):', 'jetpack' ); ?></label>
98 <input id="<?php echo $this->get_field_id( 'count' ); ?>" name="<?php echo $this->get_field_name( 'count' ); ?>" type="number" value="<?php echo (int) $count; ?>" min="1" max="10" />
99 </p>
100
101 <?php if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) : ?>
102 <p>
103 <label><?php esc_html_e( 'Order Top Posts &amp; Pages By:', 'jetpack' ); ?></label>
104 <ul>
105 <li><label><input id="<?php echo $this->get_field_id( 'ordering' ); ?>-likes" name="<?php echo $this->get_field_name( 'ordering' ); ?>" type="radio" value="likes" <?php checked( 'likes', $ordering ); ?> /> <?php esc_html_e( 'Likes', 'jetpack' ); ?></label></li>
106 <li><label><input id="<?php echo $this->get_field_id( 'ordering' ); ?>-views" name="<?php echo $this->get_field_name( 'ordering' ); ?>" type="radio" value="views" <?php checked( 'views', $ordering ); ?> /> <?php esc_html_e( 'Views', 'jetpack' ); ?></label></li>
107 </ul>
108 </p>
109 <?php endif; ?>
110
111 <p>
112 <label for="<?php echo $this->get_field_id( 'types' ); ?>"><?php esc_html_e( 'Types of pages to display:', 'jetpack' ); ?></label>
113 <ul>
114 <?php foreach( $allowed_post_types as $type ) {
115 // Get the Post Type name to display next to the checkbox
116 $post_type_object = get_post_type_object( $type );
117 $label = $post_type_object->labels->name;
118
119 $checked = '';
120 if ( in_array( $type, $types ) ) {
121 $checked = 'checked="checked" ';
122 } ?>
123
124 <li><label>
125 <input value="<?php echo esc_attr( $type ); ?>" name="<?php echo $this->get_field_name( 'types' ); ?>[]" id="<?php echo $this->get_field_id( 'types' ); ?>-<?php echo $type; ?>" type="checkbox" <?php echo $checked; ?>>
126 <?php echo esc_html( $label ); ?>
127 </label></li>
128
129 <?php } // End foreach ?>
130 </ul>
131 </p>
132
133 <p>
134 <label><?php esc_html_e( 'Display as:', 'jetpack' ); ?></label>
135 <ul>
136 <li><label><input id="<?php echo $this->get_field_id( 'display' ); ?>-text" name="<?php echo $this->get_field_name( 'display' ); ?>" type="radio" value="text" <?php checked( 'text', $display ); ?> /> <?php esc_html_e( 'Text List', 'jetpack' ); ?></label></li>
137 <li><label><input id="<?php echo $this->get_field_id( 'display' ); ?>-list" name="<?php echo $this->get_field_name( 'display' ); ?>" type="radio" value="list" <?php checked( 'list', $display ); ?> /> <?php esc_html_e( 'Image List', 'jetpack' ); ?></label></li>
138 <li><label><input id="<?php echo $this->get_field_id( 'display' ); ?>-grid" name="<?php echo $this->get_field_name( 'display' ); ?>" type="radio" value="grid" <?php checked( 'grid', $display ); ?> /> <?php esc_html_e( 'Image Grid', 'jetpack' ); ?></label></li>
139 </ul>
140 </p><?php
141
142 /**
143 * Fires after the fields are displayed in the Top Posts Widget settings in wp-admin.
144 *
145 * Allow adding extra content after the fields are displayed.
146 *
147 * @module widgets
148 *
149 * @since 3.9.3
150 *
151 * @param array $args {
152 * @param array $instance The widget instance.
153 * @param object $this The class object.
154 * }
155 */
156 do_action( 'jetpack_widget_top_posts_after_fields', array( $instance, $this ) );
157 }
158
159 /**
160 * Explains how the statics are calculated.
161 */
162 function stats_explanation() {
163 ?>
164
165 <p><?php esc_html_e( 'Top Posts &amp; Pages by views are calculated from 24-48 hours of stats. They take a while to change.', 'jetpack' ); ?></p><?php
166 }
167
168 function update( $new_instance, $old_instance ) {
169 $instance = array();
170 $instance['title'] = wp_kses( $new_instance['title'], array() );
171 if ( $instance['title'] === $this->default_title ) {
172 $instance['title'] = false; // Store as false in case of language change
173 }
174
175 $instance['count'] = (int) $new_instance['count'];
176 if ( $instance['count'] < 1 || 10 < $instance['count'] ) {
177 $instance['count'] = 10;
178 }
179
180 // 'likes' are not available in Jetpack
181 $instance['ordering'] = isset( $new_instance['ordering'] ) && 'likes' == $new_instance['ordering'] ? 'likes' : 'views';
182
183 $allowed_post_types = array_values( get_post_types( array( 'public' => true ) ) );
184 $instance['types'] = $new_instance['types'];
185 foreach( $new_instance['types'] as $key => $type ) {
186 if ( ! in_array( $type, $allowed_post_types ) ) {
187 unset( $new_instance['types'][ $key ] );
188 }
189 }
190
191 if ( isset( $new_instance['display'] ) && in_array( $new_instance['display'], array( 'grid', 'list', 'text' ) ) ) {
192 $instance['display'] = $new_instance['display'];
193 } else {
194 $instance['display'] = 'text';
195 }
196
197 /**
198 * Filters Top Posts Widget settings before they're saved.
199 *
200 * @module widgets
201 *
202 * @since 3.9.3
203 *
204 * @param array $instance The santized widget instance. Only contains data processed by the current widget.
205 * @param array $new_instance The new widget instance before sanitization.
206 */
207 $instance = apply_filters( 'jetpack_top_posts_saving', $instance, $new_instance );
208
209 return $instance;
210 }
211
212 function widget( $args, $instance ) {
213 /** This action is documented in modules/widgets/gravatar-profile.php */
214 do_action( 'jetpack_stats_extra', 'widget_view', 'top_posts' );
215
216 $instance = wp_parse_args( (array) $instance, $this->defaults() );
217
218 $title = isset( $instance['title' ] ) ? $instance['title'] : false;
219 if ( false === $title ) {
220 $title = $this->default_title;
221 }
222 /** This filter is documented in core/src/wp-includes/default-widgets.php */
223 $title = apply_filters( 'widget_title', $title );
224
225 $count = isset( $instance['count'] ) ? (int) $instance['count'] : false;
226 if ( $count < 1 || 10 < $count ) {
227 $count = 10;
228 }
229 /**
230 * Control the number of displayed posts.
231 *
232 * @module widgets
233 *
234 * @since 3.3.0
235 *
236 * @param string $count Number of Posts displayed in the Top Posts widget. Default is 10.
237 */
238 $count = apply_filters( 'jetpack_top_posts_widget_count', $count );
239
240 $types = isset( $instance['types'] ) ? (array) $instance['types'] : array( 'post', 'page' );
241
242 // 'likes' are not available in Jetpack
243 $ordering = isset( $instance['ordering'] ) && 'likes' == $instance['ordering'] ? 'likes' : 'views';
244
245 if ( isset( $instance['display'] ) && in_array( $instance['display'], array( 'grid', 'list', 'text' ) ) ) {
246 $display = $instance['display'];
247 } else {
248 $display = 'text';
249 }
250
251 if ( 'text' != $display ) {
252 $get_image_options = array(
253 'fallback_to_avatars' => true,
254 /** This filter is documented in modules/stats.php */
255 'gravatar_default' => apply_filters( 'jetpack_static_url', set_url_scheme( 'https://en.wordpress.com/i/logo/white-gray-80.png' ) ),
256 'avatar_size' => 40,
257 'width' => null,
258 'height' => null,
259 );
260 if ( 'grid' == $display ) {
261 $get_image_options['avatar_size'] = 200;
262 }
263 /**
264 * Top Posts Widget Image options.
265 *
266 * @module widgets
267 *
268 * @since 1.8.0
269 *
270 * @param array $get_image_options {
271 * Array of Image options.
272 * @type bool true Should we default to Gravatars when no image is found? Default is true.
273 * @type string $gravatar_default Default Image URL if no Gravatar is found.
274 * @type int $avatar_size Default Image size.
275 * @type mixed $width Image width, not set by default and $avatar_size is used instead.
276 * @type mixed $height Image height, not set by default and $avatar_size is used instead.
277 * }
278 */
279 $get_image_options = apply_filters( 'jetpack_top_posts_widget_image_options', $get_image_options );
280 }
281
282 if ( function_exists( 'wpl_get_blogs_most_liked_posts' ) && 'likes' == $ordering ) {
283 $posts = $this->get_by_likes( $count );
284 } else {
285 $posts = $this->get_by_views( $count, $args );
286 }
287
288 // Filter the returned posts. Remove all posts that do not match the chosen Post Types.
289 if ( isset( $types ) ) {
290 foreach ( $posts as $k => $post ) {
291 if ( ! in_array( $post['post_type'], $types ) ) {
292 unset( $posts[$k] );
293 }
294 }
295 }
296
297 if ( ! $posts ) {
298 $posts = $this->get_fallback_posts();
299 }
300
301 echo $args['before_widget'];
302 if ( ! empty( $title ) )
303 echo $args['before_title'] . $title . $args['after_title'];
304
305 if ( ! $posts ) {
306 $link = 'https://jetpack.com/support/getting-more-views-and-traffic/';
307 if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
308 $link = 'http://en.support.wordpress.com/getting-more-site-traffic/';
309 }
310
311 if ( current_user_can( 'edit_theme_options' ) ) {
312 echo '<p>' . sprintf(
313 __( 'There are no posts to display. <a href="%s" target="_blank">Want more traffic?</a>', 'jetpack' ),
314 esc_url( $link )
315 ) . '</p>';
316 }
317
318 echo $args['after_widget'];
319 return;
320 }
321
322 /**
323 * Filter the layout of the Top Posts Widget
324 *
325 * @module widgets
326 *
327 * @since 6.4.0
328 *
329 * @param string $layout layout of the Top Posts Widget (empty string)
330 * @param array $posts IDs of the posts to be displayed
331 * @param array $display Display option from widget form
332 */
333 $layout = apply_filters( 'jetpack_top_posts_widget_layout', '', $posts, $display );
334 if ( ! empty( $layout ) ) {
335 echo $layout;
336 echo $args['after_widget'];
337 return;
338 }
339
340 switch ( $display ) {
341 case 'list' :
342 case 'grid' :
343 // Keep the avatar_size as default dimensions for backward compatibility.
344 $width = (int) $get_image_options['avatar_size'];
345 $height = (int) $get_image_options['avatar_size'];
346
347 // Check if the user has changed the width.
348 if ( ! empty( $get_image_options['width'] ) ) {
349 $width = (int) $get_image_options['width'];
350 }
351
352 // Check if the user has changed the height.
353 if ( ! empty( $get_image_options['height'] ) ) {
354 $height = (int) $get_image_options['height'];
355 }
356
357 foreach ( $posts as &$post ) {
358 $image = Jetpack_PostImages::get_image(
359 $post['post_id'],
360 array(
361 'fallback_to_avatars' => true,
362 'width' => (int) $width,
363 'height' => (int) $height,
364 'avatar_size' => (int) $get_image_options['avatar_size'],
365 )
366 );
367 $post['image'] = $image['src'];
368 if ( 'blavatar' != $image['from'] && 'gravatar' != $image['from'] ) {
369 $post['image'] = jetpack_photon_url( $post['image'], array( 'resize' => "$width,$height" ) );
370 }
371 }
372
373 unset( $post );
374
375 if ( 'grid' == $display ) {
376 echo "<div class='widgets-grid-layout no-grav'>\n";
377 foreach ( $posts as $post ) :
378 ?>
379 <div class="widget-grid-view-image">
380 <?php
381 /**
382 * Fires before each Top Post result, inside <li>.
383 *
384 * @module widgets
385 *
386 * @since 3.2.0
387 *
388 * @param string $post['post_id'] Post ID.
389 */
390 do_action( 'jetpack_widget_top_posts_before_post', $post['post_id'] );
391
392 /**
393 * Filter the permalink of items in the Top Posts widget.
394 *
395 * @module widgets
396 *
397 * @since 4.4.0
398 *
399 * @param string $post['permalink'] Post permalink.
400 * @param array $post Post array.
401 */
402 $filtered_permalink = apply_filters( 'jetpack_top_posts_widget_permalink', $post['permalink'], $post );
403
404 ?>
405 <a href="<?php echo esc_url( $filtered_permalink ); ?>" title="<?php echo esc_attr( wp_kses( $post['title'], array() ) ); ?>" class="bump-view" data-bump-view="tp">
406 <img width="<?php echo absint( $width ); ?>" height="<?php echo absint( $height ); ?>" src="<?php echo esc_url( $post['image'] ); ?>" alt="<?php echo esc_attr( wp_kses( $post['title'], array() ) ); ?>" data-pin-nopin="true" />
407 </a>
408 <?php
409 /**
410 * Fires after each Top Post result, inside <li>.
411 *
412 * @module widgets
413 *
414 * @since 3.2.0
415 *
416 * @param string $post['post_id'] Post ID.
417 */
418 do_action( 'jetpack_widget_top_posts_after_post', $post['post_id'] );
419 ?>
420 </div>
421 <?php
422 endforeach;
423 echo "</div>\n";
424 } else {
425 echo "<ul class='widgets-list-layout no-grav'>\n";
426 foreach ( $posts as $post ) :
427 ?>
428 <li>
429 <?php
430 /** This action is documented in modules/widgets/top-posts.php */
431 do_action( 'jetpack_widget_top_posts_before_post', $post['post_id'] );
432
433 /** This filter is documented in modules/widgets/top-posts.php */
434 $filtered_permalink = apply_filters( 'jetpack_top_posts_widget_permalink', $post['permalink'], $post );
435 ?>
436 <a href="<?php echo esc_url( $filtered_permalink ); ?>" title="<?php echo esc_attr( wp_kses( $post['title'], array() ) ); ?>" class="bump-view" data-bump-view="tp">
437 <img width="<?php echo absint( $width ); ?>" height="<?php echo absint( $height ); ?>" src="<?php echo esc_url( $post['image'] ); ?>" class='widgets-list-layout-blavatar' alt="<?php echo esc_attr( wp_kses( $post['title'], array() ) ); ?>" data-pin-nopin="true" />
438 </a>
439 <div class="widgets-list-layout-links">
440 <a href="<?php echo esc_url( $filtered_permalink ); ?>" class="bump-view" data-bump-view="tp">
441 <?php echo esc_html( wp_kses( $post['title'], array() ) ); ?>
442 </a>
443 </div>
444 <?php
445 /** This action is documented in modules/widgets/top-posts.php */
446 do_action( 'jetpack_widget_top_posts_after_post', $post['post_id'] );
447 ?>
448 </li>
449 <?php
450 endforeach;
451 echo "</ul>\n";
452 }
453 break;
454 default :
455 echo '<ul>';
456 foreach ( $posts as $post ) :
457 ?>
458 <li>
459 <?php
460 /** This action is documented in modules/widgets/top-posts.php */
461 do_action( 'jetpack_widget_top_posts_before_post', $post['post_id'] );
462
463 /** This filter is documented in modules/widgets/top-posts.php */
464 $filtered_permalink = apply_filters( 'jetpack_top_posts_widget_permalink', $post['permalink'], $post );
465 ?>
466 <a href="<?php echo esc_url( $filtered_permalink ); ?>" class="bump-view" data-bump-view="tp">
467 <?php echo esc_html( wp_kses( $post['title'], array() ) ); ?>
468 </a>
469 <?php
470 /** This action is documented in modules/widgets/top-posts.php */
471 do_action( 'jetpack_widget_top_posts_after_post', $post['post_id'] );
472 ?>
473 </li>
474 <?php
475 endforeach;
476 echo '</ul>';
477 }
478
479 echo $args['after_widget'];
480 }
481
482 public static function defaults() {
483 return array(
484 'title' => esc_html__( 'Top Posts &amp; Pages', 'jetpack' ),
485 'count' => absint( 10 ),
486 'types' => array( 'post', 'page' ),
487 'ordering' => 'views',
488 'display' => 'text',
489 );
490 }
491
492 /*
493 * Get most liked posts
494 *
495 * ONLY TO BE USED IN WPCOM
496 */
497 function get_by_likes( $count ) {
498 $post_likes = wpl_get_blogs_most_liked_posts();
499 if ( !$post_likes ) {
500 return array();
501 }
502
503 return $this->get_posts( array_keys( $post_likes ), $count );
504 }
505
506 function get_by_views( $count, $args ) {
507 if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
508 global $wpdb;
509
510 $post_views = wp_cache_get( "get_top_posts_$count", 'stats' );
511 if ( false === $post_views ) {
512 $post_views = array_shift( stats_get_daily_history( false, get_current_blog_id(), 'postviews', 'post_id', false, 2, '', $count * 2 + 10, true ) );
513 unset( $post_views[0] );
514 wp_cache_add( "get_top_posts_$count", $post_views, 'stats', 1200);
515 }
516
517 return $this->get_posts( array_keys( $post_views ), $count );
518 }
519
520 /**
521 * Filter the number of days used to calculate Top Posts for the Top Posts widget.
522 * We do not recommend accessing more than 10 days of results at one.
523 * When more than 10 days of results are accessed at once, results should be cached via the WordPress transients API.
524 * Querying for -1 days will give results for an infinite number of days.
525 *
526 * @module widgets
527 *
528 * @since 3.9.3
529 *
530 * @param int 2 Number of days. Default is 2.
531 * @param array $args The widget arguments.
532 */
533 $days = (int) apply_filters( 'jetpack_top_posts_days', 2, $args );
534
535 /** Handling situations where the number of days makes no sense - allows for unlimited days where $days = -1 */
536 if ( 0 == $days || false == $days ) {
537 $days = 2;
538 }
539
540 $post_view_posts = stats_get_from_restapi( array(), 'top-posts?max=11&summarize=1&num=' . absint( $days ) );
541
542 if ( ! isset( $post_view_posts->summary ) || empty( $post_view_posts->summary->postviews ) ) {
543 return array();
544 }
545
546 $post_view_ids = array_filter( wp_list_pluck( $post_view_posts->summary->postviews, 'id' ) );
547
548 if ( ! $post_view_ids ) {
549 return array();
550 }
551
552 return $this->get_posts( $post_view_ids, $count );
553 }
554
555 function get_fallback_posts() {
556 if ( current_user_can( 'edit_theme_options' ) ) {
557 return array();
558 }
559
560 $post_query = new WP_Query;
561
562 $posts = $post_query->query( array(
563 'posts_per_page' => 1,
564 'post_status' => 'publish',
565 'post_type' => array( 'post', 'page' ),
566 'no_found_rows' => true,
567 ) );
568
569 if ( ! $posts ) {
570 return array();
571 }
572
573 $post = array_pop( $posts );
574
575 return $this->get_posts( $post->ID, 1 );
576 }
577
578 function get_posts( $post_ids, $count ) {
579 $counter = 0;
580
581 $posts = array();
582 foreach ( (array) $post_ids as $post_id ) {
583 $post = get_post( $post_id );
584
585 if ( ! $post ) {
586 continue;
587 }
588
589 /**
590 * Attachment pages use the 'inherit' post status by default.
591 * To be able to remove attachment pages from private and password protect posts,
592 * we need to replace their post status by the parent post' status.
593 */
594 if ( 'inherit' == $post->post_status && 'attachment' == $post->post_type ) {
595 $post->post_status = get_post_status( $post_id );
596 }
597
598 // hide private and password protected posts
599 if ( 'publish' != $post->post_status || ! empty( $post->post_password ) ) {
600 continue;
601 }
602
603 // Both get HTML stripped etc on display
604 if ( empty( $post->post_title ) ) {
605 $title_source = $post->post_content;
606 $title = wp_html_excerpt( $title_source, 50 );
607 $title .= '&hellip;';
608 } else {
609 $title = $post->post_title;
610 }
611
612 $permalink = get_permalink( $post->ID );
613
614 $post_type = $post->post_type;
615
616 $posts[] = compact( 'title', 'permalink', 'post_id', 'post_type' );
617 $counter++;
618
619 if ( $counter == $count ) {
620 break; // only need to load and show x number of likes
621 }
622 }
623
624 /**
625 * Filter the Top Posts and Pages.
626 *
627 * @module widgets
628 *
629 * @since 3.0.0
630 *
631 * @param array $posts Array of the most popular posts.
632 * @param array $post_ids Array of Post IDs.
633 * @param string $count Number of Top Posts we want to display.
634 */
635 return apply_filters( 'jetpack_widget_get_top_posts', $posts, $post_ids, $count );
636 }
637 }
638
639 /**
640 * Create a shortcode to display the widget anywhere.
641 *
642 * @since 3.9.2
643 */
644 function jetpack_do_top_posts_widget( $instance ) {
645 // Post Types can't be entered as an array in the shortcode parameters.
646 if ( isset( $instance['types'] ) && is_array( $instance['types'] ) ) {
647 $instance['types'] = implode( ',', $instance['types'] );
648 }
649
650 $instance = shortcode_atts(
651 Jetpack_Top_Posts_Widget::defaults(),
652 $instance,
653 'jetpack_top_posts_widget'
654 );
655
656 // Add a class to allow styling
657 $args = array(
658 'before_widget' => sprintf( '<div class="%s">', 'jetpack_top_posts_widget' ),
659 );
660
661 ob_start();
662 the_widget( 'Jetpack_Top_Posts_Widget', $instance, $args );
663 $output = ob_get_clean();
664
665 return $output;
666 }
667 add_shortcode( 'jetpack_top_posts_widget', 'jetpack_do_top_posts_widget' );
668