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
+17
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;