| @@ -11,12 +11,18 @@ | ||
| 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 | |
| 21 | +if ( ! defined( 'ABSPATH' ) ) { | |
| 22 | + exit( 0 ); | |
| 23 | +} | |
| 24 | + | |
| 19 | 25 | // Register the widget for use in Appearance -> Widgets |
| 20 | 26 | add_action( 'widgets_init', 'jetpack_top_posts_widget_init' ); |
| 21 | 27 | |
| 22 | 28 | /** |
| @@ -65,17 +71,14 @@ | ||
| 65 | 71 | apply_filters( 'jetpack_widget_name', __( 'Top Posts & Pages', 'jetpack' ) ), |
| 66 | 72 | array( |
| 67 | 73 | 'description' => __( 'Shows your most viewed posts and pages.', 'jetpack' ), |
| 68 | 74 | 'customize_selective_refresh' => true, |
| 75 | + 'show_instance_in_rest' => true, | |
| 69 | 76 | ) |
| 70 | 77 | ); |
| 71 | 78 | |
| 72 | 79 | $this->default_title = __( 'Top Posts & Pages', 'jetpack' ); |
| 73 | 80 | |
| 74 | - if ( is_active_widget( false, false, $this->id_base ) || is_customize_preview() ) { | |
| 75 | - add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_style' ) ); | |
| 76 | - } | |
| 77 | - | |
| 78 | 81 | /** |
| 79 | 82 | * Add explanation about how the statistics are calculated. |
| 80 | 83 | * |
| 81 | 84 | * @module widgets |
| @@ -82,11 +85,26 @@ | ||
| 82 | 85 | * |
| 83 | 86 | * @since 3.9.3 |
| 84 | 87 | */ |
| 85 | 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' ) ); | |
| 86 | 90 | } |
| 87 | 91 | |
| 88 | 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 | + /** | |
| 89 | 107 | * Enqueue stylesheet. |
| 90 | 108 | */ |
| 91 | 109 | public function enqueue_style() { |
| 92 | 110 | wp_register_style( 'jetpack-top-posts-widget', plugins_url( 'top-posts/style.css', __FILE__ ), array(), '20141013' ); |
| @@ -97,9 +115,9 @@ | ||
| 97 | 115 | * Displays the form for this widget on the Widgets page of the WP Admin area. |
| 98 | 116 | * |
| 99 | 117 | * @param array $instance Instance configuration. |
| 100 | 118 | * |
| 101 | - * @return void | |
| 119 | + * @return string|void | |
| 102 | 120 | */ |
| 103 | 121 | public function form( $instance ) { |
| 104 | 122 | $instance = wp_parse_args( (array) $instance, static::defaults() ); |
| 105 | 123 | |
| @@ -133,9 +151,9 @@ | ||
| 133 | 151 | </p> |
| 134 | 152 | |
| 135 | 153 | <p> |
| 136 | 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> |
| 137 | - <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" /> | |
| 138 | 156 | </p> |
| 139 | 157 | |
| 140 | 158 | <?php if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) : ?> |
| 141 | 159 | <p> |
| @@ -280,9 +298,9 @@ | ||
| 280 | 298 | do_action( 'jetpack_stats_extra', 'widget_view', 'top_posts' ); |
| 281 | 299 | |
| 282 | 300 | $instance = wp_parse_args( (array) $instance, static::defaults() ); |
| 283 | 301 | |
| 284 | - $title = isset( $instance['title'] ) ? $instance['title'] : false; | |
| 302 | + $title = $instance['title'] ?? false; | |
| 285 | 303 | if ( false === $title ) { |
| 286 | 304 | $title = $this->default_title; |
| 287 | 305 | } |
| 288 | 306 | |
| @@ -288,8 +306,11 @@ | ||
| 288 | 306 | |
| 289 | 307 | /** This filter is documented in core/src/wp-includes/default-widgets.php */ |
| 290 | 308 | $title = apply_filters( 'widget_title', $title ); |
| 291 | 309 | |
| 310 | + // Enqueue front end assets. | |
| 311 | + $this->enqueue_style(); | |
| 312 | + | |
| 292 | 313 | $count = isset( $instance['count'] ) ? (int) $instance['count'] : false; |
| 293 | 314 | if ( $count < 1 || 10 < $count ) { |
| 294 | 315 | $count = 10; |
| 295 | 316 | } |
| @@ -413,26 +434,26 @@ | ||
| 413 | 434 | $height = (int) $get_image_options['height']; |
| 414 | 435 | } |
| 415 | 436 | |
| 416 | 437 | foreach ( $posts as &$post ) { |
| 417 | - $image = Jetpack_PostImages::get_image( | |
| 438 | + $image = Images::get_image( | |
| 418 | 439 | $post['post_id'], |
| 419 | 440 | array( |
| 420 | 441 | 'fallback_to_avatars' => (bool) $get_image_options['fallback_to_avatars'], |
| 421 | - 'width' => (int) $width, | |
| 422 | - 'height' => (int) $height, | |
| 442 | + 'width' => $width, | |
| 443 | + 'height' => $height, | |
| 423 | 444 | 'avatar_size' => (int) $get_image_options['avatar_size'], |
| 424 | 445 | ) |
| 425 | 446 | ); |
| 426 | 447 | |
| 427 | 448 | if ( $image ) { |
| 428 | - $post['image'] = Jetpack_PostImages::fit_image_url( | |
| 449 | + $post['image'] = Images::fit_image_url( | |
| 429 | 450 | $image['src'], |
| 430 | 451 | $width, |
| 431 | 452 | $height |
| 432 | 453 | ); |
| 433 | 454 | |
| 434 | - $post['image_srcset'] = Jetpack_PostImages::generate_cropped_srcset( | |
| 455 | + $post['image_srcset'] = Images::generate_cropped_srcset( | |
| 435 | 456 | $image, |
| 436 | 457 | $width, |
| 437 | 458 | $height |
| 438 | 459 | ); |
| @@ -687,9 +708,9 @@ | ||
| 687 | 708 | */ |
| 688 | 709 | $days = (int) apply_filters( 'jetpack_top_posts_days', 2, $args ); |
| 689 | 710 | |
| 690 | 711 | /** Handling situations where the number of days makes no sense - allows for unlimited days where $days = -1 */ |
| 691 | - if ( 0 === $days || false === $days ) { | |
| 712 | + if ( 0 === $days ) { | |
| 692 | 713 | $days = 2; |
| 693 | 714 | } |
| 694 | 715 | |
| 695 | 716 | $query_args = array( |
| @@ -694,11 +715,14 @@ | ||
| 694 | 715 | |
| 695 | 716 | $query_args = array( |
| 696 | 717 | 'max' => 11, |
| 697 | 718 | 'summarize' => 1, |
| 698 | - 'num' => (int) $days, | |
| 719 | + 'num' => $days, | |
| 699 | 720 | ); |
| 700 | - $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 | + ); | |
| 701 | 725 | |
| 702 | 726 | if ( ! isset( $post_view_posts->summary ) || empty( $post_view_posts->summary->postviews ) ) { |
| 703 | 727 | return array(); |
| 704 | 728 | } |