Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trac/43421 #7351

Open
wants to merge 10 commits into
base: trunk
Choose a base branch
from
4 changes: 3 additions & 1 deletion src/wp-includes/capabilities.php
Original file line number Diff line number Diff line change
Expand Up @@ -1021,11 +1021,13 @@ function get_role( $role ) {
* Adds a role, if it does not exist.
*
* @since 2.0.0
* @since x.y.z Support an array of strings for the capabilities array.
*
* @param string $role Role name.
* @param string $display_name Display name for role.
* @param bool[] $capabilities List of capabilities keyed by the capability name,
* @param bool[]|string[] $capabilities List of capabilities keyed by the capability name,
* e.g. array( 'edit_posts' => true, 'delete_posts' => false ).
* Also supports an array of capabilities assumed to be granted.
* @return WP_Role|void WP_Role object, if the role is added.
*/
function add_role( $role, $display_name, $capabilities = array() ) {
Expand Down
10 changes: 8 additions & 2 deletions src/wp-includes/class-wp-roles.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,11 +147,13 @@ public function reinit() {
* To explicitly deny the role a capability, set the value for that capability to false.
*
* @since 2.0.0
* @since x.y.z Support an array of strings for the capabilities array.
*
* @param string $role Role name.
* @param string $display_name Role display name.
* @param bool[] $capabilities Optional. List of capabilities keyed by the capability name,
* e.g. `array( 'edit_posts' => true, 'delete_posts' => false )`.
* @param bool[]|string[] $capabilities List of capabilities keyed by the capability name,
* e.g. array( 'edit_posts' => true, 'delete_posts' => false ).
* Also supports an array of capabilities assumed to be granted.
* Default empty array.
* @return WP_Role|void WP_Role object, if the role is added.
*/
Expand All @@ -160,6 +162,10 @@ public function add_role( $role, $display_name, $capabilities = array() ) {
return;
}

if ( wp_is_numeric_array( $capabilities ) ) {
$capabilities = array_fill_keys( $capabilities, true );
}

$this->roles[ $role ] = array(
'name' => $display_name,
'capabilities' => $capabilities,
Expand Down
21 changes: 21 additions & 0 deletions tests/phpunit/tests/user/capabilities.php
Original file line number Diff line number Diff line change
Expand Up @@ -991,6 +991,27 @@ public function test_add_role() {
$this->assertFalse( $wp_roles->is_role( $role_name ) );
}

/**
* Test add_role with implied capabilities grant successfully grants capabilities.
*/
public function test_add_role_with_single_level_capabilities() {
$role_name = 'janitor';
add_role(
$role_name,
'Janitor',
array(
'level_1',
)
);
$this->flush_roles();

// Assign a user to that role.
$id = self::factory()->user->create( array( 'role' => $role_name ) );
$user = new WP_User( $id );

$this->assertTrue( $user->has_cap( 'level_1' ) );
}

/**
* Change the capabilities associated with a role and make sure the change
* is reflected in has_cap().
Expand Down
Loading