Mahjouba91/multiple-roles

Problem with bbPress

Closed this issue · 2 comments

Hello,

There is a problem with bbPress WordPress plugin (https://bbpress.org/) once your plugin is activated. Indeed, when you add a new WordPress role to the user, the bbPress role vanishes and it's impossible to set it again.

Hi,

Sorry, I did not see your issue, do you have any idea how to fix this issue ?

@Mahjouba91 I'm having this issue too. I think the underlying issue here is actually a bigger one than the title suggests.

The checkbox list uses get_editable_roles() to output roles which can be assigned/revoked by the current user. Lot's of plugins use the editable_roles filter to manipulate this output, including bbPress.

It looks like all roles are completely emptied in MDMR_Model::update_roles() and rebuilt using the checklist output. But the checklist array can/will be different for every user (because of the editable_roles filter), meaning that certain roles will disappear completely depending on the editable_roles filters being applied.

Without knowing your plugin inside out, the following changes inside MDMR_Model::update_roles() fixes the issue for me:

// Remove this, we don't want to clear all user roles
$user->set_role( '' );
// Instead, remove all editable roles
$editable = get_editable_roles();
$editable_roles = is_array($editable) ? array_keys($editable) : array();
foreach( $editable_roles as $role ) {
	$user->remove_role( $role );
}

Rather than completely clearing all roles with $user->set_role( '' ), we loop through the get_editable_roles(), and unset those. This ensures that any roles not displayed in the checklist for the current user are left alone.