/includes/admin/reporting.php

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