class-wc-tracker.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  1. <?php
  2. /**
  3. * WooCommerce Tracker
  4. *
  5. * The WooCommerce tracker class adds functionality to track WooCommerce usage based on if the customer opted in.
  6. * No personal information is tracked, only general WooCommerce settings, general product, order and user counts and admin email for discount code.
  7. *
  8. * @class WC_Tracker
  9. * @since 2.3.0
  10. * @package WooCommerce/Classes
  11. */
  12. defined( 'ABSPATH' ) || exit;
  13. /**
  14. * WooCommerce Tracker Class
  15. */
  16. class WC_Tracker {
  17. /**
  18. * URL to the WooThemes Tracker API endpoint.
  19. *
  20. * @var string
  21. */
  22. private static $api_url = 'https://tracking.woocommerce.com/v1/';
  23. /**
  24. * Hook into cron event.
  25. */
  26. public static function init() {
  27. add_action( 'woocommerce_tracker_send_event', array( __CLASS__, 'send_tracking_data' ) );
  28. }
  29. /**
  30. * Decide whether to send tracking data or not.
  31. *
  32. * @param boolean $override Should override?.
  33. */
  34. public static function send_tracking_data( $override = false ) {
  35. // Don't trigger this on AJAX Requests.
  36. if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
  37. return;
  38. }
  39. if ( ! apply_filters( 'woocommerce_tracker_send_override', $override ) ) {
  40. // Send a maximum of once per week by default.
  41. $last_send = self::get_last_send_time();
  42. if ( $last_send && $last_send > apply_filters( 'woocommerce_tracker_last_send_interval', strtotime( '-1 week' ) ) ) {
  43. return;
  44. }
  45. } else {
  46. // Make sure there is at least a 1 hour delay between override sends, we don't want duplicate calls due to double clicking links.
  47. $last_send = self::get_last_send_time();
  48. if ( $last_send && $last_send > strtotime( '-1 hours' ) ) {
  49. return;
  50. }
  51. }
  52. // Update time first before sending to ensure it is set.
  53. update_option( 'woocommerce_tracker_last_send', time() );
  54. $params = self::get_tracking_data();
  55. wp_safe_remote_post( self::$api_url, array(
  56. 'method' => 'POST',
  57. 'timeout' => 45,
  58. 'redirection' => 5,
  59. 'httpversion' => '1.0',
  60. 'blocking' => false,
  61. 'headers' => array( 'user-agent' => 'WooCommerceTracker/' . md5( esc_url_raw( home_url( '/' ) ) ) . ';' ),
  62. 'body' => wp_json_encode( $params ),
  63. 'cookies' => array(),
  64. ) );
  65. }
  66. /**
  67. * Get the last time tracking data was sent.
  68. *
  69. * @return int|bool
  70. */
  71. private static function get_last_send_time() {
  72. return apply_filters( 'woocommerce_tracker_last_send_time', get_option( 'woocommerce_tracker_last_send', false ) );
  73. }
  74. /**
  75. * Get all the tracking data.
  76. *
  77. * @return array
  78. */
  79. private static function get_tracking_data() {
  80. $data = array();
  81. // General site info.
  82. $data['url'] = home_url();
  83. $data['email'] = apply_filters( 'woocommerce_tracker_admin_email', get_option( 'admin_email' ) );
  84. $data['theme'] = self::get_theme_info();
  85. // WordPress Info.
  86. $data['wp'] = self::get_wordpress_info();
  87. // Server Info.
  88. $data['server'] = self::get_server_info();
  89. // Plugin info.
  90. $all_plugins = self::get_all_plugins();
  91. $data['active_plugins'] = $all_plugins['active_plugins'];
  92. $data['inactive_plugins'] = $all_plugins['inactive_plugins'];
  93. // Jetpack & WooCommerce Connect.
  94. $data['jetpack_version'] = defined( 'JETPACK__VERSION' ) ? JETPACK__VERSION : 'none';
  95. $data['jetpack_connected'] = ( class_exists( 'Jetpack' ) && is_callable( 'Jetpack::is_active' ) && Jetpack::is_active() ) ? 'yes' : 'no';
  96. $data['jetpack_is_staging'] = ( class_exists( 'Jetpack' ) && is_callable( 'Jetpack::is_staging_site' ) && Jetpack::is_staging_site() ) ? 'yes' : 'no';
  97. $data['connect_installed'] = class_exists( 'WC_Connect_Loader' ) ? 'yes' : 'no';
  98. $data['connect_active'] = ( class_exists( 'WC_Connect_Loader' ) && wp_next_scheduled( 'wc_connect_fetch_service_schemas' ) ) ? 'yes' : 'no';
  99. // Store count info.
  100. $data['users'] = self::get_user_counts();
  101. $data['products'] = self::get_product_counts();
  102. $data['orders'] = self::get_orders();
  103. // Payment gateway info.
  104. $data['gateways'] = self::get_active_payment_gateways();
  105. // Shipping method info.
  106. $data['shipping_methods'] = self::get_active_shipping_methods();
  107. // Get all WooCommerce options info.
  108. $data['settings'] = self::get_all_woocommerce_options_values();
  109. // Template overrides.
  110. $data['template_overrides'] = self::get_all_template_overrides();
  111. // Template overrides.
  112. $data['admin_user_agents'] = self::get_admin_user_agents();
  113. return apply_filters( 'woocommerce_tracker_data', $data );
  114. }
  115. /**
  116. * Get the current theme info, theme name and version.
  117. *
  118. * @return array
  119. */
  120. public static function get_theme_info() {
  121. $theme_data = wp_get_theme();
  122. $theme_child_theme = wc_bool_to_string( is_child_theme() );
  123. $theme_wc_support = wc_bool_to_string( current_theme_supports( 'woocommerce' ) );
  124. return array(
  125. 'name' => $theme_data->Name, // @phpcs:ignore
  126. 'version' => $theme_data->Version, // @phpcs:ignore
  127. 'child_theme' => $theme_child_theme,
  128. 'wc_support' => $theme_wc_support,
  129. );
  130. }
  131. /**
  132. * Get WordPress related data.
  133. *
  134. * @return array
  135. */
  136. private static function get_wordpress_info() {
  137. $wp_data = array();
  138. $memory = wc_let_to_num( WP_MEMORY_LIMIT );
  139. if ( function_exists( 'memory_get_usage' ) ) {
  140. $system_memory = wc_let_to_num( @ini_get( 'memory_limit' ) );
  141. $memory = max( $memory, $system_memory );
  142. }
  143. $wp_data['memory_limit'] = size_format( $memory );
  144. $wp_data['debug_mode'] = ( defined( 'WP_DEBUG' ) && WP_DEBUG ) ? 'Yes' : 'No';
  145. $wp_data['locale'] = get_locale();
  146. $wp_data['version'] = get_bloginfo( 'version' );
  147. $wp_data['multisite'] = is_multisite() ? 'Yes' : 'No';
  148. return $wp_data;
  149. }
  150. /**
  151. * Get server related info.
  152. *
  153. * @return array
  154. */
  155. private static function get_server_info() {
  156. $server_data = array();
  157. if ( ! empty( $_SERVER['SERVER_SOFTWARE'] ) ) {
  158. $server_data['software'] = $_SERVER['SERVER_SOFTWARE']; // @phpcs:ignore
  159. }
  160. if ( function_exists( 'phpversion' ) ) {
  161. $server_data['php_version'] = phpversion();
  162. }
  163. if ( function_exists( 'ini_get' ) ) {
  164. $server_data['php_post_max_size'] = size_format( wc_let_to_num( ini_get( 'post_max_size' ) ) );
  165. $server_data['php_time_limt'] = ini_get( 'max_execution_time' );
  166. $server_data['php_max_input_vars'] = ini_get( 'max_input_vars' );
  167. $server_data['php_suhosin'] = extension_loaded( 'suhosin' ) ? 'Yes' : 'No';
  168. }
  169. $database_version = wc_get_server_database_version();
  170. $server_data['mysql_version'] = $database_version['number'];
  171. $server_data['php_max_upload_size'] = size_format( wp_max_upload_size() );
  172. $server_data['php_default_timezone'] = date_default_timezone_get();
  173. $server_data['php_soap'] = class_exists( 'SoapClient' ) ? 'Yes' : 'No';
  174. $server_data['php_fsockopen'] = function_exists( 'fsockopen' ) ? 'Yes' : 'No';
  175. $server_data['php_curl'] = function_exists( 'curl_init' ) ? 'Yes' : 'No';
  176. return $server_data;
  177. }
  178. /**
  179. * Get all plugins grouped into activated or not.
  180. *
  181. * @return array
  182. */
  183. private static function get_all_plugins() {
  184. // Ensure get_plugins function is loaded.
  185. if ( ! function_exists( 'get_plugins' ) ) {
  186. include ABSPATH . '/wp-admin/includes/plugin.php';
  187. }
  188. $plugins = get_plugins();
  189. $active_plugins_keys = get_option( 'active_plugins', array() );
  190. $active_plugins = array();
  191. foreach ( $plugins as $k => $v ) {
  192. // Take care of formatting the data how we want it.
  193. $formatted = array();
  194. $formatted['name'] = strip_tags( $v['Name'] );
  195. if ( isset( $v['Version'] ) ) {
  196. $formatted['version'] = strip_tags( $v['Version'] );
  197. }
  198. if ( isset( $v['Author'] ) ) {
  199. $formatted['author'] = strip_tags( $v['Author'] );
  200. }
  201. if ( isset( $v['Network'] ) ) {
  202. $formatted['network'] = strip_tags( $v['Network'] );
  203. }
  204. if ( isset( $v['PluginURI'] ) ) {
  205. $formatted['plugin_uri'] = strip_tags( $v['PluginURI'] );
  206. }
  207. if ( in_array( $k, $active_plugins_keys ) ) {
  208. // Remove active plugins from list so we can show active and inactive separately.
  209. unset( $plugins[ $k ] );
  210. $active_plugins[ $k ] = $formatted;
  211. } else {
  212. $plugins[ $k ] = $formatted;
  213. }
  214. }
  215. return array(
  216. 'active_plugins' => $active_plugins,
  217. 'inactive_plugins' => $plugins,
  218. );
  219. }
  220. /**
  221. * Get user totals based on user role.
  222. *
  223. * @return array
  224. */
  225. private static function get_user_counts() {
  226. $user_count = array();
  227. $user_count_data = count_users();
  228. $user_count['total'] = $user_count_data['total_users'];
  229. // Get user count based on user role.
  230. foreach ( $user_count_data['avail_roles'] as $role => $count ) {
  231. $user_count[ $role ] = $count;
  232. }
  233. return $user_count;
  234. }
  235. /**
  236. * Get product totals based on product type.
  237. *
  238. * @return array
  239. */
  240. private static function get_product_counts() {
  241. $product_count = array();
  242. $product_count_data = wp_count_posts( 'product' );
  243. $product_count['total'] = $product_count_data->publish;
  244. $product_statuses = get_terms( 'product_type', array( 'hide_empty' => 0 ) );
  245. foreach ( $product_statuses as $product_status ) {
  246. $product_count[ $product_status->name ] = $product_status->count;
  247. }
  248. return $product_count;
  249. }
  250. /**
  251. * Get order counts
  252. *
  253. * @return array
  254. */
  255. private static function get_order_counts() {
  256. $order_count = array();
  257. $order_count_data = wp_count_posts( 'shop_order' );
  258. foreach ( wc_get_order_statuses() as $status_slug => $status_name ) {
  259. $order_count[ $status_slug ] = $order_count_data->{ $status_slug };
  260. }
  261. return $order_count;
  262. }
  263. /**
  264. * Combine all order data.
  265. *
  266. * @return array
  267. */
  268. private static function get_orders() {
  269. $order_dates = self::get_order_dates();
  270. $order_counts = self::get_order_counts();
  271. $order_totals = self::get_order_totals();
  272. return array_merge( $order_dates, $order_counts, $order_totals );
  273. }
  274. /**
  275. * Get a list of all active payment gateways.
  276. *
  277. * @return array
  278. */
  279. private static function get_active_payment_gateways() {
  280. $active_gateways = array();
  281. $gateways = WC()->payment_gateways->payment_gateways();
  282. foreach ( $gateways as $id => $gateway ) {
  283. if ( isset( $gateway->enabled ) && 'yes' === $gateway->enabled ) {
  284. $active_gateways[ $id ] = array(
  285. 'title' => $gateway->title,
  286. 'supports' => $gateway->supports,
  287. );
  288. }
  289. }
  290. return $active_gateways;
  291. }
  292. /**
  293. * Get a list of all active shipping methods.
  294. *
  295. * @return array
  296. */
  297. private static function get_active_shipping_methods() {
  298. $active_methods = array();
  299. $shipping_methods = WC()->shipping->get_shipping_methods();
  300. foreach ( $shipping_methods as $id => $shipping_method ) {
  301. if ( isset( $shipping_method->enabled ) && 'yes' === $shipping_method->enabled ) {
  302. $active_methods[ $id ] = array(
  303. 'title' => $shipping_method->title,
  304. 'tax_status' => $shipping_method->tax_status,
  305. );
  306. }
  307. }
  308. return $active_methods;
  309. }
  310. /**
  311. * Get all options starting with woocommerce_ prefix.
  312. *
  313. * @return array
  314. */
  315. private static function get_all_woocommerce_options_values() {
  316. return array(
  317. 'version' => WC()->version,
  318. 'currency' => get_woocommerce_currency(),
  319. 'base_location' => WC()->countries->get_base_country(),
  320. 'selling_locations' => WC()->countries->get_allowed_countries(),
  321. 'api_enabled' => get_option( 'woocommerce_api_enabled' ),
  322. 'weight_unit' => get_option( 'woocommerce_weight_unit' ),
  323. 'dimension_unit' => get_option( 'woocommerce_dimension_unit' ),
  324. 'download_method' => get_option( 'woocommerce_file_download_method' ),
  325. 'download_require_login' => get_option( 'woocommerce_downloads_require_login' ),
  326. 'calc_taxes' => get_option( 'woocommerce_calc_taxes' ),
  327. 'coupons_enabled' => get_option( 'woocommerce_enable_coupons' ),
  328. 'guest_checkout' => get_option( 'woocommerce_enable_guest_checkout' ),
  329. 'secure_checkout' => get_option( 'woocommerce_force_ssl_checkout' ),
  330. 'enable_signup_and_login_from_checkout' => get_option( 'woocommerce_enable_signup_and_login_from_checkout' ),
  331. 'enable_myaccount_registration' => get_option( 'woocommerce_enable_myaccount_registration' ),
  332. 'registration_generate_username' => get_option( 'woocommerce_registration_generate_username' ),
  333. 'registration_generate_password' => get_option( 'woocommerce_registration_generate_password' ),
  334. );
  335. }
  336. /**
  337. * Look for any template override and return filenames.
  338. *
  339. * @return array
  340. */
  341. private static function get_all_template_overrides() {
  342. $override_data = array();
  343. $template_paths = apply_filters( 'woocommerce_template_overrides_scan_paths', array( 'WooCommerce' => WC()->plugin_path() . '/templates/' ) );
  344. $scanned_files = array();
  345. require_once( WC()->plugin_path() . '/includes/admin/class-wc-admin-status.php' );
  346. foreach ( $template_paths as $plugin_name => $template_path ) {
  347. $scanned_files[ $plugin_name ] = WC_Admin_Status::scan_template_files( $template_path );
  348. }
  349. foreach ( $scanned_files as $plugin_name => $files ) {
  350. foreach ( $files as $file ) {
  351. if ( file_exists( get_stylesheet_directory() . '/' . $file ) ) {
  352. $theme_file = get_stylesheet_directory() . '/' . $file;
  353. } elseif ( file_exists( get_stylesheet_directory() . '/' . WC()->template_path() . $file ) ) {
  354. $theme_file = get_stylesheet_directory() . '/' . WC()->template_path() . $file;
  355. } elseif ( file_exists( get_template_directory() . '/' . $file ) ) {
  356. $theme_file = get_template_directory() . '/' . $file;
  357. } elseif ( file_exists( get_template_directory() . '/' . WC()->template_path() . $file ) ) {
  358. $theme_file = get_template_directory() . '/' . WC()->template_path() . $file;
  359. } else {
  360. $theme_file = false;
  361. }
  362. if ( false !== $theme_file ) {
  363. $override_data[] = basename( $theme_file );
  364. }
  365. }
  366. }
  367. return $override_data;
  368. }
  369. /**
  370. * When an admin user logs in, there user agent is tracked in user meta and collected here.
  371. *
  372. * @return array
  373. */
  374. private static function get_admin_user_agents() {
  375. return array_filter( (array) get_option( 'woocommerce_tracker_ua', array() ) );
  376. }
  377. /**
  378. * Get order totals
  379. *
  380. * @return array
  381. */
  382. private static function get_order_totals() {
  383. global $wpdb;
  384. $gross_total = $wpdb->get_var( "
  385. SELECT
  386. SUM( order_meta.meta_value ) AS 'gross_total'
  387. FROM {$wpdb->prefix}posts AS orders
  388. LEFT JOIN {$wpdb->prefix}postmeta AS order_meta ON order_meta.post_id = orders.ID
  389. WHERE order_meta.meta_key = '_order_total'
  390. AND orders.post_status = 'wc-completed'
  391. GROUP BY order_meta.meta_key
  392. " );
  393. if ( is_null( $gross_total ) ) {
  394. $gross_total = 0;
  395. }
  396. return array(
  397. 'gross' => $gross_total,
  398. );
  399. }
  400. /**
  401. * Get last order date
  402. *
  403. * @return string
  404. */
  405. private static function get_order_dates() {
  406. global $wpdb;
  407. $min_max = $wpdb->get_row( "
  408. SELECT
  409. MIN( post_date_gmt ) as 'first', MAX( post_date_gmt ) as 'last'
  410. FROM {$wpdb->prefix}posts
  411. WHERE post_type = 'shop_order'
  412. AND post_status = 'wc-completed'
  413. ", ARRAY_A );
  414. if ( is_null( $min_max ) ) {
  415. $min_max = array(
  416. 'first' => '-',
  417. 'last' => '-',
  418. );
  419. }
  420. return $min_max;
  421. }
  422. /**
  423. * Make a request when opting out of tracker usage.
  424. *
  425. * @return void
  426. */
  427. public static function opt_out_request() {
  428. $body = array(
  429. 'event' => 'opt-out',
  430. 'email' => apply_filters( 'woocommerce_tracker_admin_email', get_option( 'admin_email' ) ),
  431. 'url' => home_url(),
  432. );
  433. wp_safe_remote_post( self::$api_url . 'event/', array(
  434. 'method' => 'POST',
  435. 'redirection' => 5,
  436. 'httpversion' => '1.0',
  437. 'blocking' => false,
  438. 'headers' => array( 'user-agent' => 'WooCommerceTracker/' . md5( esc_url_raw( home_url( '/' ) ) ) . ';' ),
  439. 'body' => wp_json_encode( $body ),
  440. 'cookies' => array(),
  441. ) );
  442. }
  443. }
  444. WC_Tracker::init();