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.
      *
      * @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);
     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);
 }
Пример #2
0
 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_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);
 }
/**
 * Gets the data used by the "Top Posts" widget.
 * 
 * Our Top Posts widget (http://en.support.wordpress.com/widgets/top-posts-widget/) uses a display_top_posts() function to display a list of popular posts.
 * You can use this function in your themes. The function uses data from WordPress.com Stats (http://en.support.wordpress.com/stats/) to generate the list.
 *
 * If you would like more control over the output of display_top_posts(), use the get_top_posts() function.
 *
 * Note: in the results, post_ID = 0 is used to track home page views.
 * 
 * @param int $number Optional. At least 10 posts are always returned; this parameter controls how many extra you want. Valid values: 1-10 (default is 10).
 * @param int $days Optional. How many days of stats should be used in the calculation; defaults to 2.
 * @return array
 */
function get_top_posts($number = 10, $days = 2)
{
    global $wpdb;
    // Compat for .org
    if (!function_exists('stats_get_daily_history')) {
        return array();
    }
    // TODO: return dummy data
    $top_posts = wp_cache_get("get_top_posts_{$number}_{$days}");
    if (!$top_posts) {
        if ($number < 1 || $number > 20 || !is_int($number)) {
            $number = 10;
        }
        if ($days < 2 || !is_int($days)) {
            $days = 2;
        }
        // minimum is 2 because of how stats rollover for a new day
        $top_posts = array_shift(stats_get_daily_history(false, $wpdb->blogid, 'postviews', 'post_id', false, $days, '', $number + 10, true));
        wp_cache_add("get_top_posts_{$number}_{$days}", $top_posts, '', 1200);
    }
    return $top_posts;
}
/**
 * Get the number of pageviews for a given post ID.
 * 
 * Default to the current post.
 *
 * @param int $post_id Optional. The post ID to fetch stats for. Defaults to the $post global's value.
 * @param int $num_days Optional. How many days to go back to include in the stats. Default is 1. Maximum 90 days.
 * @param string $end_data Optional. The last day of the desired time frame. Format is 'Y-m-d' (e.g. 2007-05-01) and default is today's UTC date.
 * @return int|false Number of pageviews or false on error.
 */
function wpcom_vip_get_post_pageviews($post_id = null, $num_days = 1, $end_date = false)
{
    global $post, $wpdb;
    if (empty($post_id) && !empty($post->ID)) {
        $post_id = $post->ID;
    }
    $post_id = absint($post_id);
    if (empty($post_id)) {
        return false;
    }
    // At least 1 but no more than 90
    $num_days = max(1, min(90, intval($num_days)));
    if (true === WPCOM_IS_VIP_ENV) {
        $cache_key = 'views_' . $wpdb->blogid . '_' . $post_id . '_' . $num_days . '_' . $end_date;
        $views = wp_cache_get($cache_key, 'vip_stats');
        if (false === $views) {
            $views = 0;
            $data = stats_get_daily_history(false, $wpdb->blogid, 'postviews', 'post_id', $end_date, $num_days, $wpdb->prepare("AND post_id = %d", $post_id), 1, true);
            if (is_array($data)) {
                $views = (int) array_pop(array_pop($data));
            }
            wp_cache_set($cache_key, $views, 'vip_stats', 3600);
        }
    } else {
        $views = mt_rand(0, 20000);
    }
    return $views;
}
function mostpopular_adjust_title($title = '')
{
    if (!function_exists('stats_get_daily_history')) {
        die('Call to undefined function stats_get_daily_history().');
    }
    global $post, $wpdb, $mostpopular_duration;
    $mostpopular_duration = apply_filters("mostpopular_max_duration", $mostpopular_duration);
    $tpcacheid = md5("topposts_" . $mostpopular_duration . $wpdb->blogid);
    $topposts = wp_cache_get($tpcacheid, 'output');
    // just in the case we do not have a cache hit fill it
    if (empty($topposts)) {
        $topposts = array_shift(stats_get_daily_history(false, $wpdb->blogid, 'postviews', 'post_id', false, $mostpopular_duration, '', 100, true));
        if (!empty($topposts)) {
            wp_cache_add($tpcacheid, $topposts, 'output', 3600);
        }
    }
    $title_addon = '';
    if (isset($topposts[$post->ID])) {
        $title_addon = " (" . $topposts[$post->ID] . " " . __("views") . ")";
    }
    return $title . $title_addon;
}