Widgets */ add_action( 'widgets_init', 'chained_popular_posts_widget_init' ); function chained_popular_posts_widget_init() { // Currently, this widget depends on the Stats Module if ( !function_exists( 'stats_get_csv' ) ) { return; } register_widget( 'Chained_Popular_Posts_Widget' ); } endif; if ( ! class_exists('Chained_Popular_Posts_Widget') ) : class Chained_Popular_Posts_Widget extends WP_Widget { var $alt_option_name = 'widget_chained_popularposts'; var $default_title = ''; function __construct() { parent::__construct( 'chained-popular-posts', apply_filters( 'chained_widget_name', esc_html__( 'Chained Popular Posts', 'chained' ) ), array( 'description' => esc_html__( 'Shows your most viewed posts in an elegant way!', 'chained' ), ) ); $this->default_title = esc_html__( 'Chained Popular Posts', 'chained' ); } function form( $instance ) { $title = isset( $instance['title'] ) ? $instance['title'] : false; if ( false === $title ) { $title = $this->default_title; } $count = isset( $instance['count'] ) ? (int) $instance['count'] : 5; if ( $count < 1 || 10 < $count ) { $count = 10; } ?>

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; } return $instance; } function widget( $args, $instance ) { $title = isset( $instance['title'] ) ? $instance['title'] : false; if ( false === $title ) { $title = $this->default_title; } $title = apply_filters( 'widget_title', $title ); $count = isset( $instance['count'] ) ? (int) $instance['count'] : 5; if ( $count < 1 || 10 < $count ) { $count = 10; } $count = apply_filters( 'chained_popular_posts_widget_count', $count ); $get_image_options = array( 'fallback_to_avatars' => false, 'thumbnail_size' => 285, ); $get_image_options = apply_filters( 'chained_popular_posts_widget_image_options', $get_image_options ); $posts = $this->get_by_views( $count ); if ( ! $posts ) { $posts = $this->get_fallback_posts(); } echo wp_kses_post($args['before_widget']); if ( ! empty( $title ) ) { echo wp_kses_post($args['before_title']) . wp_kses_post($title) . wp_kses_post($args['after_title']); } if ( ! $posts ) { if ( current_user_can( 'edit_theme_options' ) ) { echo '

' . sprintf( /* translators: No posts to display */ esc_html__( 'There are no posts to display. Want more traffic?', 'chained' ), 'http://en.support.wordpress.com/getting-more-site-traffic/' ) . '

'; } echo wp_kses_post($args['after_widget']); return; } //the first post has a nice image unlike the rest $post = array_shift( $posts ); $image = Jetpack_PostImages::get_image( $post['post_id'], array( 'fallback_to_avatars' => true ) ); $post['image'] = $image['src']; if ( 'blavatar' != $image['from'] && 'gravatar' != $image['from'] ) { $size = (int) $get_image_options['thumbnail_size']; $post['image'] = jetpack_photon_url( $post['image'], array( 'resize' => "$size" ) ); } echo '
    '; ?> '; echo wp_kses_post($args['after_widget']); } function get_by_views( $count ) { $days = (int) apply_filters( 'chained_jetpack_top_posts_days', 2 ); if ( $days < 1 ) { $days = 2; } if ( $days > 10 ) { $days = 10; } $post_view_posts = stats_get_csv( 'postviews', array( 'days' => absint( $days ), 'limit' => 11 ) ); if ( ! $post_view_posts ) { return array(); } $post_view_ids = array_filter( wp_list_pluck( $post_view_posts, 'post_id' ) ); if ( ! $post_view_ids ) { return array(); } return $this->get_posts( $post_view_ids, $count ); } function get_fallback_posts() { if ( current_user_can( 'edit_theme_options' ) ) { return array(); } $post_query = new WP_Query; $posts = $post_query->query( array( 'posts_per_page' => 1, 'post_status' => 'publish', 'post_type' => array( 'post' ), 'no_found_rows' => true, ) ); if ( ! $posts ) { return array(); } $post = array_pop( $posts ); return $this->get_posts( $post->ID, 1 ); } function get_posts( $post_ids, $count ) { $counter = 0; $posts = array(); foreach ( (array) $post_ids as $post_id ) { $post = get_post( $post_id ); if ( ! $post ) { continue; } // Only posts, no attachments or pages if ( 'attachment' == $post->post_type || 'page' == $post->post_type ) { continue; } // hide private and password protected posts if ( 'publish' != $post->post_status || ! empty( $post->post_password ) || empty( $post->ID ) ) { 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 ); $posts[] = compact( 'title', 'permalink', 'post_id' ); $counter++; if ( $counter == $count ) { break; // only need to load and show x number of posts } } return apply_filters( 'chained_widget_get_popular_posts', $posts, $post_ids, $count ); } } //Class chained Popular Posts Widget endif; ?>