Skip to content

Commit

Permalink
Remove support for mixed format capabilities.
Browse files Browse the repository at this point in the history
  • Loading branch information
ethanclevenger91 committed Sep 17, 2024
1 parent ccf5d0d commit e764924
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 10 deletions.
5 changes: 4 additions & 1 deletion src/wp-includes/capabilities.php
Original file line number Diff line number Diff line change
Expand Up @@ -1021,11 +1021,14 @@ 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
14 changes: 7 additions & 7 deletions src/wp-includes/class-wp-roles.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,24 +147,24 @@ 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.
*/
public function add_role( $role, $display_name, $capabilities = array() ) {
if ( empty( $role ) || isset( $this->roles[ $role ] ) ) {
return;
}

foreach ( $capabilities as $key => $value ) {
if ( ! is_bool( $value ) ) {
$capabilities[ $value ] = true;
unset( $capabilities[ $key ] );
}
if ( wp_is_numeric_array( $capabilities ) ) {
$capabilities = array_fill_keys( $capabilities, true );
}

$this->roles[ $role ] = array(
Expand Down
2 changes: 0 additions & 2 deletions tests/phpunit/tests/user/capabilities.php
Original file line number Diff line number Diff line change
Expand Up @@ -1001,7 +1001,6 @@ public function test_add_role_with_single_level_capabilities() {
'Janitor',
array(
'level_1',
'sweep_floors' => false,
)
);
$this->flush_roles();
Expand All @@ -1011,7 +1010,6 @@ public function test_add_role_with_single_level_capabilities() {
$user = new WP_User( $id );

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

/**
Expand Down

0 comments on commit e764924

Please sign in to comment.