summaryrefslogtreecommitdiff
blob: 5dd7b2661874cb940063582d1e4f045b8a4df6af (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
/**
 * Top Posts widget.
 *
 * Currently, this widget depends on the Stats Module. To not load this file
 * when the Stats Module is not active would potentially bypass Jetpack's
 * fatal error detection on module activation, so we always load this file.
 * Instead, we don't register the widget if the Stats Module isn't active.
 *
 * @package automattic/jetpack
 */

use Automattic\Jetpack\Redirect;

/**
 * Register the widget for use in Appearance -> Widgets
 */
add_action( 'widgets_init', 'jetpack_top_posts_widget_init' );

/**
 * Register the widget, if the Stats module is active.
 */
function jetpack_top_posts_widget_init() {
	// Currently, this widget depends on the Stats Module.
	if (
		( ! defined( 'IS_WPCOM' ) || ! IS_WPCOM )
	&&
		! function_exists( 'stats_get_from_restapi' )
	) {
		return;
	}

	register_widget( 'Jetpack_Top_Posts_Widget' );
}

/**
 * Widget class.
 */
class Jetpack_Top_Posts_Widget extends WP_Widget {
	/**
	 * Widget unique identifier.
	 *
	 * @var string
	 */
	public $alt_option_name = 'widget_stats_topposts';

	/**
	 * Widget default title.
	 *
	 * @var string
	 */
	public $default_title = '';

	/**
	 * Constructor.
	 */
	public function __construct() {
		parent::__construct(
			'top-posts',
			/** This filter is documented in modules/widgets/facebook-likebox.php */
			apply_filters( 'jetpack_widget_name', __( 'Top Posts &amp; Pages', 'jetpack' ) ),
			array(
				'description'                 => __( 'Shows your most viewed posts and pages.', 'jetpack' ),
				'customize_selective_refresh' => true,
			)
		);

		$this->default_title = __( 'Top Posts &amp; Pages', 'jetpack' );

		if ( is_active_widget( false, false, $this->id_base ) || is_customize_preview() ) {
			add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_style' ) );
		}

		/**
		 * Add explanation about how the statistics are calculated.
		 *
		 * @module widgets
		 *
		 * @since 3.9.3
		 */
		add_action( 'jetpack_widget_top_posts_after_fields', array( $this, 'stats_explanation' ) );
	}

	/**
	 * Enqueue stylesheet.
	 */
	public function enqueue_style() {
		wp_register_style( 'jetpack-top-posts-widget', plugins_url( 'top-posts/style.css', __FILE__ ), array(), '20141013' );
		wp_enqueue_style( 'jetpack-top-posts-widget' );
	}

	/**
	 * Displays the form for this widget on the Widgets page of the WP Admin area.
	 *
	 * @param array $instance Instance configuration.
	 *
	 * @return void
	 */
	public function form( $instance ) {
		$instance = wp_parse_args( (array) $instance, $this->defaults() );

		if ( false === $instance['title'] ) {
			$instance['title'] = $this->default_title;
		}
		$title = stripslashes( $instance['title'] );

		$count = isset( $instance['count'] ) ? (int) $instance['count'] : 10;
		if ( $count < 1 || 10 < $count ) {
			$count = 10;
		}

		$allowed_post_types = array_values( get_post_types( array( 'public' => true ) ) );
		$types              = isset( $instance['types'] ) ? (array) $instance['types'] : array( 'post', 'page' );

		// 'likes' are not available in Jetpack
		$ordering = isset( $instance['ordering'] ) && 'likes' === $instance['ordering'] ? 'likes' : 'views';

		if ( isset( $instance['display'] ) && in_array( $instance['display'], array( 'grid', 'list', 'text' ), true ) ) {
			$display = $instance['display'];
		} else {
			$display = 'text';
		}

		?>

		<p>
			<label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php esc_html_e( 'Title:', 'jetpack' ); ?></label>
			<input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" />
		</p>

		<p>
			<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>
			<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" />
		</p>

		<?php if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) : ?>
		<p>
			<label><?php esc_html_e( 'Order Top Posts &amp; Pages By:', 'jetpack' ); ?></label>
			<ul>
				<li><label><input id="<?php echo esc_attr( $this->get_field_id( 'ordering' ) ); ?>-likes" name="<?php echo esc_attr( $this->get_field_name( 'ordering' ) ); ?>" type="radio" value="likes" <?php checked( 'likes', $ordering ); ?> /> <?php esc_html_e( 'Likes', 'jetpack' ); ?></label></li>
				<li><label><input id="<?php echo esc_attr( $this->get_field_id( 'ordering' ) ); ?>-views" name="<?php echo esc_attr( $this->get_field_name( 'ordering' ) ); ?>" type="radio" value="views" <?php checked( 'views', $ordering ); ?> /> <?php esc_html_e( 'Views', 'jetpack' ); ?></label></li>
			</ul>
		</p>
		<?php endif; ?>

		<p>
			<label for="<?php echo esc_attr( $this->get_field_id( 'types' ) ); ?>"><?php esc_html_e( 'Types of pages to display:', 'jetpack' ); ?></label>
			<ul>
				<?php
				foreach ( $allowed_post_types as $type ) {
					// Get the Post Type name to display next to the checkbox.
					$post_type_object = get_post_type_object( $type );
					$label            = $post_type_object->labels->name;

					$checked = '';
					if ( in_array( $type, $types, true ) ) {
						$checked = 'checked="checked" ';
					}
					?>

					<li><label>
						<input
							value="<?php echo esc_attr( $type ); ?>"
							name="<?php echo esc_attr( $this->get_field_name( 'types' ) ); ?>[]"
							id="<?php echo esc_attr( $this->get_field_id( 'types' ) . '-' . $type ); ?>"
							type="checkbox"
							<?php echo $checked; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
						>
						<?php echo esc_html( $label ); ?>
					</label></li>

				<?php } // End foreach ?>
			</ul>
		</p>

		<p>
			<label><?php esc_html_e( 'Display as:', 'jetpack' ); ?></label>
			<ul>
				<li><label><input id="<?php echo esc_attr( $this->get_field_id( 'display' ) ); ?>-text" name="<?php echo esc_attr( $this->get_field_name( 'display' ) ); ?>" type="radio" value="text" <?php checked( 'text', $display ); ?> /> <?php esc_html_e( 'Text List', 'jetpack' ); ?></label></li>
				<li><label><input id="<?php echo esc_attr( $this->get_field_id( 'display' ) ); ?>-list" name="<?php echo esc_attr( $this->get_field_name( 'display' ) ); ?>" type="radio" value="list" <?php checked( 'list', $display ); ?> /> <?php esc_html_e( 'Image List', 'jetpack' ); ?></label></li>
				<li><label><input id="<?php echo esc_attr( $this->get_field_id( 'display' ) ); ?>-grid" name="<?php echo esc_attr( $this->get_field_name( 'display' ) ); ?>" type="radio" value="grid" <?php checked( 'grid', $display ); ?> /> <?php esc_html_e( 'Image Grid', 'jetpack' ); ?></label></li>
			</ul>
		</p>
		<?php

		/**
		 * Fires after the fields are displayed in the Top Posts Widget settings in wp-admin.
		 *
		 * Allow adding extra content after the fields are displayed.
		 *
		 * @module widgets
		 *
		 * @since 3.9.3
		 *
		 * @param array $args {
		 *     @param array $instance The widget instance.
		 *     @param object $this The class object.
		 * }
		 */
		do_action( 'jetpack_widget_top_posts_after_fields', array( $instance, $this ) );
	}

	/**
	 * Explains how the statics are calculated.
	 */
	public function stats_explanation() {
		echo '<p>';
		esc_html_e( 'Top Posts &amp; Pages by views are calculated from 24-48 hours of stats. They take a while to change.', 'jetpack' );
		echo '</p>';
	}

	/**
	 * Deals with the settings when they are saved by the admin.
	 *
	 * @param array $new_instance New configuration values.
	 * @param array $old_instance Old configuration values.
	 *
	 * @return array
	 */
	public function update( $new_instance, $old_instance ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
		$instance          = array();
		$instance['title'] = wp_kses( $new_instance['title'], array() );
		if ( $instance['title'] === $this->default_title ) {
			$instance['title'] = false; // Store as false in case of language change.
		}

		$instance['count'] = (int) $new_instance['count'];
		if ( $instance['count'] < 1 || 10 < $instance['count'] ) {
			$instance['count'] = 10;
		}

		// 'likes' are not available in Jetpack
		$instance['ordering'] = isset( $new_instance['ordering'] ) && 'likes' === $new_instance['ordering']
			? 'likes'
			: 'views';

		$allowed_post_types = array_values( get_post_types( array( 'public' => true ) ) );
		$instance['types']  = $new_instance['types'];
		foreach ( $new_instance['types'] as $key => $type ) {
			if ( ! in_array( $type, $allowed_post_types, true ) ) {
				unset( $new_instance['types'][ $key ] );
			}
		}

		if ( isset( $new_instance['display'] ) && in_array( $new_instance['display'], array( 'grid', 'list', 'text' ), true ) ) {
			$instance['display'] = $new_instance['display'];
		} else {
			$instance['display'] = 'text';
		}

		/**
		 * Filters Top Posts Widget settings before they're saved.
		 *
		 * @module widgets
		 *
		 * @since 3.9.3
		 *
		 * @param array $instance The santized widget instance. Only contains data processed by the current widget.
		 * @param array $new_instance The new widget instance before sanitization.
		 */
		$instance = apply_filters( 'jetpack_top_posts_saving', $instance, $new_instance );

		return $instance;
	}

	/**
	 * Outputs the HTML for this widget.
	 *
	 * @param array $args     An array of standard parameters for widgets in this theme.
	 * @param array $instance An array of settings for this widget instance.
	 *
	 * @return void Echoes it's output
	 */
	public function widget( $args, $instance ) {
		/** This action is documented in modules/widgets/gravatar-profile.php */
		do_action( 'jetpack_stats_extra', 'widget_view', 'top_posts' );

		$instance = wp_parse_args( (array) $instance, $this->defaults() );

		$title = isset( $instance['title'] ) ? $instance['title'] : false;
		if ( false === $title ) {
			$title = $this->default_title;
		}

		/** This filter is documented in core/src/wp-includes/default-widgets.php */
		$title = apply_filters( 'widget_title', $title );

		$count = isset( $instance['count'] ) ? (int) $instance['count'] : false;
		if ( $count < 1 || 10 < $count ) {
			$count = 10;
		}
		/**
		 * Control the number of displayed posts.
		 *
		 * @module widgets
		 *
		 * @since 3.3.0
		 *
		 * @param string $count Number of Posts displayed in the Top Posts widget. Default is 10.
		 */
		$count = apply_filters( 'jetpack_top_posts_widget_count', $count );

		$types = isset( $instance['types'] ) ? (array) $instance['types'] : array( 'post', 'page' );

		// 'likes' are not available in Jetpack
		$ordering = isset( $instance['ordering'] ) && 'likes' === $instance['ordering']
			? 'likes'
			: 'views';

		if (
			isset( $instance['display'] )
			&& in_array( $instance['display'], array( 'grid', 'list', 'text' ), true )
		) {
			$display = $instance['display'];
		} else {
			$display = 'text';
		}

		$get_image_options = array();
		if ( 'text' !== $display ) {
			$get_image_options = array(
				'fallback_to_avatars' => true,
				/** This filter is documented in modules/stats.php */
				'gravatar_default'    => apply_filters( 'jetpack_static_url', set_url_scheme( 'https://en.wordpress.com/i/logo/white-gray-80.png' ) ),
				'avatar_size'         => 40,
				'width'               => null,
				'height'              => null,
			);
			if ( 'grid' === $display ) {
				$get_image_options['avatar_size'] = 200;
			}
			/**
			 * Top Posts Widget Image options.
			 *
			 * @module widgets
			 *
			 * @since 1.8.0
			 *
			 * @param array $get_image_options {
			 * Array of Image options.
			 * @type bool true Should we default to Gravatars when no image is found? Default is true.
			 * @type string $gravatar_default Default Image URL if no Gravatar is found.
			 * @type int $avatar_size Default Image size.
			 * @type mixed $width Image width, not set by default and $avatar_size is used instead.
			 * @type mixed $height Image height, not set by default and $avatar_size is used instead.
			 * }
			 */
			$get_image_options = apply_filters( 'jetpack_top_posts_widget_image_options', $get_image_options );
		}

		if ( function_exists( 'wpl_get_blogs_most_liked_posts' ) && 'likes' === $ordering ) {
			$posts = $this->get_by_likes( $count, $types );
		} else {
			$posts = $this->get_by_views( $count, $args, $types );
		}

		echo $args['before_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped

		if ( ! empty( $title ) ) {
			echo $args['before_title'] . $title . $args['after_title']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
		}

		/*
		 * If we have no posts, add some fallback posts
		 * and display a fallback message for admins.
		 */
		if ( ! $posts ) {
			if ( current_user_can( 'edit_theme_options' ) ) {
				echo $this->fallback_message(); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
			}

			$posts = $this->get_fallback_posts( $count, $types );
		}

		/*
		 * Display our posts.
		 */

		/**
		 * Filter the layout of the Top Posts Widget
		 *
		 * @module widgets
		 *
		 * @since 6.4.0
		 *
		 * @param string $layout layout of the Top Posts Widget (empty string).
		 * @param array $posts IDs of the posts to be displayed.
		 * @param array $display Display option from widget form.
		 */
		$layout = apply_filters( 'jetpack_top_posts_widget_layout', '', $posts, $display );
		if ( ! empty( $layout ) ) {
			echo $layout; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
		}

		switch ( $display ) {
			case 'list':
			case 'grid':
				// Keep the avatar_size as default dimensions for backward compatibility.
				$width  = (int) $get_image_options['avatar_size'];
				$height = (int) $get_image_options['avatar_size'];

				// Check if the user has changed the width.
				if ( ! empty( $get_image_options['width'] ) ) {
					$width = (int) $get_image_options['width'];
				}

				// Check if the user has changed the height.
				if ( ! empty( $get_image_options['height'] ) ) {
					$height = (int) $get_image_options['height'];
				}

				foreach ( $posts as &$post ) {
					$image         = Jetpack_PostImages::get_image(
						$post['post_id'],
						array(
							'fallback_to_avatars' => (bool) $get_image_options['fallback_to_avatars'],
							'width'               => (int) $width,
							'height'              => (int) $height,
							'avatar_size'         => (int) $get_image_options['avatar_size'],
						)
					);
					$post['image'] = $image['src'];
					if ( 'blavatar' !== $image['from'] && 'gravatar' !== $image['from'] ) {
						$post['image'] = jetpack_photon_url( $post['image'], array( 'resize' => "$width,$height" ) );
					}
				}
				unset( $post );

				if ( 'grid' === $display ) {
					echo "<div class='widgets-grid-layout no-grav'>\n";
					foreach ( $posts as $post ) {
						echo '<div class="widget-grid-view-image">';

						/**
						 * Fires before each Top Post result, inside <li>.
						 *
						 * @module widgets
						 *
						 * @since 3.2.0
						 *
						 * @param string $post['post_id'] Post ID.
						 */
						do_action( 'jetpack_widget_top_posts_before_post', $post['post_id'] );

						/**
						 * Filter the permalink of items in the Top Posts widget.
						 *
						 * @module widgets
						 *
						 * @since 4.4.0
						 *
						 * @param string $post['permalink'] Post permalink.
						 * @param array  $post              Post array.
						 */
						$filtered_permalink = apply_filters( 'jetpack_top_posts_widget_permalink', $post['permalink'], $post );

						printf(
							'<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>',
							esc_url( $filtered_permalink ),
							esc_attr( wp_kses( $post['title'], array() ) ),
							( get_queried_object_id() === $post['post_id'] ? ' aria-current="page"' : '' ),
							absint( $width ),
							absint( $height ),
							esc_url( $post['image'] )
						);

						/**
						 * Fires after each Top Post result, inside <li>.
						 *
						 * @module widgets
						 *
						 * @since 3.2.0
						 *
						 * @param string $post['post_id'] Post ID.
						 */
						do_action( 'jetpack_widget_top_posts_after_post', $post['post_id'] );

						echo '</div>';
					}
					echo "</div>\n";
				} else {
					echo "<ul class='widgets-list-layout no-grav'>\n";
					foreach ( $posts as $post ) {
						echo '<li>';

						/** This action is documented in modules/widgets/top-posts.php */
						do_action( 'jetpack_widget_top_posts_before_post', $post['post_id'] );

						/** This filter is documented in modules/widgets/top-posts.php */
						$filtered_permalink = apply_filters( 'jetpack_top_posts_widget_permalink', $post['permalink'], $post );

						printf(
							'<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>
							<div class="widgets-list-layout-links">
								<a href="%1$s" title="%2$s" class="bump-view" data-bump-view="tp"%3$s>%7$s</a>
							</div>
							',
							esc_url( $filtered_permalink ),
							esc_attr( wp_kses( $post['title'], array() ) ),
							( get_queried_object_id() === $post['post_id'] ? ' aria-current="page"' : '' ),
							absint( $width ),
							absint( $height ),
							esc_url( $post['image'] ),
							esc_html( wp_kses( $post['title'], array() ) )
						);

						/** This action is documented in modules/widgets/top-posts.php */
						do_action( 'jetpack_widget_top_posts_after_post', $post['post_id'] );

						echo '</li>';
					}
					echo "</ul>\n";
				}
				break;
			default:
				echo '<ul>';

				foreach ( $posts as $post ) {
					echo '<li>';

					/** This action is documented in modules/widgets/top-posts.php */
					do_action( 'jetpack_widget_top_posts_before_post', $post['post_id'] );

					/** This filter is documented in modules/widgets/top-posts.php */
					$filtered_permalink = apply_filters( 'jetpack_top_posts_widget_permalink', $post['permalink'], $post );

					printf(
						'<a href="%1$s" class="bump-view" data-bump-view="tp"%2$s>%3$s</a>',
						esc_url( $filtered_permalink ),
						( get_queried_object_id() === $post['post_id'] ? ' aria-current="page"' : '' ),
						esc_html( wp_kses( $post['title'], array() ) )
					);

					/** This action is documented in modules/widgets/top-posts.php */
					do_action( 'jetpack_widget_top_posts_after_post', $post['post_id'] );

					echo '</li>';
				}

				echo '</ul>';
				break;
		}

		echo $args['after_widget']; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
	}

	/**
	 * Display a message with recommendations when there are no recorded top posts.
	 *
	 * @return string $fallback_message
	 */
	private static function fallback_message() {
		$link = esc_url( Redirect::get_url( 'jetpack-support-getting-more-views-and-traffic' ) );
		if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
			$link = 'https://en.support.wordpress.com/getting-more-site-traffic/';
		}

		$fallback_message  = '<p>';
		$fallback_message .= sprintf(
			wp_kses(
				/* Translators: Placeholder: link to the Jetpack support article. */
				__( 'There are no popular posts to display. Instead, your visitors will see a list of your recent posts below. <a href="%s" target="_blank">Want more traffic?</a>', 'jetpack' ),
				array(
					'a' => array(
						'href'   => array(),
						'target' => array(),
					),
				)
			),
			esc_url( $link )
		);
		$fallback_message .= '<p>';

		return $fallback_message;
	}

	/**
	 * Widget default option values.
	 */
	public static function defaults() {
		return array(
			'title'    => esc_html__( 'Top Posts &amp; Pages', 'jetpack' ),
			'count'    => absint( 10 ),
			'types'    => array( 'post', 'page' ),
			'ordering' => 'views',
			'display'  => 'text',
		);
	}

	/**
	 * Get most liked posts
	 *
	 * ONLY TO BE USED IN WPCOM
	 *
	 * @since 8.4.0 Added $types param
	 *
	 * @param int   $count The maximum number of posts to be returned.
	 * @param array $types The post types that should be returned. Optional. Defaults to 'post' and 'page'.
	 *
	 * @return array array of posts.
	 */
	public function get_by_likes( $count, $types = array( 'post', 'page' ) ) {
		$post_likes = wpl_get_blogs_most_liked_posts();
		if ( ! $post_likes ) {
			return array();
		}

		return $this->get_posts( array_keys( $post_likes ), $count, $types );
	}

	/**
	 * Get the top posts based on views
	 *
	 * @since 8.4.0 Added $types param
	 *
	 * @param int   $count The maximum number of posts to be returned.
	 * @param array $args The widget arguments.
	 * @param array $types The post types that should be returned.
	 *
	 * @return array array of posts. Defaults to 'post' and 'page'.
	 */
	public function get_by_views( $count, $args, $types = array( 'post', 'page' ) ) {
		if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
			$post_views = wp_cache_get( "get_top_posts_$count", 'stats' );
			if ( false === $post_views ) {
				$post_views = array_shift(
					stats_get_daily_history(
						false,
						get_current_blog_id(),
						'postviews',
						'post_id',
						false,
						2,
						'',
						$count * 2 + 10,
						true
					)
				);
				unset( $post_views[0] );
				wp_cache_add( "get_top_posts_$count", $post_views, 'stats', 1200 );
			}

			return $this->get_posts( array_keys( $post_views ), $count, $types );
		}

		/**
		 * Filter the number of days used to calculate Top Posts for the Top Posts widget.
		 * We do not recommend accessing more than 10 days of results at one.
		 * When more than 10 days of results are accessed at once, results should be cached via the WordPress transients API.
		 * Querying for -1 days will give results for an infinite number of days.
		 *
		 * @module widgets
		 *
		 * @since 3.9.3
		 *
		 * @param int 2 Number of days. Default is 2.
		 * @param array $args The widget arguments.
		 */
		$days = (int) apply_filters( 'jetpack_top_posts_days', 2, $args );

		/** Handling situations where the number of days makes no sense - allows for unlimited days where $days = -1 */
		if ( 0 === $days || false === $days ) {
			$days = 2;
		}

		$post_view_posts = stats_get_from_restapi( array(), 'top-posts?max=11&summarize=1&num=' . (int) $days );

		if ( ! isset( $post_view_posts->summary ) || empty( $post_view_posts->summary->postviews ) ) {
			return array();
		}

		$post_view_ids = array_filter( wp_list_pluck( $post_view_posts->summary->postviews, 'id' ) );

		if ( ! $post_view_ids ) {
			return array();
		}

		return $this->get_posts( $post_view_ids, $count, $types );
	}

	/**
	 * Get some posts if no posts are found in the stats API
	 *
	 * @since 8.4.0 Added $count and $types
	 *
	 * @param int   $count The maximum number of posts to be returned.
	 * @param array $types The post types that should be returned.
	 * @return array
	 */
	public function get_fallback_posts( $count = 10, $types = array( 'post', 'page' ) ) {
		$post_query = new WP_Query();

		if ( ! is_array( $types ) || empty( $types ) ) {
			$types = array( 'post', 'page' );
		}

		$posts = $post_query->query(
			array(
				'posts_per_page' => $count,
				'post_status'    => 'publish',
				'post_type'      => $types,
				'no_found_rows'  => true,
				'fields'         => 'ids',
			)
		);

		if ( ! $posts ) {
			return array();
		}

		return $this->get_posts( $posts, $count, $types );
	}

	/**
	 * Get posts from an array of IDs
	 *
	 * @since 8.4.0 Added $types parameters
	 *
	 * @param array $post_ids The post IDs.
	 * @param int   $count The maximum number of posts to return.
	 * @param array $types The post types that should be returned. Optional. Defaults to 'post', 'page'.
	 * @return array
	 */
	public function get_posts( $post_ids, $count, $types = array( 'post', 'page' ) ) {
		$counter = 0;

		if ( ! is_array( $types ) || empty( $types ) ) {
			$types = array( 'post', 'page' );
		}

		$posts = array();
		foreach ( (array) $post_ids as $post_id ) {
			$post = get_post( $post_id );

			if ( ! $post ) {
				continue;
			}

			/**
			 * Attachment pages use the 'inherit' post status by default.
			 * To be able to remove attachment pages from private and password protect posts,
			 * we need to replace their post status by the parent post' status.
			 */
			if ( 'inherit' === $post->post_status && 'attachment' === $post->post_type ) {
				$post->post_status = get_post_status( $post_id );
			}

			// hide private and password protected posts.
			if ( 'publish' !== $post->post_status || ! empty( $post->post_password ) ) {
				continue;
			}

			// Filter by chosen Post Types.
			if ( ! in_array( $post->post_type, $types, true ) ) {
				continue;
			}

			// Both get HTML stripped etc on display.
			if ( empty( $post->post_title ) ) {
				$title_source = $post->post_content;
				$title        = wp_html_excerpt( $title_source, 50 );
				$title       .= '&hellip;';
			} else {
				$title = $post->post_title;
			}

			$permalink = get_permalink( $post->ID );

			$post_type = $post->post_type;

			$posts[] = compact( 'title', 'permalink', 'post_id', 'post_type' );
			$counter++;

			if ( $counter == $count ) { // phpcs:ignore Universal.Operators.StrictComparisons.LooseEqual
				break; // only need to load and show x number of likes.
			}
		}

		/**
		 * Filter the Top Posts and Pages.
		 *
		 * @module widgets
		 *
		 * @since 3.0.0
		 *
		 * @param array $posts Array of the most popular posts.
		 * @param array $post_ids Array of Post IDs.
		 * @param string $count Number of Top Posts we want to display.
		 */
		return apply_filters( 'jetpack_widget_get_top_posts', $posts, $post_ids, $count );
	}
}

/**
 * Create a shortcode to display the widget anywhere.
 *
 * @since 3.9.2
 *
 * @param array $instance Saved values from database.
 */
function jetpack_do_top_posts_widget( $instance ) {
	// Post Types can't be entered as an array in the shortcode parameters.
	if ( isset( $instance['types'] ) && is_array( $instance['types'] ) ) {
		$instance['types'] = implode( ',', $instance['types'] );
	}

	$instance = shortcode_atts(
		Jetpack_Top_Posts_Widget::defaults(),
		$instance,
		'jetpack_top_posts_widget'
	);

	// Add a class to allow styling.
	$args = array(
		'before_widget' => sprintf( '<div class="%s">', 'jetpack_top_posts_widget' ),
	);

	ob_start();
	the_widget( 'Jetpack_Top_Posts_Widget', $instance, $args );
	$output = ob_get_clean();

	return $output;
}
add_shortcode( 'jetpack_top_posts_widget', 'jetpack_do_top_posts_widget' );