Yoast_GA_Dashboards_Graph_Generate

Graph inherited class.

Defined (1)

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

/admin/dashboards/drivers/class-admin-dashboards-graph-generate.php  
  1. class Yoast_GA_Dashboards_Graph_Generate extends Yoast_GA_Dashboards_Driver_Generate { 
  2.  
  3. /** 
  4. * The field that will be used for displaying on x-axis. Mostly it will be the j (day in month) 
  5. * See: http://nl3.php.net/manual/en/function.date.php under 'format' 
  6. * @var string 
  7. */ 
  8. private $date_field = 'j'; 
  9.  
  10. /** 
  11. * @var array - Storage for $data 
  12. */ 
  13. private $data = array(); 
  14.  
  15. /** 
  16. * Storage for mapping 
  17. * @var array 
  18. */ 
  19. private $mapping = array( 
  20. 'x' => array(),  
  21. 'hover' => array(),  
  22. ); 
  23.  
  24. /** 
  25. * Construct will set all values and generate the date for response 
  26. */ 
  27. public function __construct() { 
  28. parent::__construct(); 
  29.  
  30. $this->set_date_field(); 
  31.  
  32. $this->generate(); 
  33.  
  34. /** 
  35. * Putting $this->data and $this->mapping and give them back as a json encoded string 
  36. * @return string 
  37. */ 
  38. public function get_json() { 
  39. $return = array( 
  40. 'data' => $this->data,  
  41. 'mapping' => $this->mapping,  
  42. ); 
  43.  
  44. return json_encode( $return ); 
  45.  
  46. /** 
  47. * Filtering the current data to eliminate all values which are not in given period 
  48. * @param integer $google_data 
  49. * @return integer 
  50. */ 
  51. protected function filter_google_data( $google_data ) { 
  52.  
  53. foreach ( $google_data['value'] as $unix_timestamp => $value ) { 
  54. if ( $this->is_date_in_period( $unix_timestamp ) ) { 
  55. $return[ $unix_timestamp ] = $value; 
  56.  
  57. return $return; 
  58.  
  59. /** 
  60. * Which field should be taken from timestamp. Most cases J will be good 
  61. */ 
  62. private function set_date_field() { 
  63. switch ( $this->period ) { 
  64. default: 
  65. case 'lastweek' : 
  66. case 'lastmonth' : 
  67. $date_field = 'j'; 
  68. break; 
  69.  
  70. $this->date_field = $date_field; 
  71.  
  72. /** 
  73. * Generate the data for the frontend based on the $google_data 
  74. */ 
  75. private function generate() { 
  76. $google_data = $this->get_google_data(); 
  77.  
  78. foreach ( $google_data as $timestamp => $value ) { 
  79. $timestamp = esc_html( $timestamp ); 
  80.  
  81. $this->add_data( $value ); 
  82. $this->add_x_mapping( $timestamp ); 
  83. $this->add_hover_mapping( $timestamp, $value ); 
  84.  
  85. $this->mapping['x'] = array_filter( $this->mapping['x'] ); 
  86.  
  87. /** 
  88. * Adding value to data property 
  89. * x is position on x-axis, always starting from 0 
  90. * y is the value of that point. 
  91. * @param integer $value 
  92. */ 
  93. private function add_data( $value ) { 
  94. static $current_x = 0; 
  95.  
  96. $this->data[] = array( 
  97. 'x' => $current_x,  
  98. 'y' => $value,  
  99. ); 
  100.  
  101. $current_x ++; 
  102.  
  103. /** 
  104. * Add date field to the x-mapping 
  105. * Key will be auto numbered by PHP, starting with 0, the key will always point to the the x in x-axis 
  106. * The value will be always the value that should be displayed. 
  107. * @param integer $timestamp 
  108. */ 
  109. private function add_x_mapping( $timestamp ) { 
  110.  
  111. $is_monday = ( 'Mon' === date( 'D', $timestamp ) ); 
  112. $this->mapping['x'][] = ( $is_monday ) ? date( $this->date_field . ' M', $timestamp ) : null; 
  113.  
  114. /** 
  115. * Add date field to the hover-mapping 
  116. * @param integer $timestamp 
  117. * @param integer $value 
  118. */ 
  119. private function add_hover_mapping( $timestamp, $value ) { 
  120. $this->mapping['hover'][] = esc_html( date_i18n( 'l ' . $this->date_field . ' M', $timestamp ) . ': ' . number_format_i18n( $value, 0 ) ); 
  121.