PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 16.3-a.1
Jetpack – WP Security, Backup, Speed, & Growth v16.3-a.1
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 13.9.2 14.0.1 All 504 releases
← All changes | modules/widgets/top-posts.php +40 -18 12.5.216.3-a.1 View file →
@@ -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,9 +115,9 @@
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 122 $instance = wp_parse_args( (array) $instance, static::defaults() );
107 123
@@ -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>
@@ -282,9 +298,9 @@
282 298 do_action( 'jetpack_stats_extra', 'widget_view', 'top_posts' );
283 299
284 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 }
@@ -415,26 +434,26 @@
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 ) {
430 - $post['image'] = Jetpack_PostImages::fit_image_url(
449 + $post['image'] = Images::fit_image_url(
431 450 $image['src'],
432 451 $width,
433 452 $height
434 453 );
435 454
436 - $post['image_srcset'] = Jetpack_PostImages::generate_cropped_srcset(
455 + $post['image_srcset'] = Images::generate_cropped_srcset(
437 456 $image,
438 457 $width,
439 458 $height
440 459 );
@@ -689,9 +708,9 @@
689 708 */
690 709 $days = (int) apply_filters( 'jetpack_top_posts_days', 2, $args );
691 710
692 711 /** Handling situations where the number of days makes no sense - allows for unlimited days where $days = -1 */
693 - if ( 0 === $days || false === $days ) {
712 + if ( 0 === $days ) {
694 713 $days = 2;
695 714 }
696 715
697 716 $query_args = array(
@@ -696,11 +715,14 @@
696 715
697 716 $query_args = array(
698 717 'max' => 11,
699 718 'summarize' => 1,
700 - 'num' => (int) $days,
719 + 'num' => $days,
701 720 );
702 - $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 + );
703 725
704 726 if ( ! isset( $post_view_posts->summary ) || empty( $post_view_posts->summary->postviews ) ) {
705 727 return array();
706 728 }