Make WordPress Core


Ignore:
Timestamp:
06/29/2025 09:45:30 PM (11 months ago)
Author:
johnbillion
Message:

Role/Capability: Add support for passing a numerically-indexed array of capability names to the add_role() function and the WP_Roles::add_role() method.

Denying a capability at the point of creating the role is a relatively uncommon requirement, so this simplifies the syntax needed to grant a list of capabilities to the role.

Props eclev91, johnbillion, flixos90, johnjamesjacoby, soulseekah

Fixes #43421

File:
1 edited

Legend:

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

    r59198 r60364  
    994994
    995995    /**
     996     * Test add_role with implied capabilities grant successfully grants capabilities.
     997     *
     998     * @ticket 43421
     999     */
     1000    public function test_add_role_with_single_level_capabilities() {
     1001        $role_name = 'janitor';
     1002        add_role(
     1003            $role_name,
     1004            'Janitor',
     1005            array(
     1006                'foo',
     1007            )
     1008        );
     1009        $this->flush_roles();
     1010
     1011        // Assign a user to that role.
     1012        $id   = self::factory()->user->create( array( 'role' => $role_name ) );
     1013        $user = new WP_User( $id );
     1014
     1015        $this->assertTrue( $user->has_cap( 'foo' ) );
     1016    }
     1017
     1018    /**
    9961019     * Change the capabilities associated with a role and make sure the change
    9971020     * is reflected in has_cap().
Note: See TracChangeset for help on using the changeset viewer.