PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 6.1.2
Jetpack – WP Security, Backup, Speed, & Growth v6.1.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 / top-posts.php

top-posts.php in Jetpack – WP Security, Backup, Speed, & Growth 6.1.2, at modules/widgets/top-posts.php

650 lines 21.3 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 switch ( $display ) {
323 case 'list' :
324 case 'grid' :
325 // Keep the avatar_size as default dimensions for backward compatibility.
326 $width = (int) $get_image_options['avatar_size'];
327 $height = (int) $get_image_options['avatar_size'];
328
329 // Check if the user has changed the width.
330 if ( ! empty( $get_image_options['width'] ) ) {
331 $width = (int) $get_image_options['width'];
332 }
333
334 // Check if the user has changed the height.
335 if ( ! empty( $get_image_options['height'] ) ) {
336 $height = (int) $get_image_options['height'];
337 }
338
339 foreach ( $posts as &$post ) {
340 $image = Jetpack_PostImages::get_image(
341 $post['post_id'],
342 array(
343 'fallback_to_avatars' => true,
344 'width' => (int) $width,
345 'height' => (int) $height,
346 'avatar_size' => (int) $get_image_options['avatar_size'],
347 )
348 );
349 $post['image'] = $image['src'];
350 if ( 'blavatar' != $image['from'] && 'gravatar' != $image['from'] ) {
351 $post['image'] = jetpack_photon_url( $post['image'], array( 'resize' => "$width,$height" ) );
352 }
353 }
354
355 unset( $post );
356
357 if ( 'grid' == $display ) {
358 echo "<div class='widgets-grid-layout no-grav'>\n";
359 foreach ( $posts as $post ) :
360 ?>
361 <div class="widget-grid-view-image">
362 <?php
363 /**
364 * Fires before each Top Post result, inside <li>.
365 *
366 * @module widgets
367 *
368 * @since 3.2.0
369 *
370 * @param string $post['post_id'] Post ID.
371 */
372 do_action( 'jetpack_widget_top_posts_before_post', $post['post_id'] );
373
374 /**
375 * Filter the permalink of items in the Top Posts widget.
376 *
377 * @module widgets
378 *
379 * @since 4.4.0
380 *
381 * @param string $post['permalink'] Post permalink.
382 * @param array $post Post array.
383 */
384 $filtered_permalink = apply_filters( 'jetpack_top_posts_widget_permalink', $post['permalink'], $post );
385
386 ?>
387 <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">
388 <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" />
389 </a>
390 <?php
391 /**
392 * Fires after each Top Post result, inside <li>.
393 *
394 * @module widgets
395 *
396 * @since 3.2.0
397 *
398 * @param string $post['post_id'] Post ID.
399 */
400 do_action( 'jetpack_widget_top_posts_after_post', $post['post_id'] );
401 ?>
402 </div>
403 <?php
404 endforeach;
405 echo "</div>\n";
406 } else {
407 echo "<ul class='widgets-list-layout no-grav'>\n";
408 foreach ( $posts as $post ) :
409 ?>
410 <li>
411 <?php
412 /** This action is documented in modules/widgets/top-posts.php */
413 do_action( 'jetpack_widget_top_posts_before_post', $post['post_id'] );
414
415 /** This filter is documented in modules/widgets/top-posts.php */
416 $filtered_permalink = apply_filters( 'jetpack_top_posts_widget_permalink', $post['permalink'], $post );
417 ?>
418 <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">
419 <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" />
420 </a>
421 <div class="widgets-list-layout-links">
422 <a href="<?php echo esc_url( $filtered_permalink ); ?>" class="bump-view" data-bump-view="tp">
423 <?php echo esc_html( wp_kses( $post['title'], array() ) ); ?>
424 </a>
425 </div>
426 <?php
427 /** This action is documented in modules/widgets/top-posts.php */
428 do_action( 'jetpack_widget_top_posts_after_post', $post['post_id'] );
429 ?>
430 </li>
431 <?php
432 endforeach;
433 echo "</ul>\n";
434 }
435 break;
436 default :
437 echo '<ul>';
438 foreach ( $posts as $post ) :
439 ?>
440 <li>
441 <?php
442 /** This action is documented in modules/widgets/top-posts.php */
443 do_action( 'jetpack_widget_top_posts_before_post', $post['post_id'] );
444
445 /** This filter is documented in modules/widgets/top-posts.php */
446 $filtered_permalink = apply_filters( 'jetpack_top_posts_widget_permalink', $post['permalink'], $post );
447 ?>
448 <a href="<?php echo esc_url( $filtered_permalink ); ?>" class="bump-view" data-bump-view="tp">
449 <?php echo esc_html( wp_kses( $post['title'], array() ) ); ?>
450 </a>
451 <?php
452 /** This action is documented in modules/widgets/top-posts.php */
453 do_action( 'jetpack_widget_top_posts_after_post', $post['post_id'] );
454 ?>
455 </li>
456 <?php
457 endforeach;
458 echo '</ul>';
459 }
460
461 echo $args['after_widget'];
462 }
463
464 public static function defaults() {
465 return array(
466 'title' => esc_html__( 'Top Posts &amp; Pages', 'jetpack' ),
467 'count' => absint( 10 ),
468 'types' => array( 'post', 'page' ),
469 'ordering' => 'views',
470 'display' => 'text',
471 );
472 }
473
474 /*
475 * Get most liked posts
476 *
477 * ONLY TO BE USED IN WPCOM
478 */
479 function get_by_likes( $count ) {
480 $post_likes = wpl_get_blogs_most_liked_posts();
481 if ( !$post_likes ) {
482 return array();
483 }
484
485 return $this->get_posts( array_keys( $post_likes ), $count );
486 }
487
488 function get_by_views( $count, $args ) {
489 if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
490 global $wpdb;
491
492 $post_views = wp_cache_get( "get_top_posts_$count", 'stats' );
493 if ( false === $post_views ) {
494 $post_views = array_shift( stats_get_daily_history( false, get_current_blog_id(), 'postviews', 'post_id', false, 2, '', $count * 2 + 10, true ) );
495 unset( $post_views[0] );
496 wp_cache_add( "get_top_posts_$count", $post_views, 'stats', 1200);
497 }
498
499 return $this->get_posts( array_keys( $post_views ), $count );
500 }
501
502 /**
503 * Filter the number of days used to calculate Top Posts for the Top Posts widget.
504 * We do not recommend accessing more than 10 days of results at one.
505 * When more than 10 days of results are accessed at once, results should be cached via the WordPress transients API.
506 * Querying for -1 days will give results for an infinite number of days.
507 *
508 * @module widgets
509 *
510 * @since 3.9.3
511 *
512 * @param int 2 Number of days. Default is 2.
513 * @param array $args The widget arguments.
514 */
515 $days = (int) apply_filters( 'jetpack_top_posts_days', 2, $args );
516
517 /** Handling situations where the number of days makes no sense - allows for unlimited days where $days = -1 */
518 if ( 0 == $days || false == $days ) {
519 $days = 2;
520 }
521
522 $post_view_posts = stats_get_from_restapi( array(), 'top-posts?max=11&summarize=1&num=' . absint( $days ) );
523
524 if ( ! isset( $post_view_posts->summary ) || empty( $post_view_posts->summary->postviews ) ) {
525 return array();
526 }
527
528 $post_view_ids = array_filter( wp_list_pluck( $post_view_posts->summary->postviews, 'id' ) );
529
530 if ( ! $post_view_ids ) {
531 return array();
532 }
533
534 return $this->get_posts( $post_view_ids, $count );
535 }
536
537 function get_fallback_posts() {
538 if ( current_user_can( 'edit_theme_options' ) ) {
539 return array();
540 }
541
542 $post_query = new WP_Query;
543
544 $posts = $post_query->query( array(
545 'posts_per_page' => 1,
546 'post_status' => 'publish',
547 'post_type' => array( 'post', 'page' ),
548 'no_found_rows' => true,
549 ) );
550
551 if ( ! $posts ) {
552 return array();
553 }
554
555 $post = array_pop( $posts );
556
557 return $this->get_posts( $post->ID, 1 );
558 }
559
560 function get_posts( $post_ids, $count ) {
561 $counter = 0;
562
563 $posts = array();
564 foreach ( (array) $post_ids as $post_id ) {
565 $post = get_post( $post_id );
566
567 if ( ! $post ) {
568 continue;
569 }
570
571 /**
572 * Attachment pages use the 'inherit' post status by default.
573 * To be able to remove attachment pages from private and password protect posts,
574 * we need to replace their post status by the parent post' status.
575 */
576 if ( 'inherit' == $post->post_status && 'attachment' == $post->post_type ) {
577 $post->post_status = get_post_status( $post_id );
578 }
579
580 // hide private and password protected posts
581 if ( 'publish' != $post->post_status || ! empty( $post->post_password ) ) {
582 continue;
583 }
584
585 // Both get HTML stripped etc on display
586 if ( empty( $post->post_title ) ) {
587 $title_source = $post->post_content;
588 $title = wp_html_excerpt( $title_source, 50 );
589 $title .= '&hellip;';
590 } else {
591 $title = $post->post_title;
592 }
593
594 $permalink = get_permalink( $post->ID );
595
596 $post_type = $post->post_type;
597
598 $posts[] = compact( 'title', 'permalink', 'post_id', 'post_type' );
599 $counter++;
600
601 if ( $counter == $count ) {
602 break; // only need to load and show x number of likes
603 }
604 }
605
606 /**
607 * Filter the Top Posts and Pages.
608 *
609 * @module widgets
610 *
611 * @since 3.0.0
612 *
613 * @param array $posts Array of the most popular posts.
614 * @param array $post_ids Array of Post IDs.
615 * @param string $count Number of Top Posts we want to display.
616 */
617 return apply_filters( 'jetpack_widget_get_top_posts', $posts, $post_ids, $count );
618 }
619 }
620
621 /**
622 * Create a shortcode to display the widget anywhere.
623 *
624 * @since 3.9.2
625 */
626 function jetpack_do_top_posts_widget( $instance ) {
627 // Post Types can't be entered as an array in the shortcode parameters.
628 if ( isset( $instance['types'] ) && is_array( $instance['types'] ) ) {
629 $instance['types'] = implode( ',', $instance['types'] );
630 }
631
632 $instance = shortcode_atts(
633 Jetpack_Top_Posts_Widget::defaults(),
634 $instance,
635 'jetpack_top_posts_widget'
636 );
637
638 // Add a class to allow styling
639 $args = array(
640 'before_widget' => sprintf( '<div class="%s">', 'jetpack_top_posts_widget' ),
641 );
642
643 ob_start();
644 the_widget( 'Jetpack_Top_Posts_Widget', $instance, $args );
645 $output = ob_get_clean();
646
647 return $output;
648 }
649 add_shortcode( 'jetpack_top_posts_widget', 'jetpack_do_top_posts_widget' );
650