From e764924c4c34f0e24dd46ebb2bf24c15fa9e899d Mon Sep 17 00:00:00 2001 From: Ethan Clevenger Date: Tue, 17 Sep 2024 15:45:06 -0700 Subject: [PATCH] Remove support for mixed format capabilities. --- src/wp-includes/capabilities.php | 5 ++++- src/wp-includes/class-wp-roles.php | 14 +++++++------- tests/phpunit/tests/user/capabilities.php | 2 -- 3 files changed, 11 insertions(+), 10 deletions(-) diff --git a/src/wp-includes/capabilities.php b/src/wp-includes/capabilities.php index d420717bc759b..f6c680311e8bd 100644 --- a/src/wp-includes/capabilities.php +++ b/src/wp-includes/capabilities.php @@ -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() ) { diff --git a/src/wp-includes/class-wp-roles.php b/src/wp-includes/class-wp-roles.php index 8361456a495b4..d8a7893b2810a 100644 --- a/src/wp-includes/class-wp-roles.php +++ b/src/wp-includes/class-wp-roles.php @@ -147,12 +147,15 @@ 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() ) { @@ -160,11 +163,8 @@ public function add_role( $role, $display_name, $capabilities = array() ) { 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( diff --git a/tests/phpunit/tests/user/capabilities.php b/tests/phpunit/tests/user/capabilities.php index 6b1467c795f98..23cb6936f1d3f 100644 --- a/tests/phpunit/tests/user/capabilities.php +++ b/tests/phpunit/tests/user/capabilities.php @@ -1001,7 +1001,6 @@ public function test_add_role_with_single_level_capabilities() { 'Janitor', array( 'level_1', - 'sweep_floors' => false, ) ); $this->flush_roles(); @@ -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' ) ); } /**