tracking.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. <?php
  2. /**
  3. * Tracking functions for reporting plugin usage to the site for users that have opted in
  4. *
  5. * @package ExactMetrics
  6. * @subpackage Admin
  7. * @copyright Copyright (c) 2018, Chris Christoff
  8. * @since 5.3.4
  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 ExactMetrics_Tracking {
  22. public function __construct() {
  23. $this->gadwp = GADWP();
  24. add_action( 'init', array( $this, 'schedule_send' ) );
  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( 'exactmetrics_usage_tracking_cron', array( $this, 'send_checkin' ) );
  29. add_action( 'exactmetrics_settings_usage_tracking', array( $this, 'check_for_settings_optin' ) );
  30. add_action( 'admin_notices', array( $this, 'setup_notice' ), 999 );
  31. }
  32. private function get_data() {
  33. $data = array();
  34. $options = get_option( 'gadwp_options' );
  35. if ( empty( $options ) ) {
  36. $options = array();
  37. } else {
  38. $options = (array) json_decode( $options );
  39. }
  40. $network_options = get_site_option( 'gadwp_network_options' );
  41. if ( empty( $network_options ) ) {
  42. $network_options = array();
  43. } else {
  44. $network_options = (array) json_decode( $network_options );
  45. }
  46. // Foreach network options, prefix with network
  47. if ( ! empty ( $network_options ) ) {
  48. foreach ( $network_options as $noptionid => $noptionvalue ) {
  49. $new_id = 'network_' . $noptionid;
  50. $options[ $new_id ] = $noptionvalue;
  51. }
  52. }
  53. // Ensure tokens and secrets are never sent to us
  54. unset( $options['token'] );
  55. unset( $options['client_secret'] );
  56. unset( $options['network_token'] );
  57. unset( $options['network_client_secret'] );
  58. // Retrieve current theme info
  59. $theme_data = wp_get_theme();
  60. $tracking_mode = 'default';
  61. if ( ! empty( $options['tracking_type'] ) || ! empty( $options['network_tracking_type'] ) ) {
  62. $tracking_mode = 'minor';
  63. }
  64. if ( ! empty( $options['ga_with_gtag'] ) || ! empty( $options['network_ga_with_gtag'] ) ) {
  65. $tracking_mode = 'gtag';
  66. }
  67. $update_mode = 'none';
  68. if ( ! empty( $options['automatic_updates_minorversion'] ) || ! empty( $options['network_automatic_updates_minorversion'] ) ) {
  69. $update_mode = 'minor';
  70. }
  71. $count_b = 1;
  72. if ( is_multisite() ) {
  73. if ( function_exists( 'get_blog_count' ) ) {
  74. $count_b = get_blog_count();
  75. } else {
  76. $count_b = 'Not Set';
  77. }
  78. }
  79. $data['php_version'] = phpversion();
  80. $data['mi_version'] = GADWP_CURRENT_VERSION;
  81. $data['wp_version'] = get_bloginfo( 'version' );
  82. $data['server'] = isset( $_SERVER['SERVER_SOFTWARE'] ) ? $_SERVER['SERVER_SOFTWARE'] : '';
  83. $data['multisite'] = is_multisite();
  84. $data['url'] = home_url();
  85. $data['themename'] = $theme_data->Name;
  86. $data['themeversion'] = $theme_data->Version;
  87. $data['settings'] = $options;
  88. $data['tracking_mode'] = $tracking_mode;
  89. $data['autoupdate'] = $update_mode;
  90. $data['sites'] = $count_b;
  91. $data['usagetracking'] = get_option( 'exactmetrics_usage_tracking_config', false );
  92. $data['usercount'] = function_exists( 'get_user_count' ) ? get_user_count() : 'Not Set';
  93. $data['timezoneoffset']= date('P');
  94. // Retrieve current plugin information
  95. if( ! function_exists( 'get_plugins' ) ) {
  96. include ABSPATH . '/wp-admin/includes/plugin.php';
  97. }
  98. $plugins = array_keys( get_plugins() );
  99. $active_plugins = get_option( 'active_plugins', array() );
  100. foreach ( $plugins as $key => $plugin ) {
  101. if ( in_array( $plugin, $active_plugins ) ) {
  102. // Remove active plugins from list so we can show active and inactive separately
  103. unset( $plugins[ $key ] );
  104. }
  105. }
  106. $data['active_plugins'] = $active_plugins;
  107. $data['inactive_plugins'] = $plugins;
  108. $data['locale'] = get_locale();
  109. return $data;
  110. }
  111. public function send_checkin( $override = false, $ignore_last_checkin = false ) {
  112. $home_url = trailingslashit( home_url() );
  113. if ( strpos( $home_url, 'exactmetrics.com' ) !== false ) {
  114. return false;
  115. }
  116. if( ! $this->tracking_allowed() && ! $override ) {
  117. return false;
  118. }
  119. // Send a maximum of once per week
  120. $last_send = get_option( 'exactmetrics_usage_tracking_last_checkin' );
  121. if ( is_numeric( $last_send ) && $last_send > strtotime( '-1 week' ) && ! $ignore_last_checkin ) {
  122. return false;
  123. }
  124. $request = wp_remote_post( 'https://miusage.com/v1/em-checkin/', array(
  125. 'method' => 'POST',
  126. 'timeout' => 5,
  127. 'redirection' => 5,
  128. 'httpversion' => '1.1',
  129. 'blocking' => false,
  130. 'body' => $this->get_data(),
  131. 'user-agent' => 'EM/' . GADWP_CURRENT_VERSION . '; ' . get_bloginfo( 'url' )
  132. ) );
  133. // If we have completed successfully, recheck in 1 week
  134. update_option( 'exactmetrics_usage_tracking_last_checkin', time() );
  135. return true;
  136. }
  137. private function tracking_allowed() {
  138. return (bool) $this->get_option( 'usage_tracking', 0 );
  139. }
  140. public function schedule_send() {
  141. if ( ! wp_next_scheduled( 'exactmetrics_usage_tracking_cron' ) ) {
  142. $tracking = array();
  143. $tracking['day'] = rand( 0, 6 );
  144. $tracking['hour'] = rand( 0, 23 );
  145. $tracking['minute'] = rand( 0, 59 );
  146. $tracking['second'] = rand( 0, 59 );
  147. $tracking['offset'] = ( $tracking['day'] * DAY_IN_SECONDS ) +
  148. ( $tracking['hour'] * HOUR_IN_SECONDS ) +
  149. ( $tracking['minute'] * MINUTE_IN_SECONDS ) +
  150. $tracking['second'];
  151. $tracking['initsend'] = strtotime("next sunday") + $tracking['offset'];
  152. wp_schedule_event( $tracking['initsend'], 'weekly', 'exactmetrics_usage_tracking_cron' );
  153. update_option( 'exactmetrics_usage_tracking_config', $tracking );
  154. }
  155. }
  156. public function check_for_settings_optin( $new_value ) {
  157. if ( ! current_user_can( 'manage_options' ) ) {
  158. return;
  159. }
  160. $new_value = intval( $new_value );
  161. $current_value = $this->get_option( 'usage_tracking', 0 );
  162. if ( $current_value == $new_value ) {
  163. return;
  164. }
  165. if ( $new_value ) {
  166. $this->send_checkin( true, true );
  167. update_option( 'exactmetrics_tracking_notice', 1 );
  168. } else {
  169. update_option( 'exactmetrics_tracking_notice', 0 );
  170. }
  171. }
  172. public function check_for_optin() {
  173. if ( ! ( ! empty( $_REQUEST['em_action'] ) && 'opt_into_tracking' === $_REQUEST['em_action'] ) ) {
  174. return;
  175. }
  176. if ( $this->get_option( 'usage_tracking', 0 ) ) {
  177. return;
  178. }
  179. if ( ! current_user_can( 'manage_options' ) ) {
  180. return;
  181. }
  182. $this->set_option( 'usage_tracking', 1 );
  183. $this->send_checkin( true, true );
  184. update_option( 'exactmetrics_tracking_notice', 1 );
  185. }
  186. public function check_for_optout() {
  187. if ( ! ( ! empty( $_REQUEST['em_action'] ) && 'opt_out_of_tracking' === $_REQUEST['em_action'] ) ) {
  188. return;
  189. }
  190. if ( $this->get_option( 'usage_tracking', 0 ) ) {
  191. return;
  192. }
  193. if ( ! current_user_can( 'manage_options' ) ) {
  194. return;
  195. }
  196. $this->set_option( 'usage_tracking', 0 );
  197. update_option( 'exactmetrics_tracking_notice', 1 );
  198. }
  199. public function setup_notice(){
  200. if ( ! is_network_admin() && ( ! isset( $_GET['page'] ) || ( isset( $_GET['page'] ) && $_GET['page'] !== 'gadwp_tracking_settings' ) && $_GET['page'] !== 'gadwp_settings' ) ) {
  201. if ( ! get_option( 'exactmetrics_tracking_notice' ) ) {
  202. if ( ! $this->get_option( 'usage_tracking', 0 ) ) {
  203. $optin_url = add_query_arg( 'em_action', 'opt_into_tracking' );
  204. $optout_url = add_query_arg( 'em_action', 'opt_out_of_tracking' );
  205. echo '<div class="updated"><p>';
  206. echo sprintf( esc_html__( 'ExactMetrics would like to better understand how our users use our plugin so we can get a better understanding of which features and bugfixes to prioritize. %1$sCan we collect some %2$sinformation about our plugin usage?%3$s', 'google-analytics-dashboard-for-wp' ), '<br />', '<a href="https://exactmetrics.com/usage-tracking/?utm_source=wpdashboard&utm_campaign=usagetracking&utm_medium=plugin" target="_blank">', '</a>' );
  207. echo '&nbsp;<a href="' . esc_url( $optin_url ) . '" class="button-secondary">' . __( 'Yes, I\'d like to help out', 'google-analytics-dashboard-for-wp' ) . '</a>';
  208. echo '&nbsp;<a href="' . esc_url( $optout_url ) . '" class="button-secondary">' . __( 'No thanks', 'google-analytics-dashboard-for-wp' ) . '</a>';
  209. echo '</p></div>';
  210. return;
  211. }
  212. }
  213. }
  214. }
  215. public function add_schedules( $schedules = array() ) {
  216. // Adds once weekly to the existing schedules.
  217. $schedules['weekly'] = array(
  218. 'interval' => 604800,
  219. 'display' => __( 'Once Weekly', 'google-analytics-dashboard-for-wp' )
  220. );
  221. return $schedules;
  222. }
  223. public function get_option( $option, $default = false ) {
  224. if ( ! empty( $this->gadwp->config->options ) && is_array( $this->gadwp->config->options ) && isset( $this->gadwp->config->options[$option] ) ) {
  225. return $this->gadwp->config->options[$option];
  226. } else {
  227. return $default;
  228. }
  229. }
  230. public function set_option( $option, $value ) {
  231. $new_options = array();
  232. $new_options[$option] = $value;
  233. $options = array_merge( $this->gadwp->config->options, $new_options );
  234. $this->gadwp->config->options = $options;
  235. $this->gadwp->config->set_plugin_options( false );
  236. }
  237. }