';
}
echo $args['after_widget'];
}
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
*/
function get_by_likes( $count ) {
$post_likes = wpl_get_blogs_most_liked_posts();
if ( ! $post_likes ) {
return array();
}
return $this->get_posts( array_keys( $post_likes ), $count );
}
function get_by_views( $count, $args ) {
if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
global $wpdb;
$post_views = wp_cache_get( "get_top_posts_$count", 'stats' );
if ( false === $post_views ) {
$post_views = array_shift( stats_get_daily_history( false, get_current_blog_id(), 'postviews', 'post_id', false, 2, '', $count * 2 + 10, true ) );
unset( $post_views[0] );
wp_cache_add( "get_top_posts_$count", $post_views, 'stats', 1200 );
}
return $this->get_posts( array_keys( $post_views ), $count );
}
/**
* 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;
}
$post_view_posts = stats_get_from_restapi( array(), 'top-posts?max=11&summarize=1&num=' . intval( $days ) );
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 );
}
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', 'page' ),
'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;
}
/**
* 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;
}
// 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 ) {
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
*/
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_array( $instance['types'] ) ) {
$instance['types'] = implode( ',', $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( '