MonsterInsights_Reporting

The Google Analytics for WordPress by MonsterInsights MonsterInsights Reporting class.

Defined (1)

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

/includes/admin/reporting.php  
  1. final class MonsterInsights_Reporting { 
  2.  
  3. /** 
  4. * Holds the base object. 
  5. * @access public 
  6. * @since 6.0.0 
  7. * @var MonsterInsights $base MonsterInsights Base object. 
  8. */ 
  9. public $base; 
  10.  
  11. /** 
  12. * Holds the GA client object if using oAuth. 
  13. * @access public 
  14. * @since 6.0.0 
  15. * @var MonsterInsights_GA_Client $client GA client object. 
  16. */ 
  17. public $client; 
  18.  
  19. /** 
  20. * Is the dashboard/reporting pages disabled? 
  21. * @access public 
  22. * @since 6.0.0 
  23. * @var bool $dashboard_disabled If the dashboard is disabled. 
  24. */ 
  25. public $dashboard_disabled; 
  26.  
  27. /** 
  28. * Dashboard report 
  29. * @access public 
  30. * @since 6.0.0 
  31. * @var string $dashboard_report Report hook name of dashboard report. 
  32. */ 
  33. public $dashboard_report; 
  34.  
  35. /** 
  36. * Primary class constructor. 
  37. * @access public 
  38. * @since 6.0.0 
  39. */ 
  40. public function __construct( ) { 
  41.  
  42. // Get object  
  43. $this->base = MonsterInsights(); 
  44. $this->client = $this->base->ga; 
  45. $this->dashboard_disabled = monsterinsights_get_option( 'dashboards_disabled', false ); 
  46. $this->dashboard_report = monsterinsights_get_option( 'dashboard_report', 'overview' ); 
  47.  
  48. if ( isset( $this->client->status ) && $this->client->status === 'valid' ) { 
  49. // Cron actions 
  50. // Add cron if its not there 
  51. add_action( 'wp', array( $this, 'schedule_cron' ) ); 
  52.  
  53. // Collect analytics data on cron event 
  54. add_action( 'monsterinsights_daily_cron', array( $this, 'run_cron' ) ); 
  55.  
  56. // If cron did not run for some reason (no users visited site), run it now 
  57. if ( filter_input( INPUT_GET, 'page' ) === 'monsterinsights_dashboard' || filter_input( INPUT_GET, 'page' ) === 'monsterinsights_reports' ) { 
  58. add_action( 'admin_init', array( $this, 'maybe_get_data' ) ); 
  59.  
  60.  
  61. // Dashboard disabled setting 
  62. add_action( 'monsterinsights_settings_save_general', array( $this, 'dashboard_disabled' ), 9 ); 
  63.  
  64. public function dashboard_disabled( ) { 
  65. $dashboards_disabled = isset( $_POST['dashboards_disabled'] ) ? 1 : 0; 
  66. $dashboards_disabled_old = monsterinsights_get_option( 'dashboards_disabled', false ); 
  67.  
  68. // We only care if the switch is going on or off 
  69. if ( $dashboards_disabled && ! $dashboards_disabled_old ) { 
  70. // The dashboards are now being disabled 
  71. // Clear data + last run + failed 
  72. $this->delete_aggregate_data(); 
  73.  
  74. } else if ( ! $dashboards_disabled && $dashboards_disabled_old ) { 
  75. // The dashboards are now being enabled 
  76. // Refresh data + schedule cron 
  77. $this->refresh_aggregate_data(); 
  78. $this->schedule_cron(); 
  79.  
  80. public function schedule_cron() { 
  81. if ( ! wp_next_scheduled( 'monsterinsights_daily_cron' ) ) { 
  82. // Set the next event of fetching data 
  83. wp_schedule_event( strtotime( date( 'Y-m-d', strtotime( 'tomorrow' ) ) . ' 00:01:00 ' ), 'daily', 'monsterinsights_daily_cron' ); 
  84.  
  85. public function maybe_get_data() { 
  86. if ( ! current_user_can( 'monsterinsights_view_dashboard' ) ) { 
  87. return false; 
  88.  
  89. $last_run = monsterinsights_get_option( 'cron_last_run', false ); 
  90.  
  91. // See if issue connecting or expired 
  92. if ( $last_run === false || monsterinsights_hours_between( $last_run ) >= 24 || date( 'Ymd', $last_run ) !== date('Ymd', time() ) ) {  
  93. $this->run_cron(); 
  94.  
  95. public function run_cron() { 
  96. if ( $this->dashboard_disabled ) { 
  97. return; 
  98.  
  99. if ( is_numeric( (int) $this->client->profile ) && $this->client->status === 'valid' ) { 
  100. // Profile is set 
  101. $this->add_aggregate_data(); 
  102.  
  103. public function refresh_aggregate_data() { 
  104. if ( ! current_user_can( 'monsterinsights_view_dashboard' ) ) { 
  105. return false; 
  106.  
  107. $this->delete_aggregate_data(); 
  108. $this->add_aggregate_data(); 
  109.  
  110. private function add_aggregate_data() { 
  111. if ( ! current_user_can( 'monsterinsights_view_dashboard' ) ) { 
  112. return false; 
  113. /**  
  114. * Developer Alert: 
  115. * Per the README, this is considered an internal hook and should 
  116. * not be used by other developers. This hook's behavior may be modified 
  117. * or the hook may be removed at any time, without warning. 
  118. */ 
  119. do_action( 'monsterinsights_add_aggregate_data', $this->client, $this->client->profile ); 
  120. monsterinsights_update_option( 'cron_last_run', time() ); 
  121. monsterinsights_delete_option( 'cron_failed' ); 
  122.  
  123. public function delete_aggregate_data() { 
  124. if ( ! current_user_can( 'monsterinsights_view_dashboard' ) ) { 
  125. return false; 
  126. /**  
  127. * Developer Alert: 
  128. * Per the README, this is considered an internal hook and should 
  129. * not be used by other developers. This hook's behavior may be modified 
  130. * or the hook may be removed at any time, without warning. 
  131. */ 
  132. if ( ! empty( $this->client ) && ! empty( $this->client->profile ) ) { 
  133. do_action( 'monsterinsights_delete_aggregate_data', $this->client, $this->client->profile ); 
  134. $options = array( 
  135. 'cron_failed',  
  136. 'cron_last_run',  
  137. ); 
  138. monsterinsights_delete_options( $options );