Ticket #15636: 15636.typed.1.patch

File 15636.typed.1.patch, 1.7 KB (added by mario-siteground, 6 months ago)
  • wp-admin/includes/user.php

     
    184184        global $wp_roles; 
    185185 
    186186        $all_roles = $wp_roles->roles; 
    187         $editable_roles = apply_filters('editable_roles', $all_roles); 
     187         
     188        $default_role_keys = array( 
     189                'administrator', 
     190                'editor', 
     191                'author', 
     192                'contributor', 
     193                'subscriber' 
     194        ); 
     195         
     196        $default_roles = array(); 
     197        $custom_roles = array(); 
     198         
     199        foreach( $all_roles as $key => $value ) { 
     200                if( in_array( $key, $default_role_keys ) ) 
     201                        $default_roles[ $key ] = $value; 
     202                else 
     203                        $custom_roles[ $key ] = $value; 
     204        } 
    188205 
     206        uasort( $default_roles, '_sort_editable_roles' ); 
     207        uasort( $custom_roles, '_sort_editable_roles' ); 
     208         
     209        $editable_roles = array_merge( $default_roles, $custom_roles ); 
     210        $editable_roles = apply_filters( 'editable_roles', $editable_roles ); 
     211 
    189212        return $editable_roles; 
    190213} 
    191214 
    192215/** 
     216 * Helper function used in the one above - get_editable_roles, as sorting 
     217 * callback, in order to achieve sorted roles by capabilities. 
     218 *   
     219 * @param array $role_a 
     220 * @param array $role_b 
     221 * @return int 
     222 */ 
     223function _sort_editable_roles($role_a, $role_b) { 
     224        $first_cap_count = count( $role_a['capabilities'] ); 
     225        $second_cap_count = count( $role_b['capabilities'] ); 
     226 
     227        // The capability count is equal, sorting by name then. 
     228        if ( $first_cap_count == $second_cap_count ) 
     229                return strcmp ( $role_a['name'], $role_b['name'] ); 
     230 
     231        // Less capabilities goes up 
     232        return ( $first_cap_count > $second_cap_count ) ? 1 : -1; 
     233} 
     234 
     235/** 
    193236 * Retrieve user data and filter it. 
    194237 * 
    195238 * @since 2.0.5