[wp-trac] [WordPress Trac] #53199: WP_User::add_role() runs even if the added role is already set

WordPress Trac noreply at wordpress.org
Thu May 13 15:46:07 UTC 2021


#53199: WP_User::add_role() runs even if the added role is already set
-----------------------------------+------------------------------
 Reporter:  bhujagendra            |       Owner:  (none)
     Type:  defect (bug)           |      Status:  new
 Priority:  normal                 |   Milestone:  Awaiting Review
Component:  Users                  |     Version:  2.0
 Severity:  normal                 |  Resolution:
 Keywords:  2nd-opinion has-patch  |     Focuses:
-----------------------------------+------------------------------

Comment (by bhujagendra):

 Hi @desrosj

 Thanks for looking into this and the info regarding the version on trac.

 Yes, I too see it as an edge-case where someone would rely on
 `add_user_role? firing every time. And I agree, introducing a new hook
 that will probably never be used. I just wanted to mention it and an idea
 for a solution, as it might help others to think of a unwanted side-
 effect.

 However, introducing two new filters instead, to filter the role, that
 might make more sense. By this, a plugin could prevent a given user been
 given a certain role, or a certain role to be removed. The filters could
 then look something along the lines of the following:

 {{{

 /**
  * Filters the role-to-be-added  to a user. Return an empty string to
 prevent the role being added.
  *
  * @since x.x.x
  *
  * @param string  $role          The new role.
  * @param WP_User $user          The user object
  * @param string  $original_role The new role (as originally requested).
  */
 $role = apply_filters( 'pre_add_user_role', $role, $this );
 }}}

 and

 {{{

 /**
  * Filters the role-to-be-removed from a user. Return an empty string to
 prevent the role being added.
  *
  * @since x.x.x
  *
  * @param string  $role          The role to be removed.
  * @param WP_User $user          The user object
  * @param string  $original_role The role to be removed (as originally
 requested).
  */
 $role = apply_filters( 'pre_remove_user_role', $role, $this, $role );
 }}}

 So the whole thing would look something like this:
 https://github.com/WordPress/wordpress-develop/pull/1254


 Open for comments. Happy to make amendments.

 Peace,
 Bhujagendra.

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/53199#comment:5>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list