Make WordPress Core


Ignore:
Timestamp:
03/06/2022 04:09:06 PM (4 years ago)
Author:
SergeyBiryukov
Message:

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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/user/capabilities.php

    r52010 r52823  
    16241624        $caps = $user->caps;
    16251625        $this->assertNotEmpty( $user->caps );
     1626
    16261627        $user->set_role( 'administrator' );
    16271628        $this->assertNotEmpty( $user->caps );
    16281629        $this->assertSame( $caps, $user->caps );
     1630    }
     1631
     1632    /**
     1633     * @ticket 54164
     1634     */
     1635    public function test_set_role_fires_remove_user_role_and_add_user_role_hooks() {
     1636        $user = self::$users['administrator'];
     1637
     1638        $remove_user_role = new MockAction();
     1639        $add_user_role    = new MockAction();
     1640        add_action( 'remove_user_role', array( $remove_user_role, 'action' ) );
     1641        add_action( 'add_user_role', array( $add_user_role, 'action' ) );
     1642
     1643        $user->set_role( 'editor' );
     1644        $this->assertSame( 1, $remove_user_role->get_call_count() );
     1645        $this->assertSame( 1, $add_user_role->get_call_count() );
    16291646    }
    16301647
Note: See TracChangeset for help on using the changeset viewer.