Automattic\WooCommerce\Internal\RestApi\Routes\V4\Customers

Controller::delete_item_permissions_check │ public │ WC 1.0

Check if a given request has access to delete an item.

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

Хуков нет.

Возвращает

bool|WP_Error.

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

$Controller = new Controller();
$Controller->delete_item_permissions_check( $request );
$request(WP_REST_Request) (обязательный)
The request object.

Код Controller::delete_item_permissions_check() WC 11.2.2

public function delete_item_permissions_check( $request ) {
	$user = \Automattic\WooCommerce\Internal\Utilities\Users::get_user_in_current_site( $request['id'] );

	if ( is_wp_error( $user ) ) {
		return $this->get_route_error_by_code( self::INVALID_ID );
	}

	if ( ! wc_rest_check_user_permissions( 'delete', $user->ID ) ) {
		return $this->get_authentication_error_by_method( $request->get_method() );
	}

	$id = (int) $request['id'];

	$allowed_roles = $this->get_allowed_roles();
	$customer      = new WC_Customer( $id );

	if ( ! in_array( $customer->get_role(), $allowed_roles, true ) ) {
		return new WP_Error(
			'woocommerce_rest_cannot_delete',
			sprintf(
				/* translators: 1: Role of the user (administrator, customer), 2: comma separated list of allowed roles. egs customer, subscriber */
				__( 'Sorry, users with %1$s role cannot be deleted via this endpoint. Allowed roles: %2$s', 'woocommerce' ),
				$customer->get_role(),
				implode( ', ', $allowed_roles )
			),
			array( 'status' => rest_authorization_required_code() )
		);
	}

	return true;
}