From 864ab55b33d4ddb8fcc040a5395d623ac4c3ba72 Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Sun, 6 Mar 2022 16:09:06 +0000 Subject: [PATCH] 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 --- src/wp-includes/class-wp-user.php | 23 ++++++++++++++++++++++- tests/phpunit/tests/user/capabilities.php | 17 +++++++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/src/wp-includes/class-wp-user.php b/src/wp-includes/class-wp-user.php index b2f8687828..c6c7ef4eaf 100644 --- a/src/wp-includes/class-wp-user.php +++ b/src/wp-includes/class-wp-user.php @@ -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. * diff --git a/tests/phpunit/tests/user/capabilities.php b/tests/phpunit/tests/user/capabilities.php index 496de9487a..5f838ac27d 100644 --- a/tests/phpunit/tests/user/capabilities.php +++ b/tests/phpunit/tests/user/capabilities.php @@ -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;