WC_REST_Authentication::authentication_fallback │ public │ WC 1.0

Authenticate the user if authentication wasn't performed during the determine_current_user action.

This is the usual place a WooCommerce REST API key gets authenticated: it happens here because wp_get_current_user() usually resolves before REST dispatch is confirmed.

Runs at priority 8, just ahead of the default priority other plugins tend to use on this filter, so our own call to get_current_user_id() below is what resolves the real user first.

Method of the class: WC_REST_Authentication{}

No Hooks.

Returns

WP_Error|null|bool.

Usage

$WC_REST_Authentication = new WC_REST_Authentication();
$WC_REST_Authentication->authentication_fallback( $error );
$error(WP_Error|null|true|false) (required)
Error data.

Notes

See: https://github.com/woocommerce/woocommerce/issues/26847

WC_REST_Authentication::authentication_fallback() code WC 11.2.2

public function authentication_fallback( $error ) {
	if ( is_wp_error( $error ) ) {
		// Another plugin has already declared a failure. A plain `true` falls through instead.
		return $error;
	}
	if ( 0 === get_current_user_id() && empty( $this->error ) && empty( $this->auth_method ) && empty( $this->user ) ) {
		// Authentication hasn't occurred during `determine_current_user`, so check auth.
		$user_id = $this->authenticate( false );
		if ( $user_id ) {
			wp_set_current_user( $user_id );
		}
	}
	if ( $this->user ) {
		return true;
	}
	return $error;
}