Нет описания

class-wc-tracks.php 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. /**
  3. * PHP Tracks Client
  4. *
  5. * @package WooCommerce\Tracks
  6. */
  7. /**
  8. * WC_Tracks class.
  9. */
  10. class WC_Tracks {
  11. /**
  12. * Tracks event name prefix.
  13. */
  14. const PREFIX = 'wcadmin_';
  15. /**
  16. * Get total product counts.
  17. *
  18. * @return int Number of products.
  19. */
  20. public static function get_products_count() {
  21. $product_counts = WC_Tracker::get_product_counts();
  22. return $product_counts['total'];
  23. }
  24. /**
  25. * Gather blog related properties.
  26. *
  27. * @param int $user_id User id.
  28. * @return array Blog details.
  29. */
  30. public static function get_blog_details( $user_id ) {
  31. $blog_details = get_transient( 'wc_tracks_blog_details' );
  32. if ( false === $blog_details ) {
  33. $blog_details = array(
  34. 'url' => home_url(),
  35. 'blog_lang' => get_user_locale( $user_id ),
  36. 'blog_id' => class_exists( 'Jetpack_Options' ) ? Jetpack_Options::get_option( 'id' ) : null,
  37. 'products_count' => self::get_products_count(),
  38. 'wc_version' => WC()->version,
  39. );
  40. set_transient( 'wc_tracks_blog_details', $blog_details, DAY_IN_SECONDS );
  41. }
  42. return $blog_details;
  43. }
  44. /**
  45. * Gather details from the request to the server.
  46. *
  47. * @return array Server details.
  48. */
  49. public static function get_server_details() {
  50. $data = array();
  51. $data['_via_ua'] = isset( $_SERVER['HTTP_USER_AGENT'] ) ? wc_clean( wp_unslash( $_SERVER['HTTP_USER_AGENT'] ) ) : '';
  52. $data['_via_ip'] = isset( $_SERVER['REMOTE_ADDR'] ) ? wc_clean( wp_unslash( $_SERVER['REMOTE_ADDR'] ) ) : '';
  53. $data['_lg'] = isset( $_SERVER['HTTP_ACCEPT_LANGUAGE'] ) ? wc_clean( wp_unslash( $_SERVER['HTTP_ACCEPT_LANGUAGE'] ) ) : '';
  54. $data['_dr'] = isset( $_SERVER['HTTP_REFERER'] ) ? wc_clean( wp_unslash( $_SERVER['HTTP_REFERER'] ) ) : '';
  55. $uri = isset( $_SERVER['REQUEST_URI'] ) ? wc_clean( wp_unslash( $_SERVER['REQUEST_URI'] ) ) : '';
  56. $host = isset( $_SERVER['HTTP_HOST'] ) ? wc_clean( wp_unslash( $_SERVER['HTTP_HOST'] ) ) : '';
  57. $data['_dl'] = isset( $_SERVER['REQUEST_SCHEME'] ) ? wc_clean( wp_unslash( $_SERVER['REQUEST_SCHEME'] ) ) . '://' . $host . $uri : '';
  58. return $data;
  59. }
  60. /**
  61. * Record an event in Tracks - this is the preferred way to record events from PHP.
  62. *
  63. * @param string $event_name The name of the event.
  64. * @param array $properties Custom properties to send with the event.
  65. * @return bool|WP_Error True for success or WP_Error if the event pixel could not be fired.
  66. */
  67. public static function record_event( $event_name, $properties = array() ) {
  68. /**
  69. * Don't track users who don't have tracking enabled.
  70. */
  71. if ( ! WC_Site_Tracking::is_tracking_enabled() ) {
  72. return false;
  73. }
  74. $user = wp_get_current_user();
  75. // We don't want to track user events during unit tests/CI runs.
  76. if ( $user instanceof WP_User && 'wptests_capabilities' === $user->cap_key ) {
  77. return false;
  78. }
  79. $prefixed_event_name = self::PREFIX . $event_name;
  80. $data = array(
  81. '_en' => $prefixed_event_name,
  82. '_ts' => WC_Tracks_Client::build_timestamp(),
  83. );
  84. $server_details = self::get_server_details();
  85. $identity = WC_Tracks_Client::get_identity( $user->ID );
  86. $blog_details = self::get_blog_details( $user->ID );
  87. // Allow event props to be filtered to enable adding site-wide props.
  88. $filtered_properties = apply_filters( 'woocommerce_tracks_event_properties', $properties, $prefixed_event_name );
  89. // Delete _ui and _ut protected properties.
  90. unset( $filtered_properties['_ui'] );
  91. unset( $filtered_properties['_ut'] );
  92. $event_obj = new WC_Tracks_Event( array_merge( $data, $server_details, $identity, $blog_details, $filtered_properties ) );
  93. if ( is_wp_error( $event_obj->error ) ) {
  94. return $event_obj->error;
  95. }
  96. return $event_obj->record();
  97. }
  98. }