Automattic\WooCommerce\Internal

RestApiControllerBase::check_permission()protectedWC 1.0

Permission check for REST API endpoints, given the request method.

Метод класса: RestApiControllerBase{}

Хуков нет.

Возвращает

true|false|WP_Error. True if the current user has the capability, otherwise an "Unauthorized" error or False if no error is available for the request method.

Использование

// protected - в коде основоного (родительского) или дочернего класса
$result = $this->check_permission( $request, $required_capability_name, ...$extra_args );
$request(WP_REST_Request) (обязательный)
The request for which the permission is checked.
$required_capability_name(строка) (обязательный)
The name of the required capability.
...$extra_args(разное) (обязательный)
Extra arguments to be used for the permission check.

Код RestApiControllerBase::check_permission() WC 9.7.1

protected function check_permission( WP_REST_Request $request, string $required_capability_name, ...$extra_args ) {
	if ( current_user_can( $required_capability_name, ...$extra_args ) ) {
		return true;
	}

	$error_information = $this->get_authentication_error_by_method( $request->get_method() );
	if ( is_null( $error_information ) ) {
		return false;
	}

	return new WP_Error(
		$error_information['code'],
		$error_information['message'],
		array( 'status' => rest_authorization_required_code() )
	);
}