esc_url( $post['image'] ),
esc_attr( $post['image_srcset'] )
* Fires after each Top Post result, inside <li>.
* @param string $post['post_id'] Post ID.
do_action( 'jetpack_widget_top_posts_after_post', $post['post_id'] );
echo "<ul class='widgets-list-layout no-grav'>\n";
foreach ( $posts as $post ) {
/** 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 );
'<a href="%1$s" title="%2$s" class="bump-view" data-bump-view="tp"%3$s><img loading="lazy" width="%4$d" height="%5$d" src="%6$s" srcset="%7$s" alt="%2$s" data-pin-nopin="true" class="widgets-list-layout-blavatar" /></a>',
esc_url( $filtered_permalink ),
esc_attr( wp_kses( $post['title'], array() ) ),
( get_queried_object_id() === $post['post_id'] ? ' aria-current="page"' : '' ),
esc_url( $post['image'] ),
esc_attr( $post['image_srcset'] )
'<div class="widgets-list-layout-links">
<a href="%1$s" title="%2$s" class="bump-view" data-bump-view="tp"%3$s>%4$s</a>
esc_url( $filtered_permalink ),
esc_attr( wp_kses( $post['title'], array() ) ),
( 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'] );
foreach ( $posts as $post ) {
/** 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 );
'<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 $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(
/* 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' ),
$fallback_message .= '<p>';
return $fallback_message;
* Widget default option values.
public static function defaults() {
'title' => esc_html__( 'Top Posts & Pages', 'jetpack' ),
'types' => array( 'post', 'page' ),
* 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();
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 ) {
$stats_get_daily_history = stats_get_daily_history(
$post_views = array_shift( $stats_get_daily_history );
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.
* @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 */
$wpcom_stats = new WPCOM_Stats();
$post_view_posts = $wpcom_stats->convert_stats_array_to_object(
$wpcom_stats->get_top_posts( $query_args )
if ( ! isset( $post_view_posts->summary ) || empty( $post_view_posts->summary->postviews ) ) {
$post_view_ids = array_filter( wp_list_pluck( $post_view_posts->summary->postviews, 'id' ) );
if ( ! $post_view_ids ) {
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.
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(
'posts_per_page' => $count,
'post_status' => 'publish',
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'.
public function get_posts( $post_ids, $count, $types = array( 'post', 'page' ) ) {
if ( ! is_array( $types ) || empty( $types ) ) {
$types = array( 'post', 'page' );
foreach ( (array) $post_ids as $post_id ) {
$post = get_post( $post_id );
* 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 ) ) {
// Filter by chosen Post Types.
if ( ! in_array( $post->post_type, $types, true ) ) {
// 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 = $post->post_title;
$permalink = get_permalink( $post->ID );
$post_type = $post->post_type;
$posts[] = compact( 'title', 'permalink', 'post_id', 'post_type' );
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.
* @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.
* @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_string( $instance['types'] ) ) {
$instance['types'] = explode( ',', $instance['types'] );
$instance = shortcode_atts(
Jetpack_Top_Posts_Widget::defaults(),
'jetpack_top_posts_widget'
// Add a class to allow styling.
'before_widget' => sprintf( '<div class="%s">', 'jetpack_top_posts_widget' ),
the_widget( 'Jetpack_Top_Posts_Widget', $instance, $args );
$output = ob_get_clean();
add_shortcode( 'jetpack_top_posts_widget', 'jetpack_do_top_posts_widget' );