Instantiate a Jetpack_Tracks_Event object instead * @param array $event Event keys and values. * @return mixed Validated keys and values or WP_Error on failure */ private static function validate_and_sanitize( $event ) { $_event = new Jetpack_Tracks_Event( $event ); if ( is_wp_error( $_event ) ) { return $_event; } return get_object_vars( $_event ); } /** * Builds a timestamp. * * Milliseconds since 1970-01-01. * * @return string */ public static function build_timestamp() { $ts = round( microtime( true ) * 1000 ); return number_format( $ts, 0, '', '' ); } /** * Grabs the user's anon id from cookies, or generates and sets a new one * * @return string An anon id for the user */ public static function get_anon_id() { static $anon_id = null; if ( ! isset( $anon_id ) ) { // Did the browser send us a cookie? if ( isset( $_COOKIE['tk_ai'] ) && preg_match( '#^[a-z]+:[A-Za-z0-9+/=]{24}$#', $_COOKIE['tk_ai'] ) ) { // phpcs:ignore WordPress.Security.ValidatedSanitizedInput -- This is validating. $anon_id = $_COOKIE['tk_ai']; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput -- This is validating. } else { $binary = ''; // Generate a new anonId and try to save it in the browser's cookies. // Note that base64-encoding an 18 character string generates a 24-character anon id. for ( $i = 0; $i < 18; ++$i ) { $binary .= chr( wp_rand( 0, 255 ) ); } $anon_id = 'jetpack:' . base64_encode( $binary ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode if ( ! headers_sent() && ! ( defined( 'REST_REQUEST' ) && REST_REQUEST ) && ! ( defined( 'XMLRPC_REQUEST' ) && XMLRPC_REQUEST ) ) { setcookie( 'tk_ai', $anon_id, 0, COOKIEPATH, COOKIE_DOMAIN, is_ssl(), false ); // phpcs:ignore Jetpack.Functions.SetCookie -- This is a random value and should be fine. } } } return $anon_id; } /** * Gets the WordPress.com user's Tracks identity, if connected. * * @return array|bool */ public static function get_connected_user_tracks_identity() { $user_data = ( new Manager() )->get_connected_user_data(); if ( ! $user_data ) { return false; } return array( 'blogid' => Jetpack_Options::get_option( 'id', 0 ), 'email' => $user_data['email'], 'userid' => $user_data['ID'], 'username' => $user_data['login'], 'user_locale' => $user_data['user_locale'], ); } }