Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 69 additions & 24 deletions modules/widgets/top-posts.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -38,7 +45,11 @@ function __construct() {
)
);

$this->default_title = __( 'Top Posts & Pages', 'jetpack' );
$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' ) );
Expand All @@ -51,22 +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';
}

$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
?>

<p>
Expand All @@ -88,6 +85,20 @@ function form( $instance ) {
</ul>
</p>

<p>
<label><?php esc_html_e( 'Types of Pages to display:', 'jetpack' ); ?></label>
<ul>
<?php
$types = $this->post_types;
foreach ( $types as $type ) :
$post_type_object = get_post_type_object( $type );
$label = $post_type_object->labels->name;
?>
<li><label><input type="checkbox"<?php checked( in_array( $type, $instance['type'] ) ); ?> id="<?php echo $this->get_field_id( $type ); ?>" name="<?php echo $this->get_field_name( 'type' ); ?>[]" value="<?php echo esc_attr( $type ); ?>"/> <?php echo esc_html( $label ); ?></label></li>
<?php endforeach; ?>
</ul>
</p>

<p><?php esc_html_e( 'Top Posts &amp; Pages by views are calculated from 24-48 hours of stats. They take a while to change.', 'jetpack' ); ?></p>

<?php
Expand All @@ -111,6 +122,21 @@ function update( $new_instance, $old_instance ) {
$instance['display'] = 'text';
}

/*
* There MUST be at least one post type set. If there is not then
* lets select ALL the post type. Ha! :P
*/
$instance['type'] = array();
if ( isset( $new_instance['type'] ) ) {
foreach ( $new_instance['type'] as $type ) {
if ( in_array( $type, $this->post_types ) ) {
$instance['type'][] = $type;
}
}
} else {
$instance['type'] = $this->post_types;
}

return $instance;
}

Expand Down Expand Up @@ -148,7 +174,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();
Expand Down Expand Up @@ -220,7 +253,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 ) {
Expand All @@ -241,20 +274,26 @@ 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() {
if ( current_user_can( 'edit_theme_options' ) ) {
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,
) );

Expand All @@ -267,18 +306,24 @@ 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 );

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
Expand Down