bbPress User Role Editor conflict fix

bbPress User Role Editor conflict fix

bbPress User Role Editor conflict fix

bbPress, popular WordPress plugin realizing forum functionality on the base WordPress framework and, as themselves say “WordPress way”, introduced enhanced role model starting from version 2.2. bbPress users, who use “User Role Editor” (URE) WordPress plugin to manage blog user roles, suddenly discovered that URE shows bbPress roles with almost all capabilities turned on, even for minimal ‘Blocked’ and ‘Spectator’ roles.
URE showed bbPress roles quite well before the bbPress version 2.2. update. What’s happened?
Let’s look. WordPress stores its roles data the way, that only active capabilities stored into the role. For example, if ‘Subscriber’ role has two capabilities only, WordPress stores at the database and loads into memory for internal purpose those ‘read’ and deprecated ‘level_0’ capabilities only. Thus, that was valid technique just to check if role contains capability or not, without additional checking, if that capability active (turned on) for that role. This code worked well for years:

if ( isset( $role[ 'read' ] ) ) {
  // role has 'read' capability
}

Updated bbPress role model started work other way. Every bbPress role contain all existed capabilities. Do you have a guess, what URE started to show and why? So, for begin, to fix this I added additional checking, if capability is active:

if ( isset( $role[ 'read' ] ) && !empty( $role[ 'read' ] ) ) {
  // role has 'read' capability
}

Further, reading related discussion, looking to bbPress code, I figured out, that new bbPress consider its roles fully separate from existing WordPress or, in bbPress terms, blog roles. If you have bbPress installed, look into any user profile. It has two roles related form controls now:
– “Role” or blog role – just under the Name title. Drop-down list here doesn’t include bbPress roles;
– “Forum Role” – at the bottom of the page, under the “Forum” title. Drop-down list here contains bbPress roles only.

bbPress Forum Role section at user profile

bbPress Forum Role section


So, when you select bbPress forum role for user, you don’t replace her blog role. You give user additional role. According to it, it’s incorrect to save bbPress introduced capabilities together with so called blog roles.
If you look into bbPress source code, file wp-content/plugins/bbpress/includes/core/capabilities.php:

17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/**
 * Returns an array of capabilities based on the role that is being requested.
 *
 * @since bbPress (r2994)
 *
 * @todo Map all of these and deprecate
 *
 * @param string $role Optional. Defaults to The role to load caps for
 * @uses apply_filters() Allow return value to be filtered
 *
 * @return array Capabilities for $role
 */
function bbp_get_caps_for_role( $role = '' ) {
 
	// Which role are we looking for?
	switch ( $role ) {
 
		// Keymaster
		case bbp_get_keymaster_role() :
			$caps = array(
 
				// Keymasters only
				'keep_gate'             => true,
 
				// Primary caps
				'spectate'              => true,
				'participate'           => true,
				'moderate'              => true,
// continued ...

you will see, that bbPress stores all its roles data inside PHP source code, not at the database as WordPress does. At the same bbp_get_caps_for_role() function we see the reason of the problem mentioned above, when URE showed bbPress roles stuff incorrectly:

130
131
132
133
134
135
136
137
138
139
140
		// Spectators can only read
		case bbp_get_spectator_role()   :
			$caps = array(
 
				// Primary caps
				'spectate'              => true,
				'participate'           => false,
				'moderate'              => false,
				'throttle'              => false,
				'view_trash'            => false,
// continued ...

According to WordPress itself, such role should contain 'spectate'=>true only. I understand, why developer select this way – it’s more easier modify role if needed in the future. But this is not the main problem of appeared incompatibility between bbPress and User Role Editor.

Main incompatibility problems are:
– bbPress doesn’t store its roles at the database as WordPress does, but create its roles and assign them capabilities on the fly, dynamically, every time during PHP source code execution.
– bbPress doesn’t replace blog user role by its own, just adding new capabilities to it. bbPress introduced new roles, which belong to bbPress only, and contains bbPress only capabilities. bbPress started to use multi-role assignment functionality, which existed at WordPress, but was not in use for this moment yet.

bbPress and User Role Editor conflict fix

For this reasons, starting from version 3.9 I exclude bbPress roles from User Role Editor. This way any inaccurate actions, like try to save bbPress role into database with help of User Role Editor will not create a mess at WordPress roles data.
At the same time fill list of capabilities introduced by bbPress is still available for inclusion into any WordPress blog user role.

If you have corrupted user roles at your blog somehow, changed functionality of “Reset” button (starting from version 3.9) could help you. Pressing “Reset” button at URE plugin you can return your blog roles to its zero state, just like when a fresh WordPress installation finished. Be accurate with such critical function, always create backup of WordPress database before use it, just in case something will go wrong.

Tags: , , ,