WC_Tracks_Client::get_anon_id()
Grabs the user’s anon id from cookies, or generates and sets a new one
Description Description
Return Return
(string) An anon id for the user
Source Source
File: includes/tracks/class-wc-tracks-client.php
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'] ) ) {
$anon_id = sanitize_text_field( wp_unslash( $_COOKIE['tk_ai'] ) );
} 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 = 'woo:' . base64_encode( $binary );
}
}
return $anon_id;
}