WC_Tracker

The WooCommerce WC Tracker class.

Defined (1)

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

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