PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 16.3-a.7
Jetpack – WP Security, Backup, Speed, & Growth v16.3-a.7
16.3-a.5 16.3-a.7 16.3-a.3 16.3-a.1 16.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 All 506 releases
← All changes | modules/widgets/top-posts.php +57 -24 12.3.2 → 16.3-a.7 View file →
@@ -11,16 +11,19 @@
11 11 */
12 12
13 13 // phpcs:disable Universal.Files.SeparateFunctionsFromOO.Mixed -- TODO: Move classes to appropriately-named class files.
14 14
15 -use Automattic\Jetpack\Image_CDN\Image_CDN_Core;
15 +use Automattic\Jetpack\Post_Media\Images;
16 16 use Automattic\Jetpack\Redirect;
17 17 use Automattic\Jetpack\Stats\WPCOM_Stats;
18 18 use Automattic\Jetpack\Status;
19 +use Automattic\Jetpack\Status\Host;
19 20
20 -/**
21 - * Register the widget for use in Appearance -> Widgets
22 - */
21 +if ( ! defined( 'ABSPATH' ) ) {
22 + exit( 0 );
23 +}
24 +
25 +// Register the widget for use in Appearance -> Widgets
23 26 add_action( 'widgets_init', 'jetpack_top_posts_widget_init' );
24 27
25 28 /**
26 29 * Register the widget, if the Stats module is active.
@@ -68,17 +71,14 @@
68 71 apply_filters( 'jetpack_widget_name', __( 'Top Posts & Pages', 'jetpack' ) ),
69 72 array(
70 73 'description' => __( 'Shows your most viewed posts and pages.', 'jetpack' ),
71 74 'customize_selective_refresh' => true,
75 + 'show_instance_in_rest' => true,
72 76 )
73 77 );
74 78
75 79 $this->default_title = __( 'Top Posts & Pages', 'jetpack' );
76 80
77 - if ( is_active_widget( false, false, $this->id_base ) || is_customize_preview() ) {
78 - add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_style' ) );
79 - }
80 -
81 81 /**
82 82 * Add explanation about how the statistics are calculated.
83 83 *
84 84 * @module widgets
@@ -85,11 +85,26 @@
85 85 *
86 86 * @since 3.9.3
87 87 */
88 88 add_action( 'jetpack_widget_top_posts_after_fields', array( $this, 'stats_explanation' ) );
89 + add_filter( 'widget_types_to_hide_from_legacy_widget_block', array( $this, 'hide_widget_in_block_editor' ) );
89 90 }
90 91
91 92 /**
93 + * Remove the "Top Posts and Pages" widget from the Legacy Widget block
94 + *
95 + * @param array $widget_types List of widgets that are currently removed from the Legacy Widget block.
96 + * @return array $widget_types New list of widgets that will be removed.
97 + */
98 + public function hide_widget_in_block_editor( $widget_types ) {
99 + // @TODO: Hide for Simple sites when the block API starts working.
100 + if ( ! ( new Host() )->is_wpcom_simple() ) {
101 + $widget_types[] = 'top-posts';
102 + }
103 + return $widget_types;
104 + }
105 +
106 + /**
92 107 * Enqueue stylesheet.
93 108 */
94 109 public function enqueue_style() {
95 110 wp_register_style( 'jetpack-top-posts-widget', plugins_url( 'top-posts/style.css', __FILE__ ), array(), '20141013' );
@@ -100,9 +115,9 @@
100 115 * Displays the form for this widget on the Widgets page of the WP Admin area.
101 116 *
102 117 * @param array $instance Instance configuration.
103 118 *
104 - * @return void
119 + * @return string|void
105 120 */
106 121 public function form( $instance ) {
107 122 $instance = wp_parse_args( (array) $instance, static::defaults() );
108 123
@@ -136,9 +151,9 @@
136 151 </p>
137 152
138 153 <p>
139 154 <label for="<?php echo esc_attr( $this->get_field_id( 'count' ) ); ?>"><?php esc_html_e( 'Maximum number of posts to show (no more than 10):', 'jetpack' ); ?></label>
140 - <input id="<?php echo esc_attr( $this->get_field_id( 'count' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'count' ) ); ?>" type="number" value="<?php echo (int) $count; ?>" min="1" max="10" />
155 + <input id="<?php echo esc_attr( $this->get_field_id( 'count' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'count' ) ); ?>" type="number" value="<?php echo esc_attr( (string) $count ); ?>" min="1" max="10" />
141 156 </p>
142 157
143 158 <?php if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) : ?>
144 159 <p>
@@ -283,9 +298,9 @@
283 298 do_action( 'jetpack_stats_extra', 'widget_view', 'top_posts' );
284 299
285 300 $instance = wp_parse_args( (array) $instance, static::defaults() );
286 301
287 - $title = isset( $instance['title'] ) ? $instance['title'] : false;
302 + $title = $instance['title'] ?? false;
288 303 if ( false === $title ) {
289 304 $title = $this->default_title;
290 305 }
291 306
@@ -291,8 +306,11 @@
291 306
292 307 /** This filter is documented in core/src/wp-includes/default-widgets.php */
293 308 $title = apply_filters( 'widget_title', $title );
294 309
310 + // Enqueue front end assets.
311 + $this->enqueue_style();
312 +
295 313 $count = isset( $instance['count'] ) ? (int) $instance['count'] : false;
296 314 if ( $count < 1 || 10 < $count ) {
297 315 $count = 10;
298 316 }
@@ -416,23 +434,33 @@
416 434 $height = (int) $get_image_options['height'];
417 435 }
418 436
419 437 foreach ( $posts as &$post ) {
420 - $image = Jetpack_PostImages::get_image(
438 + $image = Images::get_image(
421 439 $post['post_id'],
422 440 array(
423 441 'fallback_to_avatars' => (bool) $get_image_options['fallback_to_avatars'],
424 - 'width' => (int) $width,
425 - 'height' => (int) $height,
442 + 'width' => $width,
443 + 'height' => $height,
426 444 'avatar_size' => (int) $get_image_options['avatar_size'],
427 445 )
428 446 );
429 447
430 448 if ( $image ) {
449 + $post['image'] = Images::fit_image_url(
450 + $image['src'],
451 + $width,
452 + $height
453 + );
431 454
432 - $post['image'] = $image['src'];
433 - if ( 'blavatar' !== $image['from'] && 'gravatar' !== $image['from'] ) {
434 - $post['image'] = Image_CDN_Core::cdn_url( $post['image'], array( 'resize' => "$width,$height" ) );
455 + $post['image_srcset'] = Images::generate_cropped_srcset(
456 + $image,
457 + $width,
458 + $height
459 + );
460 +
461 + if ( empty( $post['image_srcset'] ) ) {
462 + $post['image_srcset'] = "{$post['image']} 1x";
435 463 }
436 464 }
437 465 }
438 466 unset( $post );
@@ -466,15 +494,16 @@
466 494 $filtered_permalink = apply_filters( 'jetpack_top_posts_widget_permalink', $post['permalink'], $post );
467 495
468 496 if ( $post['image'] ) {
469 497 printf(
470 - '<a href="%1$s" title="%2$s" class="bump-view" data-bump-view="tp"%3$s><img width="%4$d" height="%5$d" src="%6$s" alt="%2$s" data-pin-nopin="true"/></a>',
498 + '<a href="%1$s" title="%2$s" class="bump-view" data-bump-view="tp"%3$s><img loading="lazy" width="%4$d" height="%5$d" src="%6$s" srcset="%7$s" alt="%2$s" data-pin-nopin="true"/></a>',
471 499 esc_url( $filtered_permalink ),
472 500 esc_attr( wp_kses( $post['title'], array() ) ),
473 501 ( get_queried_object_id() === $post['post_id'] ? ' aria-current="page"' : '' ),
474 502 absint( $width ),
475 503 absint( $height ),
476 - esc_url( $post['image'] )
504 + esc_url( $post['image'] ),
505 + esc_attr( $post['image_srcset'] )
477 506 );
478 507 }
479 508
480 509 /**
@@ -503,15 +532,16 @@
503 532 $filtered_permalink = apply_filters( 'jetpack_top_posts_widget_permalink', $post['permalink'], $post );
504 533
505 534 if ( $post['image'] ) {
506 535 printf(
507 - '<a href="%1$s" title="%2$s" class="bump-view" data-bump-view="tp"%3$s><img width="%4$d" height="%5$d" src="%6$s" alt="%2$s" data-pin-nopin="true" class="widgets-list-layout-blavatar" /></a>',
536 + '<a href="%1$s" title="%2$s" class="bump-view" data-bump-view="tp"%3$s><img loading="lazy" width="%4$d" height="%5$d" src="%6$s" srcset="%7$s" alt="%2$s" data-pin-nopin="true" class="widgets-list-layout-blavatar" /></a>',
508 537 esc_url( $filtered_permalink ),
509 538 esc_attr( wp_kses( $post['title'], array() ) ),
510 539 ( get_queried_object_id() === $post['post_id'] ? ' aria-current="page"' : '' ),
511 540 absint( $width ),
512 541 absint( $height ),
513 - esc_url( $post['image'] )
542 + esc_url( $post['image'] ),
543 + esc_attr( $post['image_srcset'] )
514 544 );
515 545 }
516 546
517 547 printf(
@@ -678,9 +708,9 @@
678 708 */
679 709 $days = (int) apply_filters( 'jetpack_top_posts_days', 2, $args );
680 710
681 711 /** Handling situations where the number of days makes no sense - allows for unlimited days where $days = -1 */
682 - if ( 0 === $days || false === $days ) {
712 + if ( 0 === $days ) {
683 713 $days = 2;
684 714 }
685 715
686 716 $query_args = array(
@@ -685,11 +715,14 @@
685 715
686 716 $query_args = array(
687 717 'max' => 11,
688 718 'summarize' => 1,
689 - 'num' => (int) $days,
719 + 'num' => $days,
690 720 );
691 - $post_view_posts = convert_stats_array_to_object( ( new WPCOM_Stats() )->get_top_posts( $query_args ) );
721 + $wpcom_stats = new WPCOM_Stats();
722 + $post_view_posts = $wpcom_stats->convert_stats_array_to_object(
723 + $wpcom_stats->get_top_posts( $query_args )
724 + );
692 725
693 726 if ( ! isset( $post_view_posts->summary ) || empty( $post_view_posts->summary->postviews ) ) {
694 727 return array();
695 728 }