Changeset 60364
- Timestamp:
- 06/29/2025 09:45:30 PM (3 months ago)
- Location:
- trunk
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/capabilities.php
r60294 r60364 1097 1097 * Adds a role, if it does not exist. 1098 1098 * 1099 * The list of capabilities can be passed either as a numerically indexed array of capability names, or an 1100 * associative array of boolean values keyed by the capability name. To explicitly deny the role a capability, set 1101 * the value for that capability to false. 1102 * 1103 * Examples: 1104 * 1105 * // Add a role that can edit posts. 1106 * add_role( 'custom_role', 'Custom Role', array( 1107 * 'read', 1108 * 'edit_posts', 1109 * ) ); 1110 * 1111 * Or, using an associative array: 1112 * 1113 * // Add a role that can edit posts but explicitly cannot not delete them. 1114 * add_role( 'custom_role', 'Custom Role', array( 1115 * 'read' => true, 1116 * 'edit_posts' => true, 1117 * 'delete_posts' => false, 1118 * ) ); 1119 * 1099 1120 * @since 2.0.0 1100 * 1101 * @param string $role Role name. 1102 * @param string $display_name Display name for role. 1103 * @param bool[] $capabilities List of capabilities keyed by the capability name, 1104 * e.g. array( 'edit_posts' => true, 'delete_posts' => false ). 1121 * @since x.y.z Support was added for a numerically indexed array of strings for the capabilities array. 1122 * 1123 * @param string $role Role name. 1124 * @param string $display_name Display name for role. 1125 * @param array<string,bool>|array<int,string> $capabilities Capabilities to be added to the role. 1126 * Default empty array. 1105 1127 * @return WP_Role|void WP_Role object, if the role is added. 1106 1128 */ -
trunk/src/wp-includes/class-wp-roles.php
r58975 r60364 144 144 * Updates the list of roles, if the role doesn't already exist. 145 145 * 146 * The capabilities are defined in the following format: `array( 'read' => true )`. 147 * To explicitly deny the role a capability, set the value for that capability to false. 148 * 149 * @since 2.0.0 150 * 151 * @param string $role Role name. 152 * @param string $display_name Role display name. 153 * @param bool[] $capabilities Optional. List of capabilities keyed by the capability name, 154 * e.g. `array( 'edit_posts' => true, 'delete_posts' => false )`. 155 * Default empty array. 146 * The list of capabilities can be passed either as a numerically indexed array of capability names, or an 147 * associative array of boolean values keyed by the capability name. To explicitly deny the role a capability, set 148 * the value for that capability to false. 149 * 150 * Examples: 151 * 152 * // Add a role that can edit posts. 153 * wp_roles()->add_role( 'custom_role', 'Custom Role', array( 154 * 'read', 155 * 'edit_posts', 156 * ) ); 157 * 158 * Or, using an associative array: 159 * 160 * // Add a role that can edit posts but explicitly cannot not delete them. 161 * wp_roles()->add_role( 'custom_role', 'Custom Role', array( 162 * 'read' => true, 163 * 'edit_posts' => true, 164 * 'delete_posts' => false, 165 * ) ); 166 * 167 * @since 2.0.0 168 * @since x.y.z Support was added for a numerically indexed array of strings for the capabilities array. 169 * 170 * @param string $role Role name. 171 * @param string $display_name Role display name. 172 * @param array<string,bool>|array<int,string> $capabilities Capabilities to be added to the role. 173 * Default empty array. 156 174 * @return WP_Role|void WP_Role object, if the role is added. 157 175 */ … … 159 177 if ( empty( $role ) || isset( $this->roles[ $role ] ) ) { 160 178 return; 179 } 180 181 if ( wp_is_numeric_array( $capabilities ) ) { 182 $capabilities = array_fill_keys( $capabilities, true ); 161 183 } 162 184 -
trunk/tests/phpunit/tests/user/capabilities.php
r59198 r60364 994 994 995 995 /** 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 /** 996 1019 * Change the capabilities associated with a role and make sure the change 997 1020 * is reflected in has_cap().
Note: See TracChangeset for help on using the changeset viewer.