Automattic\WooCommerce\Internal

RestApiControllerBase::check_permissionprotectedWC 1.0

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

Method of the class: RestApiControllerBase{}

No Hooks.

Returns

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.

Usage

// protected - for code of main (parent) or child class
$result = $this->check_permission( $request, $required_capability_name, ...$extra_args );
$request(WP_REST_Request) (required)
The request for which the permission is checked.
$required_capability_name(string) (required)
The name of the required capability.
...$extra_args(mixed) (required)
Extra arguments to be used for the permission check.

RestApiControllerBase::check_permission() code WC 10.7.0

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() )
	);
}