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