tracking.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. <?php
  2. /**
  3. * Tracking functions for reporting plugin usage to the MonsterInsights site for users that have opted in
  4. *
  5. * @package MonsterInsights
  6. * @subpackage Admin
  7. * @copyright Copyright (c) 2018, Chris Christoff
  8. * @since 7.0.0
  9. */
  10. // Exit if accessed directly
  11. if ( ! defined( 'ABSPATH' ) ) {
  12. exit;
  13. }
  14. /**
  15. * Usage tracking
  16. *
  17. * @access public
  18. * @since 7.0.0
  19. * @return void
  20. */
  21. class MonsterInsights_Tracking {
  22. public function __construct() {
  23. add_action( 'init', array( $this, 'schedule_send' ) );
  24. add_action( 'monsterinsights_settings_save_general_end', array( $this, 'check_for_settings_optin' ) );
  25. add_action( 'admin_head', array( $this, 'check_for_optin' ) );
  26. add_action( 'admin_head', array( $this, 'check_for_optout' ) );
  27. add_filter( 'cron_schedules', array( $this, 'add_schedules' ) );
  28. add_action( 'monsterinsights_usage_tracking_cron', array( $this, 'send_checkin' ) );
  29. }
  30. private function get_data() {
  31. $data = array();
  32. // Retrieve current theme info
  33. $theme_data = wp_get_theme();
  34. $tracking_mode = monsterinsights_get_option( 'tracking_mode', 'analytics' );
  35. $events_mode = monsterinsights_get_option( 'events_mode', 'none' );
  36. $update_mode = monsterinsights_get_option( 'automatic_updates', false );
  37. if ( $tracking_mode === false ) {
  38. $tracking_mode = 'analytics';
  39. }
  40. if ( $events_mode === false ) {
  41. $events_mode = 'none';
  42. }
  43. if ( $update_mode === false ) {
  44. $update_mode = 'none';
  45. }
  46. $count_b = 1;
  47. if ( is_multisite() ) {
  48. if ( function_exists( 'get_blog_count' ) ) {
  49. $count_b = get_blog_count();
  50. } else {
  51. $count_b = 'Not Set';
  52. }
  53. }
  54. $usesauth = 'No';
  55. $local = MonsterInsights()->auth->is_authed();
  56. $network = MonsterInsights()->auth->is_network_authed();
  57. if ( $local && $network ) {
  58. $usesauth = 'Both';
  59. } else if ( $local ) {
  60. $usesauth = 'Local';
  61. } else if ( $network ) {
  62. $usesauth = 'Network';
  63. }
  64. $data['php_version'] = phpversion();
  65. $data['mi_version'] = MONSTERINSIGHTS_VERSION;
  66. $data['wp_version'] = get_bloginfo( 'version' );
  67. $data['server'] = isset( $_SERVER['SERVER_SOFTWARE'] ) ? $_SERVER['SERVER_SOFTWARE'] : '';
  68. $data['over_time'] = get_option( 'monsterinsights_over_time', array() );
  69. $data['multisite'] = is_multisite();
  70. $data['url'] = home_url();
  71. $data['themename'] = $theme_data->Name;
  72. $data['themeversion'] = $theme_data->Version;
  73. $data['email'] = get_bloginfo( 'admin_email' );
  74. $data['key'] = monsterinsights_get_license_key();
  75. $data['sas'] = monsterinsights_get_shareasale_id();
  76. $data['settings'] = monsterinsights_get_options();
  77. $data['tracking_mode'] = $tracking_mode;
  78. $data['events_mode'] = $events_mode;
  79. $data['autoupdate'] = $update_mode;
  80. $data['pro'] = (int) monsterinsights_is_pro_version();
  81. $data['sites'] = $count_b;
  82. $data['usagetracking'] = get_option( 'monsterinsights_usage_tracking_config', false );
  83. $data['usercount'] = function_exists( 'get_user_count' ) ? get_user_count() : 'Not Set';
  84. $data['usesauth'] = $usesauth;
  85. $data['timezoneoffset']= date('P');
  86. $data['installed_lite']= get_option( 'monsterinsights_installed_lite', array() );
  87. $data['installed_pro'] = get_option( 'monsterinsights_installed_pro', array() );
  88. // Retrieve current plugin information
  89. if( ! function_exists( 'get_plugins' ) ) {
  90. include ABSPATH . '/wp-admin/includes/plugin.php';
  91. }
  92. $plugins = array_keys( get_plugins() );
  93. $active_plugins = get_option( 'active_plugins', array() );
  94. foreach ( $plugins as $key => $plugin ) {
  95. if ( in_array( $plugin, $active_plugins ) ) {
  96. // Remove active plugins from list so we can show active and inactive separately
  97. unset( $plugins[ $key ] );
  98. }
  99. }
  100. $data['active_plugins'] = $active_plugins;
  101. $data['inactive_plugins'] = $plugins;
  102. $data['locale'] = get_locale();
  103. return $data;
  104. }
  105. public function send_checkin( $override = false, $ignore_last_checkin = false ) {
  106. $home_url = trailingslashit( home_url() );
  107. if ( strpos( $home_url, 'monsterinsights.com' ) !== false ) {
  108. return false;
  109. }
  110. if( ! $this->tracking_allowed() && ! $override ) {
  111. return false;
  112. }
  113. // Send a maximum of once per week
  114. $last_send = get_option( 'monsterinsights_usage_tracking_last_checkin' );
  115. if ( is_numeric( $last_send ) && $last_send > strtotime( '-1 week' ) && ! $ignore_last_checkin ) {
  116. return false;
  117. }
  118. $request = wp_remote_post( 'https://miusage.com/v1/checkin/', array(
  119. 'method' => 'POST',
  120. 'timeout' => 5,
  121. 'redirection' => 5,
  122. 'httpversion' => '1.1',
  123. 'blocking' => false,
  124. 'body' => $this->get_data(),
  125. 'user-agent' => 'MI/' . MONSTERINSIGHTS_VERSION . '; ' . get_bloginfo( 'url' )
  126. ) );
  127. // If we have completed successfully, recheck in 1 week
  128. update_option( 'monsterinsights_usage_tracking_last_checkin', time() );
  129. return true;
  130. }
  131. private function tracking_allowed() {
  132. return (bool) monsterinsights_get_option( 'anonymous_data', false ) || monsterinsights_is_pro_version();
  133. }
  134. public function schedule_send() {
  135. if ( ! wp_next_scheduled( 'monsterinsights_usage_tracking_cron' ) ) {
  136. $tracking = array();
  137. $tracking['day'] = rand( 0, 6 );
  138. $tracking['hour'] = rand( 0, 23 );
  139. $tracking['minute'] = rand( 0, 59 );
  140. $tracking['second'] = rand( 0, 59 );
  141. $tracking['offset'] = ( $tracking['day'] * DAY_IN_SECONDS ) +
  142. ( $tracking['hour'] * HOUR_IN_SECONDS ) +
  143. ( $tracking['minute'] * MINUTE_IN_SECONDS ) +
  144. $tracking['second'];
  145. $tracking['initsend'] = strtotime("next sunday") + $tracking['offset'];
  146. wp_schedule_event( $tracking['initsend'], 'weekly', 'monsterinsights_usage_tracking_cron' );
  147. update_option( 'monsterinsights_usage_tracking_config', $tracking );
  148. }
  149. }
  150. public function check_for_settings_optin() {
  151. if ( ! current_user_can( 'monsterinsights_save_settings' ) ) {
  152. return;
  153. }
  154. if ( monsterinsights_is_pro_version() ) {
  155. return;
  156. }
  157. // Send an intial check in on settings save
  158. $anonymous_data = isset( $_POST['anonymous_data'] ) ? 1 : 0;
  159. if ( $anonymous_data ) {
  160. $this->send_checkin( true, true );
  161. }
  162. }
  163. public function check_for_optin() {
  164. if ( ! ( ! empty( $_REQUEST['mi_action'] ) && 'opt_into_tracking' === $_REQUEST['mi_action'] ) ) {
  165. return;
  166. }
  167. if ( monsterinsights_get_option( 'anonymous_data', false ) ) {
  168. return;
  169. }
  170. if ( ! current_user_can( 'monsterinsights_save_settings' ) ) {
  171. return;
  172. }
  173. if ( monsterinsights_is_pro_version() ) {
  174. return;
  175. }
  176. monsterinsights_update_option( 'anonymous_data', 1 );
  177. $this->send_checkin( true, true );
  178. update_option( 'monsterinsights_tracking_notice', 1 );
  179. }
  180. public function check_for_optout() {
  181. if ( ! ( ! empty( $_REQUEST['mi_action'] ) && 'opt_out_of_tracking' === $_REQUEST['mi_action'] ) ) {
  182. return;
  183. }
  184. if ( monsterinsights_get_option( 'anonymous_data', false ) ) {
  185. return;
  186. }
  187. if ( ! current_user_can( 'monsterinsights_save_settings' ) ) {
  188. return;
  189. }
  190. if ( monsterinsights_is_pro_version() ) {
  191. return;
  192. }
  193. monsterinsights_update_option( 'anonymous_data', 0 );
  194. update_option( 'monsterinsights_tracking_notice', 1 );
  195. }
  196. public function add_schedules( $schedules = array() ) {
  197. // Adds once weekly to the existing schedules.
  198. $schedules['weekly'] = array(
  199. 'interval' => 604800,
  200. 'display' => __( 'Once Weekly', 'google-analytics-for-wordpress' )
  201. );
  202. return $schedules;
  203. }
  204. }
  205. new MonsterInsights_Tracking();