stats_get_csv

Get stats from WordPress.com.

Description

(array) stats_get_csv( (string) $table, (null) $args = null ); 

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

    The maximum number of records to return. Default is 10. Maximum 100.

  • 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
);        


Usage

  1. if ( !function_exists( 'stats_get_csv' ) ) { 
  2. require_once '/modules/stats.php'; 
  3.  
  4. // The stats which you want to retrieve: postviews, or searchterms. 
  5. $table = ''; 
  6.  
  7. // An associative array of arguments. 
  8. $args = array( 
  9. 'end' => value is Now, 
  10. 'days' => '', 
  11. 'limit' => 0, 
  12. 'post_id' => 0 
  13. ); 
  14.  
  15. // NOTICE! Understand what this does before running. 
  16. $result = stats_get_csv($table, $args); 
  17.  

Defined (1)

The function is defined in the following location(s).

/modules/stats.php  
  1. function stats_get_csv( $table, $args = null ) { 
  2. $defaults = array( 'end' => false, 'days' => false, 'limit' => 3, 'post_id' => false, 'summarize' => '' ); 
  3.  
  4. $args = wp_parse_args( $args, $defaults ); 
  5. $args['table'] = $table; 
  6. $args['blog_id'] = Jetpack_Options::get_option( 'id' ); 
  7.  
  8. $stats_csv_url = add_query_arg( $args, 'https://stats.wordpress.com/csv.php' ); 
  9.  
  10. $key = md5( $stats_csv_url ); 
  11.  
  12. // Get cache. 
  13. $stats_cache = get_option( 'stats_cache' ); 
  14. if ( ! $stats_cache || ! is_array( $stats_cache ) ) { 
  15. $stats_cache = array(); 
  16.  
  17. // Return or expire this key. 
  18. if ( isset( $stats_cache[ $key ] ) ) { 
  19. $time = key( $stats_cache[ $key ] ); 
  20. if ( time() - $time < 300 ) { 
  21. return $stats_cache[ $key ][ $time ]; 
  22. unset( $stats_cache[ $key ] ); 
  23.  
  24. $stats_rows = array(); 
  25. do { 
  26. if ( ! $stats = stats_get_remote_csv( $stats_csv_url ) ) { 
  27. break; 
  28.  
  29. $labels = array_shift( $stats ); 
  30.  
  31. if ( 0 === stripos( $labels[0], 'error' ) ) { 
  32. break; 
  33.  
  34. $stats_rows = array(); 
  35. for ( $s = 0; isset( $stats[ $s ] ); $s++ ) { 
  36. $row = array(); 
  37. foreach ( $labels as $col => $label ) { 
  38. $row[ $label ] = $stats[ $s ][ $col ]; 
  39. $stats_rows[] = $row; 
  40. } while ( 0 ); 
  41.  
  42. // Expire old keys. 
  43. foreach ( $stats_cache as $k => $cache ) { 
  44. if ( ! is_array( $cache ) || 300 < time() - key( $cache ) ) { 
  45. unset( $stats_cache[ $k ] ); 
  46.  
  47. // Set cache. 
  48. $stats_cache[ $key ] = array( time() => $stats_rows ); 
  49. update_option( 'stats_cache', $stats_cache ); 
  50.  
  51. return $stats_rows;