Ticket #54572: 54572.diff
| File 54572.diff, 5.7 KB (added by , 5 years ago) |
|---|
-
src/wp-includes/capabilities.php
IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8 diff --git a/src/wp-includes/capabilities.php b/src/wp-includes/capabilities.php
a b 849 849 return wp_roles()->add_role( $role, $display_name, $capabilities ); 850 850 } 851 851 852 /** 853 * Add role, if it does not exist. Update, if exists. 854 * 855 * @param string $role Role name. 856 * @param string|null $display_name Role display name. 857 * @param bool[]|null $capabilities List of capabilities keyed by the capability name, 858 * e.g. array( 'edit_posts' => true, 'delete_posts' => false ). 859 * If null, don't alter capabilities for the existing role and make 860 * empty capabilities for the new one. 861 * 862 * @return WP_Role|null WP_Role object if role is added, null if already exists. 863 */ 864 function update_role( $role, $display_name, $capabilities = null ) { 865 if ( empty( $role ) ) { 866 return; 867 } 868 869 return wp_roles()->update_role( $role, $display_name, $capabilities ); 870 } 871 852 872 /** 853 873 * Remove role, if it exists. 854 874 * -
src/wp-includes/class-wp-roles.php
IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8 diff --git a/src/wp-includes/class-wp-roles.php b/src/wp-includes/class-wp-roles.php
a b 170 170 return $this->role_objects[ $role ]; 171 171 } 172 172 173 /** 174 * Update the existing role. If the role doesn't exist, add a new role. 175 * 176 * Updates the list of roles, if the role doesn't already exist. 177 * 178 * For the capabilities format, @param string $role Role name. 179 * 180 * @param string|null $display_name Role display name. 181 * @param bool[]|null $capabilities List of capabilities keyed by the capability name, 182 * e.g. array( 'edit_posts' => true, 'delete_posts' => false ). 183 * If null, don't alter capabilities for the existing role and make 184 * empty capabilities for the new one. 185 * 186 * @return WP_Role|void WP_Role object, if role is added. 187 * @see WP_Roles::add_role() 188 * 189 */ 190 public function update_role( $role, $display_name = null, $capabilities = null ) { 191 if ( empty( $role ) || ( null === $display_name && null === $capabilities ) ) { 192 return; 193 } 194 195 if ( null === $display_name ) { 196 if ( isset( $this->role_objects[ $role ] ) ) { 197 $display_name = $this->roles[ $role ]['name']; 198 } else { 199 return; 200 } 201 } 202 203 if ( null === $capabilities ) { 204 if ( isset( $this->role_objects[ $role ] ) ) { 205 $capabilities = $this->role_objects[ $role ]->capabilities; 206 } else { 207 $capabilities = array(); 208 } 209 } 210 211 if ( isset( $this->roles[ $role ] ) ) { 212 if ( null === $capabilities ) { 213 $capabilities = $this->role_objects[ $role ]->capabilities; 214 } 215 216 unset( $this->role_objects[ $role ] ); 217 unset( $this->role_names[ $role ] ); 218 unset( $this->roles[ $role ] ); 219 } 220 221 return $this->add_role( $role, $display_name, $capabilities ); 222 } 223 173 224 /** 174 225 * Remove role by name. 175 226 * -
tests/phpunit/tests/user/capabilities.php
IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8 diff --git a/tests/phpunit/tests/user/capabilities.php b/tests/phpunit/tests/user/capabilities.php
a b 980 980 $this->assertFalse( $wp_roles->is_role( $role_name ) ); 981 981 } 982 982 983 /** 984 * Update role. 985 */ 986 public function test_update_role() { 987 global $wp_roles; 988 989 // Create role if it does not exist. 990 $role_name = 'janitor'; 991 $expected_caps = array( 992 'edit_posts' => true, 993 'edit_pages' => true, 994 'level_0' => true, 995 'level_1' => true, 996 'level_2' => true, 997 ); 998 update_role( $role_name, 'Janitor', $expected_caps ); 999 $this->flush_roles(); 1000 $this->assertTrue( $wp_roles->is_role( $role_name ) ); 1001 1002 // Update both role name and capabilities. 1003 $expected_display_name = 'Janitor Manager'; 1004 $new_expected_caps = array( 1005 'level_3' => true, 1006 'level_4' => true, 1007 ); 1008 update_role( $role_name, $expected_display_name, $new_expected_caps ); 1009 $this->flush_roles(); 1010 1011 $this->assertEquals( $expected_display_name, $wp_roles->roles[ $role_name ]['name'] ); 1012 $this->assertEquals( $new_expected_caps, $wp_roles->roles[ $role_name ]['capabilities'] ); 1013 1014 // Update role name only. 1015 $expected_display_name = 'Janitor Executive'; 1016 update_role( $role_name, $expected_display_name ); 1017 $this->flush_roles(); 1018 1019 $this->assertEquals( $expected_display_name, $wp_roles->roles[ $role_name ]['name'] ); 1020 $this->assertEquals( $new_expected_caps, $wp_roles->roles[ $role_name ]['capabilities'] ); 1021 1022 // Update empty capabilities only (proper null vs empty array handling). 1023 update_role( $role_name, null, array() ); 1024 $this->flush_roles(); 1025 1026 $this->assertEmpty( $wp_roles->roles[ $role_name ]['capabilities'] ); 1027 1028 // Clean up. 1029 remove_role( $role_name ); 1030 $this->flush_roles(); 1031 $this->assertFalse( $wp_roles->is_role( $role_name ) ); 1032 } 1033 983 1034 /** 984 1035 * Change the capabilites associated with a role and make sure the change 985 1036 * is reflected in has_cap().