PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 7.8
Jetpack – WP Security, Backup, Speed, & Growth v7.8
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 in Jetpack – WP Security, Backup, Speed, & Growth 7.8, at modules/widgets/top-posts.php

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