No Description

class-wc-tracker.php 23KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762
  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. use Automattic\Jetpack\Constants;
  13. defined( 'ABSPATH' ) || exit;
  14. /**
  15. * WooCommerce Tracker Class
  16. */
  17. class WC_Tracker {
  18. /**
  19. * URL to the WooThemes Tracker API endpoint.
  20. *
  21. * @var string
  22. */
  23. private static $api_url = 'https://tracking.woocommerce.com/v1/';
  24. /**
  25. * Hook into cron event.
  26. */
  27. public static function init() {
  28. add_action( 'woocommerce_tracker_send_event', array( __CLASS__, 'send_tracking_data' ) );
  29. }
  30. /**
  31. * Decide whether to send tracking data or not.
  32. *
  33. * @param boolean $override Should override?.
  34. */
  35. public static function send_tracking_data( $override = false ) {
  36. // Don't trigger this on AJAX Requests.
  37. if ( Constants::is_true( 'DOING_AJAX' ) ) {
  38. return;
  39. }
  40. if ( ! apply_filters( 'woocommerce_tracker_send_override', $override ) ) {
  41. // Send a maximum of once per week by default.
  42. $last_send = self::get_last_send_time();
  43. if ( $last_send && $last_send > apply_filters( 'woocommerce_tracker_last_send_interval', strtotime( '-1 week' ) ) ) {
  44. return;
  45. }
  46. } else {
  47. // Make sure there is at least a 1 hour delay between override sends, we don't want duplicate calls due to double clicking links.
  48. $last_send = self::get_last_send_time();
  49. if ( $last_send && $last_send > strtotime( '-1 hours' ) ) {
  50. return;
  51. }
  52. }
  53. // Update time first before sending to ensure it is set.
  54. update_option( 'woocommerce_tracker_last_send', time() );
  55. $params = self::get_tracking_data();
  56. wp_safe_remote_post(
  57. self::$api_url,
  58. array(
  59. 'method' => 'POST',
  60. 'timeout' => 45,
  61. 'redirection' => 5,
  62. 'httpversion' => '1.0',
  63. 'blocking' => false,
  64. 'headers' => array( 'user-agent' => 'WooCommerceTracker/' . md5( esc_url_raw( home_url( '/' ) ) ) . ';' ),
  65. 'body' => wp_json_encode( $params ),
  66. 'cookies' => array(),
  67. )
  68. );
  69. }
  70. /**
  71. * Get the last time tracking data was sent.
  72. *
  73. * @return int|bool
  74. */
  75. private static function get_last_send_time() {
  76. return apply_filters( 'woocommerce_tracker_last_send_time', get_option( 'woocommerce_tracker_last_send', false ) );
  77. }
  78. /**
  79. * Test whether this site is a staging site according to the Jetpack criteria.
  80. *
  81. * With Jetpack 8.1+, Jetpack::is_staging_site has been deprecated.
  82. * \Automattic\Jetpack\Status::is_staging_site is the replacement.
  83. * However, there are version of JP where \Automattic\Jetpack\Status exists, but does *not* contain is_staging_site method,
  84. * so with those, code still needs to use the previous check as a fallback.
  85. *
  86. * @return bool
  87. */
  88. private static function is_jetpack_staging_site() {
  89. if ( class_exists( '\Automattic\Jetpack\Status' ) ) {
  90. // Preferred way of checking with Jetpack 8.1+.
  91. $jp_status = new \Automattic\Jetpack\Status();
  92. if ( is_callable( array( $jp_status, 'is_staging_site' ) ) ) {
  93. return $jp_status->is_staging_site();
  94. }
  95. }
  96. return ( class_exists( 'Jetpack' ) && is_callable( 'Jetpack::is_staging_site' ) && Jetpack::is_staging_site() );
  97. }
  98. /**
  99. * Get all the tracking data.
  100. *
  101. * @return array
  102. */
  103. public static function get_tracking_data() {
  104. $data = array();
  105. // General site info.
  106. $data['url'] = home_url();
  107. $data['email'] = apply_filters( 'woocommerce_tracker_admin_email', get_option( 'admin_email' ) );
  108. $data['theme'] = self::get_theme_info();
  109. // WordPress Info.
  110. $data['wp'] = self::get_wordpress_info();
  111. // Server Info.
  112. $data['server'] = self::get_server_info();
  113. // Plugin info.
  114. $all_plugins = self::get_all_plugins();
  115. $data['active_plugins'] = $all_plugins['active_plugins'];
  116. $data['inactive_plugins'] = $all_plugins['inactive_plugins'];
  117. // Jetpack & WooCommerce Connect.
  118. $data['jetpack_version'] = Constants::is_defined( 'JETPACK__VERSION' ) ? Constants::get_constant( 'JETPACK__VERSION' ) : 'none';
  119. $data['jetpack_connected'] = ( class_exists( 'Jetpack' ) && is_callable( 'Jetpack::is_active' ) && Jetpack::is_active() ) ? 'yes' : 'no';
  120. $data['jetpack_is_staging'] = self::is_jetpack_staging_site() ? 'yes' : 'no';
  121. $data['connect_installed'] = class_exists( 'WC_Connect_Loader' ) ? 'yes' : 'no';
  122. $data['connect_active'] = ( class_exists( 'WC_Connect_Loader' ) && wp_next_scheduled( 'wc_connect_fetch_service_schemas' ) ) ? 'yes' : 'no';
  123. $data['helper_connected'] = self::get_helper_connected();
  124. // Store count info.
  125. $data['users'] = self::get_user_counts();
  126. $data['products'] = self::get_product_counts();
  127. $data['orders'] = self::get_orders();
  128. $data['reviews'] = self::get_review_counts();
  129. $data['categories'] = self::get_category_counts();
  130. // Payment gateway info.
  131. $data['gateways'] = self::get_active_payment_gateways();
  132. // Shipping method info.
  133. $data['shipping_methods'] = self::get_active_shipping_methods();
  134. // Get all WooCommerce options info.
  135. $data['settings'] = self::get_all_woocommerce_options_values();
  136. // Template overrides.
  137. $data['template_overrides'] = self::get_all_template_overrides();
  138. // Cart & checkout tech (blocks or shortcodes).
  139. $data['cart_checkout'] = self::get_cart_checkout_info();
  140. // WooCommerce Admin info.
  141. $data['wc_admin_disabled'] = apply_filters( 'woocommerce_admin_disabled', false ) ? 'yes' : 'no';
  142. return apply_filters( 'woocommerce_tracker_data', $data );
  143. }
  144. /**
  145. * Get the current theme info, theme name and version.
  146. *
  147. * @return array
  148. */
  149. public static function get_theme_info() {
  150. $theme_data = wp_get_theme();
  151. $theme_child_theme = wc_bool_to_string( is_child_theme() );
  152. $theme_wc_support = wc_bool_to_string( current_theme_supports( 'woocommerce' ) );
  153. return array(
  154. 'name' => $theme_data->Name, // @phpcs:ignore
  155. 'version' => $theme_data->Version, // @phpcs:ignore
  156. 'child_theme' => $theme_child_theme,
  157. 'wc_support' => $theme_wc_support,
  158. );
  159. }
  160. /**
  161. * Get WordPress related data.
  162. *
  163. * @return array
  164. */
  165. private static function get_wordpress_info() {
  166. $wp_data = array();
  167. $memory = wc_let_to_num( WP_MEMORY_LIMIT );
  168. if ( function_exists( 'memory_get_usage' ) ) {
  169. $system_memory = wc_let_to_num( @ini_get( 'memory_limit' ) );
  170. $memory = max( $memory, $system_memory );
  171. }
  172. // WordPress 5.5+ environment type specification.
  173. // 'production' is the default in WP, thus using it as a default here, too.
  174. $environment_type = 'production';
  175. if ( function_exists( 'wp_get_environment_type' ) ) {
  176. $environment_type = wp_get_environment_type();
  177. }
  178. $wp_data['memory_limit'] = size_format( $memory );
  179. $wp_data['debug_mode'] = ( defined( 'WP_DEBUG' ) && WP_DEBUG ) ? 'Yes' : 'No';
  180. $wp_data['locale'] = get_locale();
  181. $wp_data['version'] = get_bloginfo( 'version' );
  182. $wp_data['multisite'] = is_multisite() ? 'Yes' : 'No';
  183. $wp_data['env_type'] = $environment_type;
  184. return $wp_data;
  185. }
  186. /**
  187. * Get server related info.
  188. *
  189. * @return array
  190. */
  191. private static function get_server_info() {
  192. $server_data = array();
  193. if ( ! empty( $_SERVER['SERVER_SOFTWARE'] ) ) {
  194. $server_data['software'] = $_SERVER['SERVER_SOFTWARE']; // @phpcs:ignore
  195. }
  196. if ( function_exists( 'phpversion' ) ) {
  197. $server_data['php_version'] = phpversion();
  198. }
  199. if ( function_exists( 'ini_get' ) ) {
  200. $server_data['php_post_max_size'] = size_format( wc_let_to_num( ini_get( 'post_max_size' ) ) );
  201. $server_data['php_time_limt'] = ini_get( 'max_execution_time' );
  202. $server_data['php_max_input_vars'] = ini_get( 'max_input_vars' );
  203. $server_data['php_suhosin'] = extension_loaded( 'suhosin' ) ? 'Yes' : 'No';
  204. }
  205. $database_version = wc_get_server_database_version();
  206. $server_data['mysql_version'] = $database_version['number'];
  207. $server_data['php_max_upload_size'] = size_format( wp_max_upload_size() );
  208. $server_data['php_default_timezone'] = date_default_timezone_get();
  209. $server_data['php_soap'] = class_exists( 'SoapClient' ) ? 'Yes' : 'No';
  210. $server_data['php_fsockopen'] = function_exists( 'fsockopen' ) ? 'Yes' : 'No';
  211. $server_data['php_curl'] = function_exists( 'curl_init' ) ? 'Yes' : 'No';
  212. return $server_data;
  213. }
  214. /**
  215. * Get all plugins grouped into activated or not.
  216. *
  217. * @return array
  218. */
  219. private static function get_all_plugins() {
  220. // Ensure get_plugins function is loaded.
  221. if ( ! function_exists( 'get_plugins' ) ) {
  222. include ABSPATH . '/wp-admin/includes/plugin.php';
  223. }
  224. $plugins = get_plugins();
  225. $active_plugins_keys = get_option( 'active_plugins', array() );
  226. $active_plugins = array();
  227. foreach ( $plugins as $k => $v ) {
  228. // Take care of formatting the data how we want it.
  229. $formatted = array();
  230. $formatted['name'] = strip_tags( $v['Name'] );
  231. if ( isset( $v['Version'] ) ) {
  232. $formatted['version'] = strip_tags( $v['Version'] );
  233. }
  234. if ( isset( $v['Author'] ) ) {
  235. $formatted['author'] = strip_tags( $v['Author'] );
  236. }
  237. if ( isset( $v['Network'] ) ) {
  238. $formatted['network'] = strip_tags( $v['Network'] );
  239. }
  240. if ( isset( $v['PluginURI'] ) ) {
  241. $formatted['plugin_uri'] = strip_tags( $v['PluginURI'] );
  242. }
  243. if ( in_array( $k, $active_plugins_keys ) ) {
  244. // Remove active plugins from list so we can show active and inactive separately.
  245. unset( $plugins[ $k ] );
  246. $active_plugins[ $k ] = $formatted;
  247. } else {
  248. $plugins[ $k ] = $formatted;
  249. }
  250. }
  251. return array(
  252. 'active_plugins' => $active_plugins,
  253. 'inactive_plugins' => $plugins,
  254. );
  255. }
  256. /**
  257. * Check to see if the helper is connected to woocommerce.com
  258. *
  259. * @return string
  260. */
  261. private static function get_helper_connected() {
  262. if ( class_exists( 'WC_Helper_Options' ) && is_callable( 'WC_Helper_Options::get' ) ) {
  263. $authenticated = WC_Helper_Options::get( 'auth' );
  264. } else {
  265. $authenticated = '';
  266. }
  267. return ( ! empty( $authenticated ) ) ? 'yes' : 'no';
  268. }
  269. /**
  270. * Get user totals based on user role.
  271. *
  272. * @return array
  273. */
  274. private static function get_user_counts() {
  275. $user_count = array();
  276. $user_count_data = count_users();
  277. $user_count['total'] = $user_count_data['total_users'];
  278. // Get user count based on user role.
  279. foreach ( $user_count_data['avail_roles'] as $role => $count ) {
  280. $user_count[ $role ] = $count;
  281. }
  282. return $user_count;
  283. }
  284. /**
  285. * Get product totals based on product type.
  286. *
  287. * @return array
  288. */
  289. public static function get_product_counts() {
  290. $product_count = array();
  291. $product_count_data = wp_count_posts( 'product' );
  292. $product_count['total'] = $product_count_data->publish;
  293. $product_statuses = get_terms( 'product_type', array( 'hide_empty' => 0 ) );
  294. foreach ( $product_statuses as $product_status ) {
  295. $product_count[ $product_status->name ] = $product_status->count;
  296. }
  297. return $product_count;
  298. }
  299. /**
  300. * Get order counts.
  301. *
  302. * @return array
  303. */
  304. private static function get_order_counts() {
  305. $order_count = array();
  306. $order_count_data = wp_count_posts( 'shop_order' );
  307. foreach ( wc_get_order_statuses() as $status_slug => $status_name ) {
  308. $order_count[ $status_slug ] = $order_count_data->{ $status_slug };
  309. }
  310. return $order_count;
  311. }
  312. /**
  313. * Combine all order data.
  314. *
  315. * @return array
  316. */
  317. private static function get_orders() {
  318. $order_dates = self::get_order_dates();
  319. $order_counts = self::get_order_counts();
  320. $order_totals = self::get_order_totals();
  321. $order_gateways = self::get_orders_by_gateway();
  322. return array_merge( $order_dates, $order_counts, $order_totals, $order_gateways );
  323. }
  324. /**
  325. * Get order totals.
  326. *
  327. * @since 5.4.0
  328. * @return array
  329. */
  330. private static function get_order_totals() {
  331. global $wpdb;
  332. $gross_total = $wpdb->get_var(
  333. "
  334. SELECT
  335. SUM( order_meta.meta_value ) AS 'gross_total'
  336. FROM {$wpdb->prefix}posts AS orders
  337. LEFT JOIN {$wpdb->prefix}postmeta AS order_meta ON order_meta.post_id = orders.ID
  338. WHERE order_meta.meta_key = '_order_total'
  339. AND orders.post_status in ( 'wc-completed', 'wc-refunded' )
  340. GROUP BY order_meta.meta_key
  341. "
  342. );
  343. if ( is_null( $gross_total ) ) {
  344. $gross_total = 0;
  345. }
  346. $processing_gross_total = $wpdb->get_var(
  347. "
  348. SELECT
  349. SUM( order_meta.meta_value ) AS 'gross_total'
  350. FROM {$wpdb->prefix}posts AS orders
  351. LEFT JOIN {$wpdb->prefix}postmeta AS order_meta ON order_meta.post_id = orders.ID
  352. WHERE order_meta.meta_key = '_order_total'
  353. AND orders.post_status = 'wc-processing'
  354. GROUP BY order_meta.meta_key
  355. "
  356. );
  357. if ( is_null( $processing_gross_total ) ) {
  358. $processing_gross_total = 0;
  359. }
  360. return array(
  361. 'gross' => $gross_total,
  362. 'processing_gross' => $processing_gross_total,
  363. );
  364. }
  365. /**
  366. * Get last order date.
  367. *
  368. * @return string
  369. */
  370. private static function get_order_dates() {
  371. global $wpdb;
  372. $min_max = $wpdb->get_row(
  373. "
  374. SELECT
  375. MIN( post_date_gmt ) as 'first', MAX( post_date_gmt ) as 'last'
  376. FROM {$wpdb->prefix}posts
  377. WHERE post_type = 'shop_order'
  378. AND post_status = 'wc-completed'
  379. ",
  380. ARRAY_A
  381. );
  382. if ( is_null( $min_max ) ) {
  383. $min_max = array(
  384. 'first' => '-',
  385. 'last' => '-',
  386. );
  387. }
  388. $processing_min_max = $wpdb->get_row(
  389. "
  390. SELECT
  391. MIN( post_date_gmt ) as 'processing_first', MAX( post_date_gmt ) as 'processing_last'
  392. FROM {$wpdb->prefix}posts
  393. WHERE post_type = 'shop_order'
  394. AND post_status = 'wc-processing'
  395. ",
  396. ARRAY_A
  397. );
  398. if ( is_null( $processing_min_max ) ) {
  399. $processing_min_max = array(
  400. 'processing_first' => '-',
  401. 'processing_last' => '-',
  402. );
  403. }
  404. return array_merge( $min_max, $processing_min_max );
  405. }
  406. /**
  407. * Get order details by gateway.
  408. *
  409. * @return array
  410. */
  411. private static function get_orders_by_gateway() {
  412. global $wpdb;
  413. $orders_by_gateway = $wpdb->get_results(
  414. "
  415. SELECT
  416. gateway, currency, SUM(total) AS totals, COUNT(order_id) AS counts
  417. FROM (
  418. SELECT
  419. orders.id AS order_id,
  420. MAX(CASE WHEN meta_key = '_payment_method' THEN meta_value END) gateway,
  421. MAX(CASE WHEN meta_key = '_order_total' THEN meta_value END) total,
  422. MAX(CASE WHEN meta_key = '_order_currency' THEN meta_value END) currency
  423. FROM
  424. {$wpdb->prefix}posts orders
  425. LEFT JOIN
  426. {$wpdb->prefix}postmeta order_meta ON order_meta.post_id = orders.id
  427. WHERE orders.post_type = 'shop_order'
  428. AND orders.post_status in ( 'wc-completed', 'wc-processing', 'wc-refunded' )
  429. AND meta_key in( '_payment_method','_order_total','_order_currency')
  430. GROUP BY orders.id
  431. ) order_gateways
  432. GROUP BY gateway, currency
  433. "
  434. );
  435. $orders_by_gateway_currency = array();
  436. foreach ( $orders_by_gateway as $orders_details ) {
  437. $gateway = 'gateway_' . $orders_details->gateway;
  438. $currency = $orders_details->currency;
  439. $count = $gateway . '_' . $currency . '_count';
  440. $total = $gateway . '_' . $currency . '_total';
  441. $orders_by_gateway_currency[ $count ] = $orders_details->counts;
  442. $orders_by_gateway_currency[ $total ] = $orders_details->totals;
  443. }
  444. return $orders_by_gateway_currency;
  445. }
  446. /**
  447. * Get review counts for different statuses.
  448. *
  449. * @return array
  450. */
  451. private static function get_review_counts() {
  452. global $wpdb;
  453. $review_count = array( 'total' => 0 );
  454. $status_map = array(
  455. '0' => 'pending',
  456. '1' => 'approved',
  457. 'trash' => 'trash',
  458. 'spam' => 'spam',
  459. );
  460. $counts = $wpdb->get_results(
  461. "
  462. SELECT comment_approved, COUNT(*) AS num_reviews
  463. FROM {$wpdb->comments}
  464. WHERE comment_type = 'review'
  465. GROUP BY comment_approved
  466. ",
  467. ARRAY_A
  468. );
  469. if ( ! $counts ) {
  470. return $review_count;
  471. }
  472. foreach ( $counts as $count ) {
  473. $status = $count['comment_approved'];
  474. if ( array_key_exists( $status, $status_map ) ) {
  475. $review_count[ $status_map[ $status ] ] = $count['num_reviews'];
  476. }
  477. $review_count['total'] += $count['num_reviews'];
  478. }
  479. return $review_count;
  480. }
  481. /**
  482. * Get the number of product categories.
  483. *
  484. * @return int
  485. */
  486. private static function get_category_counts() {
  487. return wp_count_terms( 'product_cat' );
  488. }
  489. /**
  490. * Get a list of all active payment gateways.
  491. *
  492. * @return array
  493. */
  494. private static function get_active_payment_gateways() {
  495. $active_gateways = array();
  496. $gateways = WC()->payment_gateways->payment_gateways();
  497. foreach ( $gateways as $id => $gateway ) {
  498. if ( isset( $gateway->enabled ) && 'yes' === $gateway->enabled ) {
  499. $active_gateways[ $id ] = array(
  500. 'title' => $gateway->title,
  501. 'supports' => $gateway->supports,
  502. );
  503. }
  504. }
  505. return $active_gateways;
  506. }
  507. /**
  508. * Get a list of all active shipping methods.
  509. *
  510. * @return array
  511. */
  512. private static function get_active_shipping_methods() {
  513. $active_methods = array();
  514. $shipping_methods = WC()->shipping()->get_shipping_methods();
  515. foreach ( $shipping_methods as $id => $shipping_method ) {
  516. if ( isset( $shipping_method->enabled ) && 'yes' === $shipping_method->enabled ) {
  517. $active_methods[ $id ] = array(
  518. 'title' => $shipping_method->title,
  519. 'tax_status' => $shipping_method->tax_status,
  520. );
  521. }
  522. }
  523. return $active_methods;
  524. }
  525. /**
  526. * Get all options starting with woocommerce_ prefix.
  527. *
  528. * @return array
  529. */
  530. private static function get_all_woocommerce_options_values() {
  531. return array(
  532. 'version' => WC()->version,
  533. 'currency' => get_woocommerce_currency(),
  534. 'base_location' => WC()->countries->get_base_country(),
  535. 'base_state' => WC()->countries->get_base_state(),
  536. 'base_postcode' => WC()->countries->get_base_postcode(),
  537. 'selling_locations' => WC()->countries->get_allowed_countries(),
  538. 'api_enabled' => get_option( 'woocommerce_api_enabled' ),
  539. 'weight_unit' => get_option( 'woocommerce_weight_unit' ),
  540. 'dimension_unit' => get_option( 'woocommerce_dimension_unit' ),
  541. 'download_method' => get_option( 'woocommerce_file_download_method' ),
  542. 'download_require_login' => get_option( 'woocommerce_downloads_require_login' ),
  543. 'calc_taxes' => get_option( 'woocommerce_calc_taxes' ),
  544. 'coupons_enabled' => get_option( 'woocommerce_enable_coupons' ),
  545. 'guest_checkout' => get_option( 'woocommerce_enable_guest_checkout' ),
  546. 'checkout_login_reminder' => get_option( 'woocommerce_enable_checkout_login_reminder' ),
  547. 'secure_checkout' => get_option( 'woocommerce_force_ssl_checkout' ),
  548. 'enable_signup_and_login_from_checkout' => get_option( 'woocommerce_enable_signup_and_login_from_checkout' ),
  549. 'enable_myaccount_registration' => get_option( 'woocommerce_enable_myaccount_registration' ),
  550. 'registration_generate_username' => get_option( 'woocommerce_registration_generate_username' ),
  551. 'registration_generate_password' => get_option( 'woocommerce_registration_generate_password' ),
  552. );
  553. }
  554. /**
  555. * Look for any template override and return filenames.
  556. *
  557. * @return array
  558. */
  559. private static function get_all_template_overrides() {
  560. $override_data = array();
  561. $template_paths = apply_filters( 'woocommerce_template_overrides_scan_paths', array( 'WooCommerce' => WC()->plugin_path() . '/templates/' ) );
  562. $scanned_files = array();
  563. require_once WC()->plugin_path() . '/includes/admin/class-wc-admin-status.php';
  564. foreach ( $template_paths as $plugin_name => $template_path ) {
  565. $scanned_files[ $plugin_name ] = WC_Admin_Status::scan_template_files( $template_path );
  566. }
  567. foreach ( $scanned_files as $plugin_name => $files ) {
  568. foreach ( $files as $file ) {
  569. if ( file_exists( get_stylesheet_directory() . '/' . $file ) ) {
  570. $theme_file = get_stylesheet_directory() . '/' . $file;
  571. } elseif ( file_exists( get_stylesheet_directory() . '/' . WC()->template_path() . $file ) ) {
  572. $theme_file = get_stylesheet_directory() . '/' . WC()->template_path() . $file;
  573. } elseif ( file_exists( get_template_directory() . '/' . $file ) ) {
  574. $theme_file = get_template_directory() . '/' . $file;
  575. } elseif ( file_exists( get_template_directory() . '/' . WC()->template_path() . $file ) ) {
  576. $theme_file = get_template_directory() . '/' . WC()->template_path() . $file;
  577. } else {
  578. $theme_file = false;
  579. }
  580. if ( false !== $theme_file ) {
  581. $override_data[] = basename( $theme_file );
  582. }
  583. }
  584. }
  585. return $override_data;
  586. }
  587. /**
  588. * Search a specific post for text content.
  589. *
  590. * @param integer $post_id The id of the post to search.
  591. * @param string $text The text to search for.
  592. * @return string 'Yes' if post contains $text (otherwise 'No').
  593. */
  594. public static function post_contains_text( $post_id, $text ) {
  595. global $wpdb;
  596. // Search for the text anywhere in the post.
  597. $wildcarded = "%{$text}%";
  598. $result = $wpdb->get_var(
  599. $wpdb->prepare(
  600. "
  601. SELECT COUNT( * ) FROM {$wpdb->prefix}posts
  602. WHERE ID=%d
  603. AND {$wpdb->prefix}posts.post_content LIKE %s
  604. ",
  605. array( $post_id, $wildcarded )
  606. )
  607. );
  608. return ( '0' !== $result ) ? 'Yes' : 'No';
  609. }
  610. /**
  611. * Get tracker data for a specific block type on a woocommerce page.
  612. *
  613. * @param string $block_name The name (id) of a block, e.g. `woocommerce/cart`.
  614. * @param string $woo_page_name The woo page to search, e.g. `cart`.
  615. * @return array Associative array of tracker data with keys:
  616. * - page_contains_block
  617. * - block_attributes
  618. */
  619. public static function get_block_tracker_data( $block_name, $woo_page_name ) {
  620. $blocks = WC_Blocks_Utils::get_blocks_from_page( $block_name, $woo_page_name );
  621. $block_present = false;
  622. $attributes = array();
  623. if ( $blocks && count( $blocks ) ) {
  624. // Return any customised attributes from the first block.
  625. $block_present = true;
  626. $attributes = $blocks[0]['attrs'];
  627. }
  628. return array(
  629. 'page_contains_block' => $block_present ? 'Yes' : 'No',
  630. 'block_attributes' => $attributes,
  631. );
  632. }
  633. /**
  634. * Get info about the cart & checkout pages.
  635. *
  636. * @return array
  637. */
  638. public static function get_cart_checkout_info() {
  639. $cart_page_id = wc_get_page_id( 'cart' );
  640. $checkout_page_id = wc_get_page_id( 'checkout' );
  641. $cart_block_data = self::get_block_tracker_data( 'woocommerce/cart', 'cart' );
  642. $checkout_block_data = self::get_block_tracker_data( 'woocommerce/checkout', 'checkout' );
  643. return array(
  644. 'cart_page_contains_cart_shortcode' => self::post_contains_text(
  645. $cart_page_id,
  646. '[woocommerce_cart]'
  647. ),
  648. 'checkout_page_contains_checkout_shortcode' => self::post_contains_text(
  649. $checkout_page_id,
  650. '[woocommerce_checkout]'
  651. ),
  652. 'cart_page_contains_cart_block' => $cart_block_data['page_contains_block'],
  653. 'cart_block_attributes' => $cart_block_data['block_attributes'],
  654. 'checkout_page_contains_checkout_block' => $checkout_block_data['page_contains_block'],
  655. 'checkout_block_attributes' => $checkout_block_data['block_attributes'],
  656. );
  657. }
  658. }
  659. WC_Tracker::init();