| @@ -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 | /** |
| @@ -71,12 +77,8 @@ | ||
| 71 | 77 | ); |
| 72 | 78 | |
| 73 | 79 | $this->default_title = __( 'Top Posts & Pages', 'jetpack' ); |
| 74 | 80 | |
| 75 | - if ( is_active_widget( false, false, $this->id_base ) || is_customize_preview() ) { | |
| 76 | - add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_style' ) ); | |
| 77 | - } | |
| 78 | - | |
| 79 | 81 | /** |
| 80 | 82 | * Add explanation about how the statistics are calculated. |
| 81 | 83 | * |
| 82 | 84 | * @module widgets |
| @@ -93,9 +95,12 @@ | ||
| 93 | 95 | * @param array $widget_types List of widgets that are currently removed from the Legacy Widget block. |
| 94 | 96 | * @return array $widget_types New list of widgets that will be removed. |
| 95 | 97 | */ |
| 96 | 98 | public function hide_widget_in_block_editor( $widget_types ) { |
| 97 | - $widget_types[] = 'top-posts'; | |
| 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 | + } | |
| 98 | 103 | return $widget_types; |
| 99 | 104 | } |
| 100 | 105 | |
| 101 | 106 | /** |
| @@ -110,9 +115,9 @@ | ||
| 110 | 115 | * Displays the form for this widget on the Widgets page of the WP Admin area. |
| 111 | 116 | * |
| 112 | 117 | * @param array $instance Instance configuration. |
| 113 | 118 | * |
| 114 | - * @return void | |
| 119 | + * @return string|void | |
| 115 | 120 | */ |
| 116 | 121 | public function form( $instance ) { |
| 117 | 122 | $instance = wp_parse_args( (array) $instance, static::defaults() ); |
| 118 | 123 | |
| @@ -146,9 +151,9 @@ | ||
| 146 | 151 | </p> |
| 147 | 152 | |
| 148 | 153 | <p> |
| 149 | 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> |
| 150 | - <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" /> | |
| 151 | 156 | </p> |
| 152 | 157 | |
| 153 | 158 | <?php if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) : ?> |
| 154 | 159 | <p> |
| @@ -293,9 +298,9 @@ | ||
| 293 | 298 | do_action( 'jetpack_stats_extra', 'widget_view', 'top_posts' ); |
| 294 | 299 | |
| 295 | 300 | $instance = wp_parse_args( (array) $instance, static::defaults() ); |
| 296 | 301 | |
| 297 | - $title = isset( $instance['title'] ) ? $instance['title'] : false; | |
| 302 | + $title = $instance['title'] ?? false; | |
| 298 | 303 | if ( false === $title ) { |
| 299 | 304 | $title = $this->default_title; |
| 300 | 305 | } |
| 301 | 306 | |
| @@ -301,8 +306,11 @@ | ||
| 301 | 306 | |
| 302 | 307 | /** This filter is documented in core/src/wp-includes/default-widgets.php */ |
| 303 | 308 | $title = apply_filters( 'widget_title', $title ); |
| 304 | 309 | |
| 310 | + // Enqueue front end assets. | |
| 311 | + $this->enqueue_style(); | |
| 312 | + | |
| 305 | 313 | $count = isset( $instance['count'] ) ? (int) $instance['count'] : false; |
| 306 | 314 | if ( $count < 1 || 10 < $count ) { |
| 307 | 315 | $count = 10; |
| 308 | 316 | } |
| @@ -426,26 +434,26 @@ | ||
| 426 | 434 | $height = (int) $get_image_options['height']; |
| 427 | 435 | } |
| 428 | 436 | |
| 429 | 437 | foreach ( $posts as &$post ) { |
| 430 | - $image = Jetpack_PostImages::get_image( | |
| 438 | + $image = Images::get_image( | |
| 431 | 439 | $post['post_id'], |
| 432 | 440 | array( |
| 433 | 441 | 'fallback_to_avatars' => (bool) $get_image_options['fallback_to_avatars'], |
| 434 | - 'width' => (int) $width, | |
| 435 | - 'height' => (int) $height, | |
| 442 | + 'width' => $width, | |
| 443 | + 'height' => $height, | |
| 436 | 444 | 'avatar_size' => (int) $get_image_options['avatar_size'], |
| 437 | 445 | ) |
| 438 | 446 | ); |
| 439 | 447 | |
| 440 | 448 | if ( $image ) { |
| 441 | - $post['image'] = Jetpack_PostImages::fit_image_url( | |
| 449 | + $post['image'] = Images::fit_image_url( | |
| 442 | 450 | $image['src'], |
| 443 | 451 | $width, |
| 444 | 452 | $height |
| 445 | 453 | ); |
| 446 | 454 | |
| 447 | - $post['image_srcset'] = Jetpack_PostImages::generate_cropped_srcset( | |
| 455 | + $post['image_srcset'] = Images::generate_cropped_srcset( | |
| 448 | 456 | $image, |
| 449 | 457 | $width, |
| 450 | 458 | $height |
| 451 | 459 | ); |
| @@ -700,9 +708,9 @@ | ||
| 700 | 708 | */ |
| 701 | 709 | $days = (int) apply_filters( 'jetpack_top_posts_days', 2, $args ); |
| 702 | 710 | |
| 703 | 711 | /** Handling situations where the number of days makes no sense - allows for unlimited days where $days = -1 */ |
| 704 | - if ( 0 === $days || false === $days ) { | |
| 712 | + if ( 0 === $days ) { | |
| 705 | 713 | $days = 2; |
| 706 | 714 | } |
| 707 | 715 | |
| 708 | 716 | $query_args = array( |
| @@ -707,9 +715,9 @@ | ||
| 707 | 715 | |
| 708 | 716 | $query_args = array( |
| 709 | 717 | 'max' => 11, |
| 710 | 718 | 'summarize' => 1, |
| 711 | - 'num' => (int) $days, | |
| 719 | + 'num' => $days, | |
| 712 | 720 | ); |
| 713 | 721 | $wpcom_stats = new WPCOM_Stats(); |
| 714 | 722 | $post_view_posts = $wpcom_stats->convert_stats_array_to_object( |
| 715 | 723 | $wpcom_stats->get_top_posts( $query_args ) |