Action Since 1.4.0 uncanny-learndash-toolkit

set_user_switching_cookie

Fires immediately before the User Switching authentication cookie is set. Fires before the User Switching authentication cookie is set, providing cookie details and user information.

add_action( 'set_user_switching_cookie', $callback, 10, 5 );

Description

Fires just before the user switching authentication cookie is set. Developers can hook into this action to modify or inspect the cookie's values, including the authentication cookie, expiration time, old user ID, authentication scheme, and session token, before it's sent to the browser.


Usage

add_action( 'set_user_switching_cookie', 'your_function_name', 10, 5 );

Parameters

$auth_cookie (string)
JSON-encoded array of authentication cookie values.
$expiration (int)
The time when the authentication cookie expires as a UNIX timestamp.
$old_user_id (int)
User ID.
$scheme (string)
Authentication scheme. Values include 'auth' or 'secure_auth'.
$token (string)
User's session token to use for the latest cookie.

Examples

add_action( 'set_user_switching_cookie', 'my_custom_user_switching_cookie_handler', 10, 5 );

/**
 * Example handler for the 'set_user_switching_cookie' action.
 *
 * This function demonstrates how to hook into the action that fires
 * before the user switching authentication cookie is set. It logs
 * the cookie details for debugging purposes.
 *
 * @param string $auth_cookie JSON-encoded array of authentication cookie values.
 * @param int    $expiration  The time when the authentication cookie expires as a UNIX timestamp.
 * @param int    $old_user_id User ID.
 * @param string $scheme      Authentication scheme. Values include 'auth' or 'secure_auth'.
 * @param string $token       User's session token to use for the latest cookie.
 */
function my_custom_user_switching_cookie_handler( $auth_cookie, $expiration, $old_user_id, $scheme, $token ) {
    // Decode the JSON cookie data to access individual parts if needed.
    $decoded_auth_cookie = json_decode( $auth_cookie, true );

    // For demonstration, let's log the details.
    // In a real scenario, you might perform other actions like updating a log,
    // sending a notification, or performing security checks.
    error_log( sprintf(
        'User Switching: Authentication cookie is being set. User ID: %d, Scheme: %s, Expiration: %s, Token: %s, Cookie Data: %s',
        $old_user_id,
        $scheme,
        date( 'Y-m-d H:i:s', $expiration ),
        $token,
        print_r( $decoded_auth_cookie, true )
    ) );

    // No return value is needed for an action hook unless you intend to filter
    // the parameters by modifying them before they are used by subsequent hooks,
    // which is not the typical use case for 'set_user_switching_cookie' itself.
}

Placement

This code should be placed in the functions.php file of your active theme, a custom plugin, or using a code snippets plugin.


Source Code

src/includes/user-switching.php:1347

function user_switching_set_olduser_cookie( $old_user_id, $pop = false, $token = '' ) {
		$secure_auth_cookie    = user_switching::secure_auth_cookie();
		$secure_olduser_cookie = user_switching::secure_olduser_cookie();
		$expiration            = time() + 172800; // 48 hours
		$auth_cookie           = user_switching_get_auth_cookie();
		$olduser_cookie        = wp_generate_auth_cookie( $old_user_id, $expiration, 'logged_in', $token );

		if ( $secure_auth_cookie ) {
			$auth_cookie_name = USER_SWITCHING_SECURE_COOKIE;
			$scheme           = 'secure_auth';
		} else {
			$auth_cookie_name = USER_SWITCHING_COOKIE;
			$scheme           = 'auth';
		}

		if ( $pop ) {
			array_pop( $auth_cookie );
		} else {
			array_push( $auth_cookie, wp_generate_auth_cookie( $old_user_id, $expiration, $scheme, $token ) );
		}

		$auth_cookie = json_encode( $auth_cookie );

		if ( false === $auth_cookie ) {
			return;
		}

		/**
		 * Fires immediately before the User Switching authentication cookie is set.
		 *
		 * @since 1.4.0
		 *
		 * @param string $auth_cookie JSON-encoded array of authentication cookie values.
		 * @param int    $expiration  The time when the authentication cookie expires as a UNIX timestamp.
		 * @param int    $old_user_id User ID.
		 * @param string $scheme      Authentication scheme. Values include 'auth' or 'secure_auth'.
		 * @param string $token       User's session token to use for the latest cookie.
		 */
		do_action( 'set_user_switching_cookie', $auth_cookie, $expiration, $old_user_id, $scheme, $token );

		$scheme = 'logged_in';

		/**
		 * Fires immediately before the User Switching old user cookie is set.
		 *
		 * @since 1.4.0
		 *
		 * @param string $olduser_cookie The old user cookie value.
		 * @param int    $expiration     The time when the logged-in authentication cookie expires as a UNIX timestamp.
		 * @param int    $old_user_id    User ID.
		 * @param string $scheme         Authentication scheme. Values include 'auth' or 'secure_auth'.
		 * @param string $token          User's session token to use for this cookie.
		 */
		do_action( 'set_olduser_cookie', $olduser_cookie, $expiration, $old_user_id, $scheme, $token );

		/**
		 * Allows preventing auth cookies from actually being sent to the client.
		 *
		 * @since 1.5.4
		 *
		 * @param bool $send Whether to send auth cookies to the client.
		 */
		if ( ! apply_filters( 'user_switching_send_auth_cookies', true ) ) {
			return;
		}

		setcookie( $auth_cookie_name, $auth_cookie, $expiration, SITECOOKIEPATH, COOKIE_DOMAIN, $secure_auth_cookie, true );
		setcookie( USER_SWITCHING_OLDUSER_COOKIE, $olduser_cookie, $expiration, COOKIEPATH, COOKIE_DOMAIN, $secure_olduser_cookie, true );
	}


Scroll to Top