示例#1
0
function stats_dashboard_widget_content()
{
    $blog_id = stats_get_option('blog_id');
    if (!($width = (int) ($_GET['width'] / 2)) || $width < 250) {
        $width = 370;
    }
    if (!($height = (int) $_GET['height'] - 36) || $height < 230) {
        $height = 230;
    }
    $_width = $width - 5;
    $_height = $height - ($GLOBALS['is_winIE'] ? 16 : 5);
    // hack!
    $options = stats_dashboard_widget_options();
    $q = array('noheader' => 'true', 'proxy' => '', 'page' => 'stats', 'blog' => $blog_id, 'key' => stats_get_api_key(), 'chart' => '', 'unit' => $options['chart'], 'width' => $_width, 'height' => $_height);
    $url = 'http://dashboard.wordpress.com/wp-admin/index.php';
    $url = add_query_arg($q, $url);
    $get = wp_remote_get($url, array('timeout' => 300));
    if (is_wp_error($get) || empty($get['body'])) {
        $http = $_SERVER['HTTPS'] ? 'https' : 'http';
        $src = clean_url("{$http}://dashboard.wordpress.com/wp-admin/index.php?page=estats&blog={$blog_id}&noheader=true&chart&unit={$options['chart']}&width={$_width}&height={$_height}");
        echo "<iframe id='stats-graph' class='stats-section' frameborder='0' style='width: {$width}px; height: {$height}px; overflow: hidden' src='{$src}'></iframe>";
    } else {
        $body = stats_convert_post_titles($get['body']);
        $body = stats_convert_swf_urls($body);
        $body = stats_convert_chart_urls($body);
        $body = stats_convert_image_urls($body);
        echo $body;
    }
    $post_ids = array();
    if (version_compare('2.7-z', $GLOBALS['wp_version'], '<=')) {
        $csv_args = array('top' => '&limit=8', 'active' => '&limit=5', 'search' => '&limit=5');
        /* translators: Stats dashboard widget postviews list: "$post_title $views Views" */
        $printf = __('%1$s %2$s Views', 'stats');
    } else {
        $csv_args = array('top' => '', 'active' => '', 'search' => '');
        /* translators: Stats dashboard widget postviews list: "$post_title, $views Views" */
        $printf = __('%1$s, %2$s views', 'stats');
    }
    foreach ($top_posts = stats_get_csv('postviews', "days={$options['top']}{$csv_args['top']}") as $post) {
        $post_ids[] = $post['post_id'];
    }
    foreach ($active_posts = stats_get_csv('postviews', "days={$options['active']}{$csv_args['active']}") as $post) {
        $post_ids[] = $post['post_id'];
    }
    // cache
    get_posts(array('include' => join(',', array_unique($post_ids))));
    $searches = array();
    foreach ($search_terms = stats_get_csv('searchterms', "days={$options['search']}{$csv_args['search']}") as $search_term) {
        $searches[] = esc_html($search_term['searchterm']);
    }
    ?>
<div id="stats-info">
	<div id="top-posts" class='stats-section'>
		<div class="stats-section-inner">
		<h4 class="heading"><?php 
    _e('Top Posts', 'stats');
    ?>
</h4>
		<?php 
    foreach ($top_posts as $post) {
        if (!get_post($post['post_id'])) {
            continue;
        }
        ?>
		<p><?php 
        printf($printf, '<a href="' . get_permalink($post['post_id']) . '">' . get_the_title($post['post_id']) . '</a>', number_format_i18n($post['views']));
        ?>
</p>
		<?php 
    }
    ?>
		</div>
	</div>
	<div id="top-search" class='stats-section'>
		<div class="stats-section-inner">
		<h4 class="heading"><?php 
    _e('Top Searches', 'stats');
    ?>
</h4>
		<p><?php 
    echo join(',&nbsp; ', $searches);
    ?>
</p>
		</div>
	</div>
	<div id="active" class='stats-section'>
		<div class="stats-section-inner">
		<h4 class="heading"><?php 
    _e('Most Active', 'stats');
    ?>
</h4>
		<?php 
    foreach ($active_posts as $post) {
        if (!get_post($post['post_id'])) {
            continue;
        }
        ?>
		<p><?php 
        printf($printf, '<a href="' . get_permalink($post['post_id']) . '">' . get_the_title($post['post_id']) . '</a>', number_format_i18n($post['views']));
        ?>
</p>
		<?php 
    }
    ?>
		</div>
	</div>
</div>
<br class="clear" />
<p class="textright">
	<a class="button" href="index.php?page=stats"><?php 
    _e('View All', 'stats');
    ?>
</a>
</p>
<?php 
    exit;
}
示例#2
0
function stats_get_csv($table, $args = null)
{
    $blog_id = stats_get_option('blog_id');
    $key = stats_get_api_key();
    if (!$blog_id || !$key) {
        return array();
    }
    $defaults = array('end' => false, 'days' => false, 'limit' => 3, 'post_id' => false, 'summarize' => '');
    $args = wp_parse_args($args, $defaults);
    $args['table'] = $table;
    $args['blog_id'] = $blog_id;
    $args['api_key'] = $key;
    $stats_csv_url = add_query_arg($args, 'http://stats.wordpress.com/csv.php');
    $key = md5($stats_csv_url);
    // Get cache
    $stats_cache = get_option('stats_cache');
    if (!$stats_cache || !is_array($stats_cache)) {
        $stats_cache = array();
    }
    // Return or expire this key
    if (isset($stats_cache[$key])) {
        $time = key($stats_cache[$key]);
        if (time() - $time < 300) {
            return $stats_cache[$key][$time];
        }
        unset($stats_cache[$key]);
    }
    $stats_rows = array();
    do {
        if (!($stats = stats_get_remote_csv($stats_csv_url))) {
            break;
        }
        $labels = array_shift($stats);
        if (0 === stripos($labels[0], 'error')) {
            break;
        }
        $stats_rows = array();
        for ($s = 0; isset($stats[$s]); $s++) {
            $row = array();
            foreach ($labels as $col => $label) {
                $row[$label] = $stats[$s][$col];
            }
            $stats_rows[] = $row;
        }
    } while (0);
    // Expire old keys
    foreach ($stats_cache as $k => $cache) {
        if (!is_array($cache) || 300 < time() - key($cache)) {
            unset($stats_cache[$k]);
        }
    }
    // Set cache
    $stats_cache[$key] = array(time() => $stats_rows);
    update_option('stats_cache', $stats_cache);
    return $stats_rows;
}