WC_WCCOM_Site::authentication_fallback │ public static │ WC 11.3.0

Authenticate a WooCommerce.com request that carries rest_route.

WordPress determines the current user before it parses the request, so authenticate_wccom() leaves these requests alone during determine_current_user they are authenticated here instead.

Method of the class: WC_WCCOM_Site{}

No Hooks.

Returns

WP_Error|null|bool.

Usage

$result = WC_WCCOM_Site::authentication_fallback( $error );
$error(WP_Error|null|true|false) (required)
Error from another authentication handler, null if none.

Changelog

Since 11.3.0 Introduced.

WC_WCCOM_Site::authentication_fallback() code WC 11.3.0

public static function authentication_fallback( $error ) {
	if ( ! empty( $error ) || 0 !== get_current_user_id() || ! isset( $_REQUEST['rest_route'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
		return $error;
	}

	$user = self::authenticate_wccom( false );

	if ( ! $user instanceof WP_User ) {
		return $error;
	}

	wp_set_current_user( $user->ID );
	return true;
}