暂无描述

class-wc-tracks-client.php 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. <?php
  2. /**
  3. * Send Tracks events on behalf of a user.
  4. *
  5. * @package WooCommerce\Tracks
  6. */
  7. use Automattic\Jetpack\Constants;
  8. use Automattic\WooCommerce\Utilities\NumberUtil;
  9. defined( 'ABSPATH' ) || exit;
  10. /**
  11. * WC_Tracks_Client class.
  12. */
  13. class WC_Tracks_Client {
  14. /**
  15. * Pixel URL.
  16. */
  17. const PIXEL = 'https://pixel.wp.com/t.gif';
  18. /**
  19. * Browser type.
  20. */
  21. const BROWSER_TYPE = 'php-agent';
  22. /**
  23. * User agent.
  24. */
  25. const USER_AGENT_SLUG = 'tracks-client';
  26. /**
  27. * Initialize tracks client class
  28. *
  29. * @return void
  30. */
  31. public static function init() {
  32. // Use wp hook for setting the identity cookie to avoid headers already sent warnings.
  33. add_action( 'admin_init', array( __CLASS__, 'maybe_set_identity_cookie' ) );
  34. }
  35. /**
  36. * Check if identiy cookie is set, if not set it.
  37. *
  38. * @return void
  39. */
  40. public static function maybe_set_identity_cookie() {
  41. // Do not set on AJAX requests.
  42. if ( Constants::is_true( 'DOING_AJAX' ) ) {
  43. return;
  44. }
  45. // Bail if cookie already set.
  46. if ( isset( $_COOKIE['tk_ai'] ) ) {
  47. return;
  48. }
  49. $user = wp_get_current_user();
  50. // We don't want to track user events during unit tests/CI runs.
  51. if ( $user instanceof WP_User && 'wptests_capabilities' === $user->cap_key ) {
  52. return false;
  53. }
  54. $user_id = $user->ID;
  55. $anon_id = get_user_meta( $user_id, '_woocommerce_tracks_anon_id', true );
  56. // If an id is still not found, create one and save it.
  57. if ( ! $anon_id ) {
  58. $anon_id = self::get_anon_id();
  59. update_user_meta( $user_id, '_woocommerce_tracks_anon_id', $anon_id );
  60. }
  61. // Don't set cookie on API requests.
  62. if ( ! Constants::is_true( 'REST_REQUEST' ) && ! Constants::is_true( 'XMLRPC_REQUEST' ) ) {
  63. wc_setcookie( 'tk_ai', $anon_id );
  64. }
  65. }
  66. /**
  67. * Record a Tracks event
  68. *
  69. * @param array $event Array of event properties.
  70. * @return bool|WP_Error True on success, WP_Error on failure.
  71. */
  72. public static function record_event( $event ) {
  73. if ( ! $event instanceof WC_Tracks_Event ) {
  74. $event = new WC_Tracks_Event( $event );
  75. }
  76. if ( is_wp_error( $event ) ) {
  77. return $event;
  78. }
  79. $pixel = $event->build_pixel_url( $event );
  80. if ( ! $pixel ) {
  81. return new WP_Error( 'invalid_pixel', 'cannot generate tracks pixel for given input', 400 );
  82. }
  83. return self::record_pixel( $pixel );
  84. }
  85. /**
  86. * Synchronously request the pixel.
  87. *
  88. * @param string $pixel pixel url and query string.
  89. * @return bool Always returns true.
  90. */
  91. public static function record_pixel( $pixel ) {
  92. // Add the Request Timestamp and URL terminator just before the HTTP request.
  93. $pixel .= '&_rt=' . self::build_timestamp() . '&_=_';
  94. wp_safe_remote_get(
  95. $pixel,
  96. array(
  97. 'blocking' => false,
  98. 'redirection' => 2,
  99. 'httpversion' => '1.1',
  100. 'timeout' => 1,
  101. )
  102. );
  103. return true;
  104. }
  105. /**
  106. * Create a timestap representing milliseconds since 1970-01-01
  107. *
  108. * @return string A string representing a timestamp.
  109. */
  110. public static function build_timestamp() {
  111. $ts = NumberUtil::round( microtime( true ) * 1000 );
  112. return number_format( $ts, 0, '', '' );
  113. }
  114. /**
  115. * Get a user's identity to send to Tracks. If Jetpack exists, default to its implementation.
  116. *
  117. * @param int $user_id User id.
  118. * @return array Identity properties.
  119. */
  120. public static function get_identity( $user_id ) {
  121. $jetpack_lib = '/tracks/client.php';
  122. if ( class_exists( 'Jetpack' ) && Constants::is_defined( 'JETPACK__VERSION' ) ) {
  123. if ( version_compare( Constants::get_constant( 'JETPACK__VERSION' ), '7.5', '<' ) ) {
  124. if ( file_exists( jetpack_require_lib_dir() . $jetpack_lib ) ) {
  125. include_once jetpack_require_lib_dir() . $jetpack_lib;
  126. if ( function_exists( 'jetpack_tracks_get_identity' ) ) {
  127. return jetpack_tracks_get_identity( $user_id );
  128. }
  129. }
  130. } else {
  131. $tracking = new Automattic\Jetpack\Tracking();
  132. return $tracking->tracks_get_identity( $user_id );
  133. }
  134. }
  135. // Start with a previously set cookie.
  136. $anon_id = isset( $_COOKIE['tk_ai'] ) ? sanitize_text_field( wp_unslash( $_COOKIE['tk_ai'] ) ) : false;
  137. // If there is no cookie, apply a saved id.
  138. if ( ! $anon_id ) {
  139. $anon_id = get_user_meta( $user_id, '_woocommerce_tracks_anon_id', true );
  140. }
  141. // If an id is still not found, create one and save it.
  142. if ( ! $anon_id ) {
  143. $anon_id = self::get_anon_id();
  144. update_user_meta( $user_id, '_woocommerce_tracks_anon_id', $anon_id );
  145. }
  146. return array(
  147. '_ut' => 'anon',
  148. '_ui' => $anon_id,
  149. );
  150. }
  151. /**
  152. * Grabs the user's anon id from cookies, or generates and sets a new one
  153. *
  154. * @return string An anon id for the user
  155. */
  156. public static function get_anon_id() {
  157. static $anon_id = null;
  158. if ( ! isset( $anon_id ) ) {
  159. // Did the browser send us a cookie?
  160. if ( isset( $_COOKIE['tk_ai'] ) ) {
  161. $anon_id = sanitize_text_field( wp_unslash( $_COOKIE['tk_ai'] ) );
  162. } else {
  163. $binary = '';
  164. // Generate a new anonId and try to save it in the browser's cookies.
  165. // Note that base64-encoding an 18 character string generates a 24-character anon id.
  166. for ( $i = 0; $i < 18; ++$i ) {
  167. $binary .= chr( wp_rand( 0, 255 ) );
  168. }
  169. // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode
  170. $anon_id = 'woo:' . base64_encode( $binary );
  171. }
  172. }
  173. return $anon_id;
  174. }
  175. }
  176. WC_Tracks_Client::init();