From 3aaee7e361acf224c8ea79b5cacfc8094588f828 Mon Sep 17 00:00:00 2001 From: Jeremy Herve Date: Mon, 6 Jan 2014 15:20:28 +0000 Subject: [PATCH 1/2] Top Posts: add an option to choose what Post Types to display This is only a first pass, trying to support CPTs as well I've also removed the code that excluded attachment pages, since people would now be able to exclude them in the widget options. Original trac ticket: http://plugins.trac.wordpress.org/ticket/1797 --- modules/widgets/top-posts.php | 66 +++++++++++++++++++++++++++++++---- 1 file changed, 59 insertions(+), 7 deletions(-) diff --git a/modules/widgets/top-posts.php b/modules/widgets/top-posts.php index ba288df34949..cb59ec80f674 100644 --- a/modules/widgets/top-posts.php +++ b/modules/widgets/top-posts.php @@ -39,6 +39,7 @@ function __construct() { ); $this->default_title = __( 'Top Posts & Pages', 'jetpack' ); + $this->post_types = array_values( get_post_types( array( 'public' => true ) ) ); if ( is_active_widget( false, false, $this->id_base ) ) { add_action( 'wp_print_styles', array( $this, 'enqueue_style' ) ); @@ -67,6 +68,12 @@ function form( $instance ) { $display = 'text'; } + if ( isset( $instance['type'] ) && in_array( $instance['type'], $this->post_types ) ) { + $type = $instance['type']; + } else { + $type = $this->post_types; + } + ?>

@@ -88,6 +95,21 @@ function form( $instance ) {

+

+ +

+

+

post_types ) ) { + $instance['type'][] = $type; + } + } + } else { + $instance['type'] = $this->post_types; + } + return $instance; } @@ -148,7 +181,14 @@ function widget( $args, $instance ) { $get_image_options = apply_filters( 'jetpack_top_posts_widget_image_options', $get_image_options ); } - $posts = $this->get_by_views( $count ); + if ( isset( $instance['type'] ) && in_array( $instance['type'], $this->post_types ) ) { + $type = $instance['type']; + } else { + $type = $this->post_types; + } + + + $posts = $this->get_by_views( $count, $type ); if ( !$posts ) { $posts = $this->get_fallback_posts(); @@ -220,7 +260,7 @@ function widget( $args, $instance ) { echo $args['after_widget']; } - function get_by_views( $count ) { + function get_by_views( $count, $type ) { $days = (int) apply_filters( 'jetpack_top_posts_days', 2 ); if ( $days < 1 ) { @@ -241,7 +281,7 @@ function get_by_views( $count ) { return array(); } - return $this->get_posts( $post_view_ids, $count ); + return $this->get_posts( $post_view_ids, $count, $type ); } function get_fallback_posts() { @@ -249,12 +289,18 @@ function get_fallback_posts() { return array(); } + if ( isset( $instance['type'] ) && in_array( $instance['type'], $this->post_types ) ) { + $type = $instance['type']; + } else { + $type = $this->post_types; + } + $post_query = new WP_Query; $posts = $post_query->query( array( 'posts_per_page' => 1, 'post_status' => 'publish', - 'post_type' => array( 'post', 'page' ), + 'post_type' => $type, 'no_found_rows' => true, ) ); @@ -267,9 +313,15 @@ function get_fallback_posts() { return $this->get_posts( $post->ID, 1 ); } - function get_posts( $post_ids, $count ) { + function get_posts( $post_ids, $count, $type ) { $counter = 0; + if ( isset( $instance['type'] ) && in_array( $instance['type'], $this->post_types ) ) { + $type = $instance['type']; + } else { + $type = $this->post_types; + } + $posts = array(); foreach ( (array) $post_ids as $post_id ) { $post = get_post( $post_id ); @@ -277,8 +329,8 @@ function get_posts( $post_ids, $count ) { if ( !$post ) continue; - // Only posts and pages, no attachments - if ( 'attachment' == $post->post_type ) + // Only the post types we've selected in the widget options + if ( !in_array( $post->post_type, $type ) ) continue; // hide private and password protected posts From 3484fe2f229b4210bc2bda368e35c7786aa7fced Mon Sep 17 00:00:00 2001 From: blobaugh Date: Thu, 9 Jan 2014 23:11:25 -0800 Subject: [PATCH 2/2] Jetpack: Widget: Top Posts: Added defaults to form values --- modules/widgets/top-posts.php | 45 +++++++++++++++-------------------- 1 file changed, 19 insertions(+), 26 deletions(-) diff --git a/modules/widgets/top-posts.php b/modules/widgets/top-posts.php index cb59ec80f674..85c95570b673 100644 --- a/modules/widgets/top-posts.php +++ b/modules/widgets/top-posts.php @@ -29,6 +29,13 @@ class Jetpack_Top_Posts_Widget extends WP_Widget { var $alt_option_name = 'widget_stats_topposts'; var $default_title = ''; + private $default_settings = array( + 'title' => '', + 'count' => 10, + 'display' => 'text', + 'type' => array() + ); + function __construct() { parent::__construct( 'top-posts', @@ -38,8 +45,11 @@ function __construct() { ) ); - $this->default_title = __( 'Top Posts & Pages', 'jetpack' ); - $this->post_types = array_values( get_post_types( array( 'public' => true ) ) ); + $this->default_title = __( 'Top Posts & Pages', 'jetpack' );// Refactor to remove this line + $this->default_settings['title'] = $this->default_title; + + $this->post_types = array_values( get_post_types( array( 'public' => true ) ) );// Refactor to remove this line + $this->default_settings['type'] = $this->post_types; if ( is_active_widget( false, false, $this->id_base ) ) { add_action( 'wp_print_styles', array( $this, 'enqueue_style' ) ); @@ -52,28 +62,8 @@ function enqueue_style() { } function form( $instance ) { - $title = isset( $instance['title' ] ) ? $instance['title'] : false; - if ( false === $title ) { - $title = $this->default_title; - } - - $count = isset( $instance['count'] ) ? (int) $instance['count'] : 10; - if ( $count < 1 || 10 < $count ) { - $count = 10; - } - - if ( isset( $instance['display'] ) && in_array( $instance['display'], array( 'grid', 'list', 'text' ) ) ) { - $display = $instance['display']; - } else { - $display = 'text'; - } - - if ( isset( $instance['type'] ) && in_array( $instance['type'], $this->post_types ) ) { - $type = $instance['type']; - } else { - $type = $this->post_types; - } - + $instance = wp_parse_args( $instance, $this->default_settings ); + extract( $instance ); // Bad idea! Limit the amount of black magic by updating below to use $instance instead ?>

@@ -100,7 +90,6 @@ function form( $instance ) {