WC_Tracks_Client::maybe_set_identity_cookie
Check if identity cookie is set, if not set it.
Method of the class: WC_Tracks_Client{}
No Hooks.
Returns
null. Nothing (null).
Usage
$result = WC_Tracks_Client::maybe_set_identity_cookie();
WC_Tracks_Client::maybe_set_identity_cookie() WC Tracks Client::maybe set identity cookie code WC 10.8.1
public static function maybe_set_identity_cookie() {
// Do not set on AJAX requests.
if ( Constants::is_true( 'DOING_AJAX' ) ) {
return;
}
// Bail if cookie already set.
if ( isset( $_COOKIE['tk_ai'] ) ) {
return;
}
$user = wp_get_current_user();
// We don't want to track user events during unit tests/CI runs.
if ( $user instanceof WP_User && 'wptests_capabilities' === $user->cap_key ) {
return false;
}
$user_id = $user->ID;
$anon_id = get_user_meta( $user_id, '_woocommerce_tracks_anon_id', true );
// If an id is still not found, create one and save it.
if ( ! $anon_id ) {
$anon_id = self::get_anon_id();
update_user_meta( $user_id, '_woocommerce_tracks_anon_id', $anon_id );
}
// Don't set cookie on API requests.
if ( ! Constants::is_true( 'REST_REQUEST' ) && ! Constants::is_true( 'XMLRPC_REQUEST' ) ) {
WC_Site_Tracking::set_tracking_cookie( 'tk_ai', $anon_id );
}
}