PageRenderTime 24ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/woocommerce/includes/class-wc-tracker.php

https://gitlab.com/webkod3r/tripolis
PHP | 393 lines | 241 code | 59 blank | 93 comment | 34 complexity | 17c8ebe13d79e9109b3c3240f3ad55a0 MD5 | raw file
  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 infomation is tracked, only general WooCommerce settings, general product, order and user counts and admin email for discount code.
  7. *
  8. * @class WC_Tracker
  9. * @version 2.3.0
  10. * @package WooCommerce/Classes
  11. * @category Class
  12. * @author WooThemes
  13. */
  14. if ( ! defined( 'ABSPATH' ) ) {
  15. exit;
  16. }
  17. class WC_Tracker {
  18. /**
  19. * URL to the WooThemes Tracker API endpoint.
  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
  33. */
  34. public static function send_tracking_data( $override = false ) {
  35. // Dont 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 dont 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. $response = 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( home_url( '/' ) ) ) . ';' ),
  62. 'body' => json_encode( $params ),
  63. 'cookies' => array()
  64. )
  65. );
  66. }
  67. /**
  68. * Get the last time tracking data was sent.
  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. * @return array
  77. */
  78. private static function get_tracking_data() {
  79. $data = array();
  80. // General site info
  81. $data['url'] = home_url();
  82. $data['email'] = apply_filters( 'woocommerce_tracker_admin_email', get_option( 'admin_email' ) );
  83. $data['theme'] = self::get_theme_info();
  84. // WordPress Info
  85. $data['wp'] = self::get_wordpress_info();
  86. // Server Info
  87. $data['server'] = self::get_server_info();
  88. // Plugin info
  89. $all_plugins = self::get_all_plugins();
  90. $data['active_plugins'] = $all_plugins['active_plugins'];
  91. $data['inactive_plugins'] = $all_plugins['inactive_plugins'];
  92. // Store count info
  93. $data['users'] = self::get_user_counts();
  94. $data['products'] = self::get_product_counts();
  95. $data['orders'] = self::get_order_counts();
  96. // Payment gateway info
  97. $data['gateways'] = self::get_active_payment_gateways();
  98. // Shipping method info
  99. $data['shipping_methods'] = self::get_active_shipping_methods();
  100. // Get all WooCommerce options info
  101. $data['settings'] = self::get_all_woocommerce_options_values();
  102. // Template overrides
  103. $data['template_overrides'] = self::get_all_template_overrides();
  104. return apply_filters( 'woocommerce_tracker_data', $data );
  105. }
  106. /**
  107. * Get the current theme info, theme name and version.
  108. * @return array
  109. */
  110. public static function get_theme_info() {
  111. $wp_version = get_bloginfo( 'version' );
  112. if ( version_compare( $wp_version, '3.4', '<' ) ) {
  113. $theme_data = get_theme_data( get_stylesheet_directory() . '/style.css' );
  114. $theme_name = $theme_data['Name'];
  115. $theme_version = $theme_data['Version'];
  116. } else {
  117. $theme_data = wp_get_theme();
  118. $theme_name = $theme_data->Name;
  119. $theme_version = $theme_data->Version;
  120. }
  121. $theme_child_theme = is_child_theme() ? 'Yes' : 'No';
  122. $theme_wc_support = ( ! current_theme_supports( 'woocommerce' ) && ! in_array( $theme_data->template, wc_get_core_supported_themes() ) ) ? 'No' : 'Yes';
  123. return array( 'name' => $theme_name, 'version' => $theme_version, 'child_theme' => $theme_child_theme, 'wc_support' => $theme_wc_support );
  124. }
  125. /**
  126. * Get WordPress related data.
  127. * @return array
  128. */
  129. private static function get_wordpress_info() {
  130. $wp_data = array();
  131. $memory = wc_let_to_num( WP_MEMORY_LIMIT );
  132. if ( function_exists( 'memory_get_usage' ) ) {
  133. $system_memory = wc_let_to_num( @ini_get( 'memory_limit' ) );
  134. $memory = max( $memory, $system_memory );
  135. }
  136. $wp_data['memory_limit'] = size_format( $memory );
  137. $wp_data['debug_mode'] = ( defined('WP_DEBUG') && WP_DEBUG ) ? 'Yes' : 'No';
  138. $wp_data['locale'] = get_locale();
  139. $wp_data['version'] = get_bloginfo( 'version' );
  140. $wp_data['multisite'] = is_multisite() ? 'Yes' : 'No';
  141. return $wp_data;
  142. }
  143. /**
  144. * Get server related info.
  145. * @return array
  146. */
  147. private static function get_server_info() {
  148. $server_data = array();
  149. if ( isset( $_SERVER['SERVER_SOFTWARE'] ) && ! empty( $_SERVER['SERVER_SOFTWARE'] ) ) {
  150. $server_data['software'] = $_SERVER['SERVER_SOFTWARE'];
  151. }
  152. if ( function_exists( 'phpversion' ) ) {
  153. $server_data['php_version'] = phpversion();
  154. }
  155. if ( function_exists( 'ini_get' ) ) {
  156. $server_data['php_post_max_size'] = size_format( wc_let_to_num( ini_get( 'post_max_size' ) ) );
  157. $server_data['php_time_limt'] = ini_get( 'max_execution_time' );
  158. $server_data['php_max_input_vars'] = ini_get( 'max_input_vars' );
  159. $server_data['php_suhosin'] = extension_loaded( 'suhosin' ) ? 'Yes' : 'No';
  160. }
  161. global $wpdb;
  162. $server_data['mysql_version'] = $wpdb->db_version();
  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. return $server_data;
  169. }
  170. /**
  171. * Get all plugins grouped into activated or not.
  172. * @return array
  173. */
  174. private static function get_all_plugins() {
  175. // Ensure get_plugins function is loaded
  176. if( ! function_exists( 'get_plugins' ) ) {
  177. include ABSPATH . '/wp-admin/includes/plugin.php';
  178. }
  179. $plugins = get_plugins();
  180. $active_plugins_keys = get_option( 'active_plugins', array() );
  181. $active_plugins = array();
  182. foreach ( $plugins as $k => $v ) {
  183. // Take care of formatting the data how we want it.
  184. $formatted = array();
  185. $formatted['name'] = strip_tags( $v['Name'] );
  186. if ( isset( $v['Version'] ) ) {
  187. $formatted['version'] = strip_tags( $v['Version'] );
  188. }
  189. if ( isset( $v['Author'] ) ) {
  190. $formatted['author'] = strip_tags( $v['Author'] );
  191. }
  192. if ( isset( $v['Network'] ) ) {
  193. $formatted['network'] = strip_tags( $v['Network'] );
  194. }
  195. if ( isset( $v['PluginURI'] ) ) {
  196. $formatted['plugin_uri'] = strip_tags( $v['PluginURI'] );
  197. }
  198. if ( in_array( $k, $active_plugins_keys ) ) {
  199. // Remove active plugins from list so we can show active and inactive separately
  200. unset( $plugins[$k] );
  201. $active_plugins[$k] = $formatted;
  202. } else {
  203. $plugins[$k] = $formatted;
  204. }
  205. }
  206. return array( 'active_plugins' => $active_plugins, 'inactive_plugins' => $plugins );
  207. }
  208. /**
  209. * Get user totals based on user role.
  210. * @return array
  211. */
  212. private static function get_user_counts() {
  213. $user_count = array();
  214. $user_count_data = count_users();
  215. $user_count['total'] = $user_count_data['total_users'];
  216. // Get user count based on user role
  217. foreach ( $user_count_data['avail_roles'] as $role => $count ) {
  218. $user_count[ $role ] = $count;
  219. }
  220. return $user_count;
  221. }
  222. /**
  223. * Get product totals based on product type.
  224. * @return array
  225. */
  226. private static function get_product_counts() {
  227. $product_count = array();
  228. $product_count_data = wp_count_posts( 'product' );
  229. $product_count['total'] = $product_count_data->publish;
  230. $product_statuses = get_terms( 'product_type', array( 'hide_empty' => 0 ) );
  231. foreach ( $product_statuses as $product_status ) {
  232. $product_count[ $product_status->name ] = $product_status->count;
  233. }
  234. return $product_count;
  235. }
  236. /**
  237. * Get order counts based on order status.
  238. * @return array
  239. */
  240. private static function get_order_counts() {
  241. $order_count = array();
  242. $order_count_data = wp_count_posts( 'shop_order' );
  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 ) && $gateway->enabled == 'yes' ) {
  257. $active_gateways[ $id ] = array( 'title' => $gateway->title, 'supports' => $gateway->supports );
  258. }
  259. }
  260. return $active_gateways;
  261. }
  262. /**
  263. * Get a list of all active shipping methods.
  264. * @return array
  265. */
  266. private static function get_active_shipping_methods() {
  267. $active_methods = array();
  268. $shipping_methods = WC()->shipping->get_shipping_methods();
  269. foreach ( $shipping_methods as $id => $shipping_method ) {
  270. if ( isset( $shipping_method->enabled ) && $shipping_method->enabled == 'yes' ) {
  271. $active_methods[ $id ] = array( 'title' => $shipping_method->title, 'tax_status' => $shipping_method->tax_status );
  272. }
  273. }
  274. return $active_methods;
  275. }
  276. /**
  277. * Get all options starting with woocommerce_ prefix.
  278. * @return array
  279. */
  280. private static function get_all_woocommerce_options_values() {
  281. return array(
  282. 'version' => WC()->version,
  283. 'currency' => get_woocommerce_currency(),
  284. 'base_location' => WC()->countries->get_base_country(),
  285. 'selling_locations' => WC()->countries->get_allowed_countries(),
  286. 'api_enabled' => get_option( 'woocommerce_api_enabled' ),
  287. 'weight_unit' => get_option( 'woocommerce_weight_unit' ),
  288. 'dimension_unit' => get_option( 'woocommerce_dimension_unit' ),
  289. 'download_method' => get_option( 'woocommerce_file_download_method' ),
  290. 'download_require_login' => get_option( 'woocommerce_downloads_require_login' ),
  291. 'calc_taxes' => get_option( 'woocommerce_calc_taxes' ),
  292. 'coupons_enabled' => get_option( 'woocommerce_enable_coupons' ),
  293. 'guest_checkout' => get_option( 'woocommerce_enable_guest_checkout'),
  294. 'secure_checkout' => get_option( 'woocommerce_force_ssl_checkout' ),
  295. 'enable_signup_and_login_from_checkout' => get_option( 'woocommerce_enable_signup_and_login_from_checkout' ),
  296. 'enable_myaccount_registration' => get_option( 'woocommerce_enable_myaccount_registration' ),
  297. 'registration_generate_username' => get_option( 'woocommerce_registration_generate_username' ),
  298. 'registration_generate_password' => get_option( 'woocommerce_registration_generate_password' ),
  299. );
  300. }
  301. /**
  302. * Look for any template override and return filenames.
  303. * @return array
  304. */
  305. private static function get_all_template_overrides() {
  306. $override_data = array();
  307. $template_paths = apply_filters( 'woocommerce_template_overrides_scan_paths', array( 'WooCommerce' => WC()->plugin_path() . '/templates/' ) );
  308. $scanned_files = array();
  309. require_once( WC()->plugin_path() . '/includes/admin/class-wc-admin-status.php' );
  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 ( $theme_file !== false ) {
  327. $override_data[] = basename( $theme_file );
  328. }
  329. }
  330. }
  331. return $override_data;
  332. }
  333. }
  334. WC_Tracker::init();