Yoast_GA_Dashboards_Driver_Generate

Base abstract class.

Defined (1)

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

/admin/dashboards/drivers/class-admin-dashboards-driver-generate.php  
  1. abstract class Yoast_GA_Dashboards_Driver_Generate { 
  2.  
  3. /** 
  4. * @var string - Which type of data should be loaded 
  5. */ 
  6. protected $graph_type; 
  7.  
  8. /** 
  9. * @var string - For which period should the data be shown 
  10. */ 
  11. protected $period; 
  12.  
  13. /** 
  14. * @var string - The end date 
  15. */ 
  16. protected $end_date; 
  17.  
  18. /** 
  19. * @var string - The start date 
  20. */ 
  21. protected $start_date; 
  22.  
  23. /** 
  24. * Construct will set all values and generate the date for response 
  25. */ 
  26. public function __construct() { 
  27. $this->set_graph_type(); 
  28. $this->set_period(); 
  29. $this->set_end_date(); 
  30. $this->set_start_date(); 
  31.  
  32. /** 
  33. * Getting graph_id from post and strip HTML-prefix graph- to get the type 
  34. */ 
  35. protected function set_graph_type() { 
  36. $graph_id = filter_input( INPUT_GET, 'graph_id' ); 
  37. $graph_type = str_replace( array( 'graph-', 'table-' ), '', $graph_id ); 
  38. $this->graph_type = $graph_type; 
  39.  
  40. /** 
  41. * Getting the period from post 
  42. */ 
  43. protected function set_period() { 
  44. $this->period = filter_input( INPUT_GET, 'period' ); 
  45.  
  46. /** 
  47. * Setting the end date 
  48. */ 
  49. protected function set_end_date() { 
  50. $this->end_date = time(); 
  51.  
  52. /** 
  53. * This method will set a start_date based on $this->period 
  54. * The values in dropdown, that will be mapped in strtotime 
  55. * See: http://php.net/manual/en/datetime.formats.relative.php 
  56. */ 
  57. protected function set_start_date() { 
  58.  
  59. switch ( $this->period ) { 
  60. case 'lastweek' : 
  61. $time = '-6 days'; 
  62. break; 
  63. default: 
  64. case 'lastmonth' : 
  65. $time = '-1 month'; 
  66. break; 
  67.  
  68. $start_date = strtotime( $time, $this->end_date ); 
  69.  
  70. $this->start_date = $start_date; 
  71.  
  72. /** 
  73. * Getting the saved Google data 
  74. * @return array 
  75. */ 
  76. protected function get_google_data() { 
  77.  
  78. $response = Yoast_GA_Dashboards_Data::get( $this->graph_type ); 
  79.  
  80. if ( $response != array() && array_key_exists( 'body', $response['value'] ) ) { 
  81. $return = $response['value']['body']; 
  82. else { 
  83. $return = $response; 
  84.  
  85. return $this->filter_google_data( $return ); 
  86.  
  87. /** 
  88. * Check if given timestamp is in given period 
  89. * @param integer $timestamp 
  90. * @return bool 
  91. */ 
  92. protected function is_date_in_period( $timestamp ) { 
  93. return ( $timestamp >= $this->start_date && $timestamp <= $this->end_date ); 
  94.  
  95. /** 
  96. * Escape the data array before output 
  97. * @param array $data The data array that we need to check 
  98. * @return array|boolean The data array which is escaped 
  99. */ 
  100. protected function escape_strings_array( $data ) { 
  101. if ( is_array( $data ) ) { 
  102. foreach ( $data as $key => $value ) { 
  103. if ( is_array( $value ) ) { 
  104. foreach ( $value as $subkey => $subvar ) { 
  105. $data[ $key ][ $subkey ] = esc_html( $subvar ); 
  106. else { 
  107. $data[ $key ] = esc_html( (string) $value ); 
  108.  
  109. return $data; 
  110.  
  111. return false; 
  112.  
  113. /** 
  114. * Should always be available 
  115. * @return mixed 
  116. */ 
  117. abstract public function get_json(); 
  118.