[wp-trac] [WordPress Trac] #54164: Consistently fire user role hooks

WordPress Trac noreply at wordpress.org
Thu Sep 23 07:48:00 UTC 2021


#54164: Consistently fire user role hooks
-------------------------+-----------------------------
 Reporter:  dd32         |      Owner:  (none)
     Type:  enhancement  |     Status:  new
 Priority:  normal       |  Milestone:  Awaiting Review
Component:  Users        |    Version:  4.3
 Severity:  normal       |   Keywords:  has-patch
  Focuses:               |
-------------------------+-----------------------------
 There are three main user role hooks:
  - add_user_role
  - remove_user_role
  - set_user_role

 There are some differences in how these hooks are fired:
  - `add_user_role` is always fired if `$user->add_role()` is called with a
 non-empty role. Whether the user had the role already or not.
  - `remove_user_role` is only fired if `$user->remove_role()` is called
 with a non-empty role AND the user had that role.
  - `set_user_role` (Which combines add & remove, but doesn't fire their
 hooks) is only fired if the user had different roles prior to being
 called. If the role matches what the user already had, it's not.

 This leaves with some complicated rules if you want to watch for user role
 changes.
  - `add_user_role` firing doesn't mean a user has gained a role.
  - `set_user_role` may have removed a role.
  - `set_user_role` may fire without gaining a role.

 Attached is a proposed patch that:
  - Doesn't fire `add_user_role` if the user already had the role.
  - Fires `remove_user_role` and `add_user_role` from within `$user->set()`
 when appropriate

 This standardises the actions that one needs to hook to, being either
 `add_user_role` or `remove_user_role`.

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/54164>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list