Widgets */ add_action( 'widgets_init', 'jetpack_top_posts_widget_init' ); 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' ); } class Jetpack_Top_Posts_Widget extends WP_Widget { public $alt_option_name = 'widget_stats_topposts'; public $default_title = ''; 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, ) ); $this->default_title = __( 'Top Posts & 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' ) ); } 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' ); } 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' ) ) ) { $display = $instance['display']; } else { $display = 'text'; } ?>

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 ) ) { unset( $new_instance['types'][ $key ] ); } } if ( isset( $new_instance['display'] ) && in_array( $new_instance['display'], array( 'grid', 'list', 'text' ) ) ) { $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; } 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' ) ) ) { $display = $instance['display']; } else { $display = 'text'; } 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 ); } else { $posts = $this->get_by_views( $count, $args ); } // Filter the returned posts. Remove all posts that do not match the chosen Post Types. if ( isset( $types ) ) { foreach ( $posts as $k => $post ) { if ( ! in_array( $post['post_type'], $types ) ) { unset( $posts[ $k ] ); } } } if ( ! $posts ) { $posts = $this->get_fallback_posts(); } echo $args['before_widget']; if ( ! empty( $title ) ) { echo $args['before_title'] . $title . $args['after_title']; } if ( ! $posts ) { $link = 'https://jetpack.com/support/getting-more-views-and-traffic/'; if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) { $link = 'http://en.support.wordpress.com/getting-more-site-traffic/'; } if ( current_user_can( 'edit_theme_options' ) ) { echo '

' . sprintf( __( 'There are no posts to display. Want more traffic?', 'jetpack' ), esc_url( $link ) ) . '

'; } echo $args['after_widget']; return; } /** * 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; echo $args['after_widget']; return; } 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' => true, '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 "
\n"; foreach ( $posts as $post ) : ?>
. * * @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 ); ?> <?php echo esc_attr( wp_kses( $post['title'], array() ) ); ?> . * * @module widgets * * @since 3.2.0 * * @param string $post['post_id'] Post ID. */ do_action( 'jetpack_widget_top_posts_after_post', $post['post_id'] ); ?>
\n"; } else { echo "