| @@ -11,15 +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\Post_Media\Images; | |
| 15 | 16 | use Automattic\Jetpack\Redirect; |
| 16 | 17 | use Automattic\Jetpack\Stats\WPCOM_Stats; |
| 17 | 18 | use Automattic\Jetpack\Status; |
| 19 | +use Automattic\Jetpack\Status\Host; | |
| 18 | 20 | |
| 19 | -/** | |
| 20 | - * Register the widget for use in Appearance -> Widgets | |
| 21 | - */ | |
| 21 | +if ( ! defined( 'ABSPATH' ) ) { | |
| 22 | + exit( 0 ); | |
| 23 | +} | |
| 24 | + | |
| 25 | +// Register the widget for use in Appearance -> Widgets | |
| 22 | 26 | add_action( 'widgets_init', 'jetpack_top_posts_widget_init' ); |
| 23 | 27 | |
| 24 | 28 | /** |
| 25 | 29 | * Register the widget, if the Stats module is active. |
| @@ -67,17 +71,14 @@ | ||
| 67 | 71 | apply_filters( 'jetpack_widget_name', __( 'Top Posts & Pages', 'jetpack' ) ), |
| 68 | 72 | array( |
| 69 | 73 | 'description' => __( 'Shows your most viewed posts and pages.', 'jetpack' ), |
| 70 | 74 | 'customize_selective_refresh' => true, |
| 75 | + 'show_instance_in_rest' => true, | |
| 71 | 76 | ) |
| 72 | 77 | ); |
| 73 | 78 | |
| 74 | 79 | $this->default_title = __( 'Top Posts & Pages', 'jetpack' ); |
| 75 | 80 | |
| 76 | - if ( is_active_widget( false, false, $this->id_base ) || is_customize_preview() ) { | |
| 77 | - add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_style' ) ); | |
| 78 | - } | |
| 79 | - | |
| 80 | 81 | /** |
| 81 | 82 | * Add explanation about how the statistics are calculated. |
| 82 | 83 | * |
| 83 | 84 | * @module widgets |
| @@ -84,11 +85,26 @@ | ||
| 84 | 85 | * |
| 85 | 86 | * @since 3.9.3 |
| 86 | 87 | */ |
| 87 | 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' ) ); | |
| 88 | 90 | } |
| 89 | 91 | |
| 90 | 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 | + /** | |
| 91 | 107 | * Enqueue stylesheet. |
| 92 | 108 | */ |
| 93 | 109 | public function enqueue_style() { |
| 94 | 110 | wp_register_style( 'jetpack-top-posts-widget', plugins_url( 'top-posts/style.css', __FILE__ ), array(), '20141013' ); |
| @@ -99,12 +115,12 @@ | ||
| 99 | 115 | * Displays the form for this widget on the Widgets page of the WP Admin area. |
| 100 | 116 | * |
| 101 | 117 | * @param array $instance Instance configuration. |
| 102 | 118 | * |
| 103 | - * @return void | |
| 119 | + * @return string|void | |
| 104 | 120 | */ |
| 105 | 121 | public function form( $instance ) { |
| 106 | - $instance = wp_parse_args( (array) $instance, $this->defaults() ); | |
| 122 | + $instance = wp_parse_args( (array) $instance, static::defaults() ); | |
| 107 | 123 | |
| 108 | 124 | if ( false === $instance['title'] ) { |
| 109 | 125 | $instance['title'] = $this->default_title; |
| 110 | 126 | } |
| @@ -135,9 +151,9 @@ | ||
| 135 | 151 | </p> |
| 136 | 152 | |
| 137 | 153 | <p> |
| 138 | 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> |
| 139 | - <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" /> | |
| 140 | 156 | </p> |
| 141 | 157 | |
| 142 | 158 | <?php if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) : ?> |
| 143 | 159 | <p> |
| @@ -174,9 +190,9 @@ | ||
| 174 | 190 | > |
| 175 | 191 | <?php echo esc_html( $label ); ?> |
| 176 | 192 | </label></li> |
| 177 | 193 | |
| 178 | - <?php } // End foreach ?> | |
| 194 | +<?php } // End foreach ?> | |
| 179 | 195 | </ul> |
| 180 | 196 | </p> |
| 181 | 197 | |
| 182 | 198 | <p> |
| @@ -280,11 +296,11 @@ | ||
| 280 | 296 | public function widget( $args, $instance ) { |
| 281 | 297 | /** This action is documented in modules/widgets/gravatar-profile.php */ |
| 282 | 298 | do_action( 'jetpack_stats_extra', 'widget_view', 'top_posts' ); |
| 283 | 299 | |
| 284 | - $instance = wp_parse_args( (array) $instance, $this->defaults() ); | |
| 300 | + $instance = wp_parse_args( (array) $instance, static::defaults() ); | |
| 285 | 301 | |
| 286 | - $title = isset( $instance['title'] ) ? $instance['title'] : false; | |
| 302 | + $title = $instance['title'] ?? false; | |
| 287 | 303 | if ( false === $title ) { |
| 288 | 304 | $title = $this->default_title; |
| 289 | 305 | } |
| 290 | 306 | |
| @@ -290,8 +306,11 @@ | ||
| 290 | 306 | |
| 291 | 307 | /** This filter is documented in core/src/wp-includes/default-widgets.php */ |
| 292 | 308 | $title = apply_filters( 'widget_title', $title ); |
| 293 | 309 | |
| 310 | + // Enqueue front end assets. | |
| 311 | + $this->enqueue_style(); | |
| 312 | + | |
| 294 | 313 | $count = isset( $instance['count'] ) ? (int) $instance['count'] : false; |
| 295 | 314 | if ( $count < 1 || 10 < $count ) { |
| 296 | 315 | $count = 10; |
| 297 | 316 | } |
| @@ -371,9 +390,9 @@ | ||
| 371 | 390 | * and display a fallback message for admins. |
| 372 | 391 | */ |
| 373 | 392 | if ( ! $posts ) { |
| 374 | 393 | if ( current_user_can( 'edit_theme_options' ) ) { |
| 375 | - echo $this->fallback_message(); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped | |
| 394 | + echo self::fallback_message(); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped | |
| 376 | 395 | } |
| 377 | 396 | |
| 378 | 397 | $posts = $this->get_fallback_posts( $count, $types ); |
| 379 | 398 | } |
| @@ -415,23 +434,33 @@ | ||
| 415 | 434 | $height = (int) $get_image_options['height']; |
| 416 | 435 | } |
| 417 | 436 | |
| 418 | 437 | foreach ( $posts as &$post ) { |
| 419 | - $image = Jetpack_PostImages::get_image( | |
| 438 | + $image = Images::get_image( | |
| 420 | 439 | $post['post_id'], |
| 421 | 440 | array( |
| 422 | 441 | 'fallback_to_avatars' => (bool) $get_image_options['fallback_to_avatars'], |
| 423 | - 'width' => (int) $width, | |
| 424 | - 'height' => (int) $height, | |
| 442 | + 'width' => $width, | |
| 443 | + 'height' => $height, | |
| 425 | 444 | 'avatar_size' => (int) $get_image_options['avatar_size'], |
| 426 | 445 | ) |
| 427 | 446 | ); |
| 428 | 447 | |
| 429 | 448 | if ( $image ) { |
| 449 | + $post['image'] = Images::fit_image_url( | |
| 450 | + $image['src'], | |
| 451 | + $width, | |
| 452 | + $height | |
| 453 | + ); | |
| 430 | 454 | |
| 431 | - $post['image'] = $image['src']; | |
| 432 | - if ( 'blavatar' !== $image['from'] && 'gravatar' !== $image['from'] ) { | |
| 433 | - $post['image'] = jetpack_photon_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"; | |
| 434 | 463 | } |
| 435 | 464 | } |
| 436 | 465 | } |
| 437 | 466 | unset( $post ); |
| @@ -465,15 +494,16 @@ | ||
| 465 | 494 | $filtered_permalink = apply_filters( 'jetpack_top_posts_widget_permalink', $post['permalink'], $post ); |
| 466 | 495 | |
| 467 | 496 | if ( $post['image'] ) { |
| 468 | 497 | printf( |
| 469 | - '<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>', | |
| 470 | 499 | esc_url( $filtered_permalink ), |
| 471 | 500 | esc_attr( wp_kses( $post['title'], array() ) ), |
| 472 | 501 | ( get_queried_object_id() === $post['post_id'] ? ' aria-current="page"' : '' ), |
| 473 | 502 | absint( $width ), |
| 474 | 503 | absint( $height ), |
| 475 | - esc_url( $post['image'] ) | |
| 504 | + esc_url( $post['image'] ), | |
| 505 | + esc_attr( $post['image_srcset'] ) | |
| 476 | 506 | ); |
| 477 | 507 | } |
| 478 | 508 | |
| 479 | 509 | /** |
| @@ -502,15 +532,16 @@ | ||
| 502 | 532 | $filtered_permalink = apply_filters( 'jetpack_top_posts_widget_permalink', $post['permalink'], $post ); |
| 503 | 533 | |
| 504 | 534 | if ( $post['image'] ) { |
| 505 | 535 | printf( |
| 506 | - '<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>', | |
| 507 | 537 | esc_url( $filtered_permalink ), |
| 508 | 538 | esc_attr( wp_kses( $post['title'], array() ) ), |
| 509 | 539 | ( get_queried_object_id() === $post['post_id'] ? ' aria-current="page"' : '' ), |
| 510 | 540 | absint( $width ), |
| 511 | 541 | absint( $height ), |
| 512 | - esc_url( $post['image'] ) | |
| 542 | + esc_url( $post['image'] ), | |
| 543 | + esc_attr( $post['image_srcset'] ) | |
| 513 | 544 | ); |
| 514 | 545 | } |
| 515 | 546 | |
| 516 | 547 | printf( |
| @@ -642,21 +673,20 @@ | ||
| 642 | 673 | public function get_by_views( $count, $args, $types = array( 'post', 'page' ) ) { |
| 643 | 674 | if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) { |
| 644 | 675 | $post_views = wp_cache_get( "get_top_posts_$count", 'stats' ); |
| 645 | 676 | if ( false === $post_views ) { |
| 646 | - $post_views = array_shift( | |
| 647 | - stats_get_daily_history( | |
| 648 | - false, | |
| 649 | - get_current_blog_id(), | |
| 650 | - 'postviews', | |
| 651 | - 'post_id', | |
| 652 | - false, | |
| 653 | - 2, | |
| 654 | - '', | |
| 655 | - $count * 2 + 10, | |
| 656 | - true | |
| 657 | - ) | |
| 677 | + $stats_get_daily_history = stats_get_daily_history( | |
| 678 | + false, | |
| 679 | + get_current_blog_id(), | |
| 680 | + 'postviews', | |
| 681 | + 'post_id', | |
| 682 | + false, | |
| 683 | + 2, | |
| 684 | + '', | |
| 685 | + $count * 2 + 10, | |
| 686 | + true | |
| 658 | 687 | ); |
| 688 | + $post_views = array_shift( $stats_get_daily_history ); | |
| 659 | 689 | unset( $post_views[0] ); |
| 660 | 690 | wp_cache_add( "get_top_posts_$count", $post_views, 'stats', 1200 ); |
| 661 | 691 | } |
| 662 | 692 | |
| @@ -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 | } |