Users: Bring some consistency to user role hooks.

This standardizes the actions that one needs to hook to for tracking user role changes:

* `add_user_role` is only fired when the user has actually gained a new role.
* `remove_user_role` is only fired when the role was actually removed.

Both actions are now fired in `WP_User::set_role()` as appropriate.

Props dd32, SergeyBiryukov.
Fixes #54164.

git-svn-id: https://develop.svn.wordpress.org/trunk@52823 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Sergey Biryukov 2022-03-06 16:09:06 +00:00
parent f0fdad1c8e
commit 864ab55b33
2 changed files with 39 additions and 1 deletions

View File

@ -542,6 +542,10 @@ class WP_User {
return;
}
if ( in_array( $role, $this->roles, true ) ) {
return;
}
$this->caps[ $role ] = true;
update_user_meta( $this->ID, $this->cap_key, $this->caps );
$this->get_role_caps();
@ -569,6 +573,7 @@ class WP_User {
if ( ! in_array( $role, $this->roles, true ) ) {
return;
}
unset( $this->caps[ $role ] );
update_user_meta( $this->ID, $this->cap_key, $this->caps );
$this->get_role_caps();
@ -606,16 +611,32 @@ class WP_User {
}
$old_roles = $this->roles;
if ( ! empty( $role ) ) {
$this->caps[ $role ] = true;
$this->roles = array( $role => true );
} else {
$this->roles = false;
$this->roles = array();
}
update_user_meta( $this->ID, $this->cap_key, $this->caps );
$this->get_role_caps();
$this->update_user_level_from_caps();
foreach ( $old_roles as $old_role ) {
if ( ! $old_role || $old_role === $role ) {
continue;
}
/** This action is documented in wp-includes/class-wp-user.php */
do_action( 'remove_user_role', $this->ID, $old_role );
}
if ( $role && ! in_array( $role, $old_roles, true ) ) {
/** This action is documented in wp-includes/class-wp-user.php */
do_action( 'add_user_role', $this->ID, $role );
}
/**
* Fires after the user's role has changed.
*

View File

@ -1623,11 +1623,28 @@ class Tests_User_Capabilities extends WP_UnitTestCase {
$user = self::$users['administrator'];
$caps = $user->caps;
$this->assertNotEmpty( $user->caps );
$user->set_role( 'administrator' );
$this->assertNotEmpty( $user->caps );
$this->assertSame( $caps, $user->caps );
}
/**
* @ticket 54164
*/
public function test_set_role_fires_remove_user_role_and_add_user_role_hooks() {
$user = self::$users['administrator'];
$remove_user_role = new MockAction();
$add_user_role = new MockAction();
add_action( 'remove_user_role', array( $remove_user_role, 'action' ) );
add_action( 'add_user_role', array( $add_user_role, 'action' ) );
$user->set_role( 'editor' );
$this->assertSame( 1, $remove_user_role->get_call_count() );
$this->assertSame( 1, $add_user_role->get_call_count() );
}
public function test_current_user_can_for_blog() {
global $wpdb;