Automattic\WooCommerce\Internal\RestApi

InteractiveSessionCheck::require_interactive_session │ public static │ WC 11.2.2

Check that the current user was authenticated by cookie and that the request carries a valid wp_rest nonce, as a _wpnonce parameter or an X-WP-Nonce header.

Method of the class: InteractiveSessionCheck{}

No Hooks.

Returns

WP_Error|true.

Usage

$result = InteractiveSessionCheck::require_interactive_session( $request );
$request(required)
.

Notes

Global. Mixed. $wp_rest_auth_cookie True when core validated an auth cookie for this request.

Changelog

Since 11.2.2 Introduced.

InteractiveSessionCheck::require_interactive_session() code WC 11.2.2

public static function require_interactive_session( $request ) {
	global $wp_rest_auth_cookie;

	$nonce = $request->get_param( '_wpnonce' ) ?? $request->get_header( 'X-WP-Nonce' );

	if ( ! is_user_logged_in() || true !== $wp_rest_auth_cookie || ! is_string( $nonce ) || ! wp_verify_nonce( $nonce, 'wp_rest' ) ) {
		return new WP_Error(
			'woocommerce_rest_missing_session',
			__( 'Sorry, you cannot perform this action without an interactive admin session.', 'woocommerce' ),
			array( 'status' => rest_authorization_required_code() )
		);
	}

	return true;
}