stats_get_csv
Get stats from WordPress.com.
Description
Returns (array)
{ An array of post view data, each post as an array array { The post view data for a single post @type string $post_id The ID of the post @type string $post_title The title of the post @type string $post_permalink The permalink for the post @type string $views The number of views for the post within the $num_days specified } }
Parameters (2)
- 0. $table (string)
- The stats which you want to retrieve: postviews, or searchterms
- 1. $args — Optional. (null) =>
null
- An associative array of arguments.
Options
- end (bool) =>
value is Now
The last day of the desired time frame. Format is Y-m-d (e.g. 2007-05-01) and default timezone is UTC date.
- days (string) =>
''
The length of the desired time frame. Default is 30. Maximum 90 days.
- limit (int) =>
0
- post_id (int) =>
0
The ID of the post to retrieve stats data for
array( /** * The last day of the desired time frame. Format is 'Y-m-d' (e.g. 2007-05-01) and default * timezone is UTC date. * * @type bool * @default value is Now */ 'end' => value is Now, /** * The length of the desired time frame. Default is 30. Maximum 90 days. * * @type string * @default '' */ 'days' => '', /** * The maximum number of records to return. Default is 10. Maximum 100. * * @type int */ 'limit' => 0, /** * The ID of the post to retrieve stats data for * * @type int */ 'post_id' => 0 );
…
- end (bool) =>
Usage
if ( !function_exists( 'stats_get_csv' ) ) { require_once ABSPATH . PLUGINDIR . 'jetpack-by-wordpress-com/modules/stats.php'; } // The stats which you want to retrieve: postviews, or searchterms $table = ''; // An associative array of arguments. $args = array( 'end' => value is Now, 'days' => '', 'limit' => 0, 'post_id' => 0 ); // NOTICE! Understand what this does before running. $result = stats_get_csv($table, $args);
Defined (1)
The function is defined in the following location(s).
- /modules/stats.php
- function stats_get_csv( $table, $args = null ) {
- $defaults = array( 'end' => false, 'days' => false, 'limit' => 3, 'post_id' => false, 'summarize' => '' );
- $args = wp_parse_args( $args, $defaults );
- $args['table'] = $table;
- $args['blog_id'] = Jetpack_Options::get_option( 'id' );
- $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;
- }