PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 16.3-a.5
Jetpack – WP Security, Backup, Speed, & Growth v16.3-a.5
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 +28 -16 13.0.2 → 16.3-a.5 View file →
@@ -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
@@ -92,9 +95,12 @@
92 95 * @param array $widget_types List of widgets that are currently removed from the Legacy Widget block.
93 96 * @return array $widget_types New list of widgets that will be removed.
94 97 */
95 98 public function hide_widget_in_block_editor( $widget_types ) {
96 - $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 + }
97 103 return $widget_types;
98 104 }
99 105
100 106 /**
@@ -109,9 +115,9 @@
109 115 * Displays the form for this widget on the Widgets page of the WP Admin area.
110 116 *
111 117 * @param array $instance Instance configuration.
112 118 *
113 - * @return void
119 + * @return string|void
114 120 */
115 121 public function form( $instance ) {
116 122 $instance = wp_parse_args( (array) $instance, static::defaults() );
117 123
@@ -145,9 +151,9 @@
145 151 </p>
146 152
147 153 <p>
148 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>
149 - <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" />
150 156 </p>
151 157
152 158 <?php if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) : ?>
153 159 <p>
@@ -292,9 +298,9 @@
292 298 do_action( 'jetpack_stats_extra', 'widget_view', 'top_posts' );
293 299
294 300 $instance = wp_parse_args( (array) $instance, static::defaults() );
295 301
296 - $title = isset( $instance['title'] ) ? $instance['title'] : false;
302 + $title = $instance['title'] ?? false;
297 303 if ( false === $title ) {
298 304 $title = $this->default_title;
299 305 }
300 306
@@ -300,8 +306,11 @@
300 306
301 307 /** This filter is documented in core/src/wp-includes/default-widgets.php */
302 308 $title = apply_filters( 'widget_title', $title );
303 309
310 + // Enqueue front end assets.
311 + $this->enqueue_style();
312 +
304 313 $count = isset( $instance['count'] ) ? (int) $instance['count'] : false;
305 314 if ( $count < 1 || 10 < $count ) {
306 315 $count = 10;
307 316 }
@@ -425,26 +434,26 @@
425 434 $height = (int) $get_image_options['height'];
426 435 }
427 436
428 437 foreach ( $posts as &$post ) {
429 - $image = Jetpack_PostImages::get_image(
438 + $image = Images::get_image(
430 439 $post['post_id'],
431 440 array(
432 441 'fallback_to_avatars' => (bool) $get_image_options['fallback_to_avatars'],
433 - 'width' => (int) $width,
434 - 'height' => (int) $height,
442 + 'width' => $width,
443 + 'height' => $height,
435 444 'avatar_size' => (int) $get_image_options['avatar_size'],
436 445 )
437 446 );
438 447
439 448 if ( $image ) {
440 - $post['image'] = Jetpack_PostImages::fit_image_url(
449 + $post['image'] = Images::fit_image_url(
441 450 $image['src'],
442 451 $width,
443 452 $height
444 453 );
445 454
446 - $post['image_srcset'] = Jetpack_PostImages::generate_cropped_srcset(
455 + $post['image_srcset'] = Images::generate_cropped_srcset(
447 456 $image,
448 457 $width,
449 458 $height
450 459 );
@@ -699,9 +708,9 @@
699 708 */
700 709 $days = (int) apply_filters( 'jetpack_top_posts_days', 2, $args );
701 710
702 711 /** Handling situations where the number of days makes no sense - allows for unlimited days where $days = -1 */
703 - if ( 0 === $days || false === $days ) {
712 + if ( 0 === $days ) {
704 713 $days = 2;
705 714 }
706 715
707 716 $query_args = array(
@@ -706,11 +715,14 @@
706 715
707 716 $query_args = array(
708 717 'max' => 11,
709 718 'summarize' => 1,
710 - 'num' => (int) $days,
719 + 'num' => $days,
711 720 );
712 - $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 + );
713 725
714 726 if ( ! isset( $post_view_posts->summary ) || empty( $post_view_posts->summary->postviews ) ) {
715 727 return array();
716 728 }