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 ) && ( ! Jetpack::is_module_active( 'stats' ) || ( new Status() )->is_offline_mode() ) ) { 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 & Pages', 'jetpack' ) ), array( 'description' => __( 'Shows your most viewed posts and pages.', 'jetpack' ), 'customize_selective_refresh' => true, 'show_instance_in_rest' => true, ) ); $this->default_title = __( 'Top Posts & Pages', 'jetpack' ); /** * 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' ) ); add_filter( 'widget_types_to_hide_from_legacy_widget_block', array( $this, 'hide_widget_in_block_editor' ) ); } /** * Remove the "Top Posts and Pages" widget from the Legacy Widget block * * @param array $widget_types List of widgets that are currently removed from the Legacy Widget block. * @return array $widget_types New list of widgets that will be removed. */ public function hide_widget_in_block_editor( $widget_types ) { // @TODO: Hide for Simple sites when the block API starts working. if ( ! ( new Host() )->is_wpcom_simple() ) { $widget_types[] = 'top-posts'; } return $widget_types; } /** * 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, static::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'; } ?>
'; $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. Want more traffic?', 'jetpack' ), array( 'a' => array( 'href' => array(), 'target' => array(), ), ) ), esc_url( $link ) ); $fallback_message .= '
'; return $fallback_message; } /** * Widget default option values. */ public static function defaults() { return array( 'title' => esc_html__( 'Top Posts & 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 ) { $stats_get_daily_history = stats_get_daily_history( false, get_current_blog_id(), 'postviews', 'post_id', false, 2, '', $count * 2 + 10, true ); $post_views = array_shift( $stats_get_daily_history ); 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; } $query_args = array( 'max' => 11, 'summarize' => 1, 'num' => (int) $days, ); $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 ) ) { 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 .= '…'; } 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_string( $instance['types'] ) ) { $instance['types'] = explode( ',', $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( '