暂无描述

class-wc-site-tracking.php 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?php
  2. /**
  3. * Nosara Tracks for WooCommerce
  4. *
  5. * @package WooCommerce\Tracks
  6. */
  7. defined( 'ABSPATH' ) || exit;
  8. /**
  9. * This class adds actions to track usage of WooCommerce.
  10. */
  11. class WC_Site_Tracking {
  12. /**
  13. * Check if tracking is enabled.
  14. *
  15. * @return bool
  16. */
  17. public static function is_tracking_enabled() {
  18. /**
  19. * Don't track users if a filter has been applied to turn it off.
  20. * `woocommerce_apply_tracking` will be deprecated. Please use
  21. * `woocommerce_apply_user_tracking` instead.
  22. */
  23. if ( ! apply_filters( 'woocommerce_apply_user_tracking', true ) || ! apply_filters( 'woocommerce_apply_tracking', true ) ) {
  24. return false;
  25. }
  26. // Check if tracking is actively being opted into.
  27. $is_obw_opting_in = isset( $_POST['wc_tracker_checkbox'] ) && 'yes' === sanitize_text_field( $_POST['wc_tracker_checkbox'] ); // phpcs:ignore WordPress.Security.NonceVerification.Missing, WordPress.Security.ValidatedSanitizedInput
  28. /**
  29. * Don't track users who haven't opted-in to tracking or aren't in
  30. * the process of opting-in.
  31. */
  32. if ( 'yes' !== get_option( 'woocommerce_allow_tracking' ) && ! $is_obw_opting_in ) {
  33. return false;
  34. }
  35. if ( ! class_exists( 'WC_Tracks' ) ) {
  36. return false;
  37. }
  38. return true;
  39. }
  40. /**
  41. * Register scripts required to record events from javascript.
  42. */
  43. public static function register_scripts() {
  44. wp_register_script( 'woo-tracks', 'https://stats.wp.com/w.js', array( 'wp-hooks' ), gmdate( 'YW' ), false );
  45. }
  46. /**
  47. * Add scripts required to record events from javascript.
  48. */
  49. public static function enqueue_scripts() {
  50. wp_enqueue_script( 'woo-tracks' );
  51. }
  52. /**
  53. * Adds the tracking function to the admin footer.
  54. */
  55. public static function add_tracking_function() {
  56. ?>
  57. <!-- WooCommerce Tracks -->
  58. <script type="text/javascript">
  59. window.wcTracks = window.wcTracks || {};
  60. window.wcTracks.isEnabled = <?php echo self::is_tracking_enabled() ? 'true' : 'false'; ?>;
  61. window.wcTracks.recordEvent = function( name, properties ) {
  62. if ( ! window.wcTracks.isEnabled ) {
  63. return;
  64. }
  65. var eventName = '<?php echo esc_attr( WC_Tracks::PREFIX ); ?>' + name;
  66. var eventProperties = properties || {};
  67. eventProperties.url = '<?php echo esc_html( home_url() ); ?>'
  68. eventProperties.products_count = '<?php echo intval( WC_Tracks::get_products_count() ); ?>';
  69. if ( window.wp && window.wp.hooks && window.wp.hooks.applyFilters ) {
  70. eventProperties = window.wp.hooks.applyFilters( 'woocommerce_tracks_client_event_properties', eventProperties, eventName );
  71. delete( eventProperties._ui );
  72. delete( eventProperties._ut );
  73. }
  74. window._tkq = window._tkq || [];
  75. window._tkq.push( [ 'recordEvent', eventName, eventProperties ] );
  76. }
  77. </script>
  78. <?php
  79. }
  80. /**
  81. * Adds a function to load tracking scripts and enable them client-side on the fly.
  82. * Note that this function does not update `woocommerce_allow_tracking` in the database
  83. * and will not persist enabled tracking across page loads.
  84. */
  85. public static function add_enable_tracking_function() {
  86. global $wp_scripts;
  87. if ( ! isset( $wp_scripts->registered['woo-tracks'] ) ) {
  88. return;
  89. }
  90. $woo_tracks_script = $wp_scripts->registered['woo-tracks']->src;
  91. ?>
  92. <script type="text/javascript">
  93. window.wcTracks.enable = function( callback ) {
  94. window.wcTracks.isEnabled = true;
  95. var scriptUrl = '<?php echo esc_url( $woo_tracks_script ); ?>';
  96. var existingScript = document.querySelector( `script[src="${ scriptUrl }"]` );
  97. if ( existingScript ) {
  98. return;
  99. }
  100. var script = document.createElement('script');
  101. script.src = scriptUrl;
  102. document.body.append(script);
  103. // Callback after scripts have loaded.
  104. script.onload = function() {
  105. if ( 'function' === typeof callback ) {
  106. callback( true );
  107. }
  108. }
  109. // Callback triggered if the script fails to load.
  110. script.onerror = function() {
  111. if ( 'function' === typeof callback ) {
  112. callback( false );
  113. }
  114. }
  115. }
  116. </script>
  117. <?php
  118. }
  119. /**
  120. * Init tracking.
  121. */
  122. public static function init() {
  123. // Define window.wcTracks.recordEvent in case it is enabled client-side.
  124. self::register_scripts();
  125. add_filter( 'admin_footer', array( __CLASS__, 'add_tracking_function' ), 24 );
  126. if ( ! self::is_tracking_enabled() ) {
  127. add_filter( 'admin_footer', array( __CLASS__, 'add_enable_tracking_function' ), 24 );
  128. return;
  129. }
  130. self::enqueue_scripts();
  131. include_once WC_ABSPATH . 'includes/tracks/events/class-wc-admin-setup-wizard-tracking.php';
  132. include_once WC_ABSPATH . 'includes/tracks/events/class-wc-extensions-tracking.php';
  133. include_once WC_ABSPATH . 'includes/tracks/events/class-wc-importer-tracking.php';
  134. include_once WC_ABSPATH . 'includes/tracks/events/class-wc-products-tracking.php';
  135. include_once WC_ABSPATH . 'includes/tracks/events/class-wc-orders-tracking.php';
  136. include_once WC_ABSPATH . 'includes/tracks/events/class-wc-settings-tracking.php';
  137. include_once WC_ABSPATH . 'includes/tracks/events/class-wc-status-tracking.php';
  138. include_once WC_ABSPATH . 'includes/tracks/events/class-wc-coupons-tracking.php';
  139. include_once WC_ABSPATH . 'includes/tracks/events/class-wc-order-tracking.php';
  140. include_once WC_ABSPATH . 'includes/tracks/events/class-wc-coupon-tracking.php';
  141. $tracking_classes = array(
  142. 'WC_Extensions_Tracking',
  143. 'WC_Importer_Tracking',
  144. 'WC_Products_Tracking',
  145. 'WC_Orders_Tracking',
  146. 'WC_Settings_Tracking',
  147. 'WC_Status_Tracking',
  148. 'WC_Coupons_Tracking',
  149. 'WC_Order_Tracking',
  150. 'WC_Coupon_Tracking',
  151. );
  152. foreach ( $tracking_classes as $tracking_class ) {
  153. $tracker_instance = new $tracking_class();
  154. $tracker_init_method = array( $tracker_instance, 'init' );
  155. if ( is_callable( $tracker_init_method ) ) {
  156. call_user_func( $tracker_init_method );
  157. }
  158. }
  159. }
  160. }