This is one reason plugins should not work with roles for “permission”. Capabilities exist for deciding whether users should have or not have access to something.
It’s perfectly OK for the user to “see” that they’re choosing a role, but under the hood, the plugin should be working with a custom capability.
In order to keep your current UI, here’s what I’d do:
1) Use a new capability wpans_block_access
. This will be used to check if the user role is blocked.
current_user_can( 'wpans_block_access' );
2) When saving, loop through the selected roles and add that capability to them.
$role_object->add_cap( 'wpans_block_access' );
3) When outputting the form fields, you can check if the role has the cap.
$role_object->has_cap( 'wpans_block_access' );
——
I must add that this should be flipped around. You should work from a position of giving access rather than a position of blocking access. So, instead of a blacklist, you should have a whitelist by granting a capability like wpans_allow_access
.