WordPress.org

Make WordPress Core

Ticket #11635: wp-get-user-role.diff

File wp-get-user-role.diff, 2.7 KB (added by wonderboymusic, 6 years ago)
  • wp-includes/user.php

     
    14871487        );
    14881488        return apply_filters( 'user_contactmethods', $user_contactmethods, $user );
    14891489}
     1490
     1491/**
     1492 * Retrieve the highest-order role for a user
     1493 *
     1494 * @access public
     1495 * @since 3.5.0
     1496 *
     1497 * @param int|object $user User ID or data object (optional)
     1498 * @return string $user_role
     1499 */
     1500function wp_get_user_role( $user = null ) {
     1501        if ( is_int( $user ) )
     1502                $user = get_user_by( 'id', $user );
     1503       
     1504        if ( empty( $user->ID ) && get_current_user_id() )
     1505                $user = wp_get_current_user();
     1506               
     1507        if ( !empty( $user->roles ) )
     1508                return reset( $user->roles );
     1509}
     1510 No newline at end of file
  • wp-admin/includes/ajax-actions.php

     
    10251025        $x = new WP_Ajax_Response( array(
    10261026                'what' => 'user',
    10271027                'id' => $user_id,
    1028                 'data' => $wp_list_table->single_row( $user_object, '', $user_object->roles[0] ),
     1028                'data' => $wp_list_table->single_row( $user_object, '', wp_get_user_role( $user_object ) ),
    10291029                'supplemental' => array(
    10301030                        'show-link' => sprintf(__( 'User <a href="#%s">%s</a> added' ), "user-$user_id", $user_object->user_login),
    1031                         'role' => $user_object->roles[0]
     1031                        'role' => wp_get_user_role( $user_object )
    10321032                )
    10331033        ) );
    10341034        $x->send();
  • wp-admin/includes/class-wp-users-list-table.php

     
    193193
    194194                $style = '';
    195195                foreach ( $this->items as $userid => $user_object ) {
    196                         $role = reset( $user_object->roles );
     196                        $role = wp_get_user_role( $user_object );
    197197
    198198                        if ( is_multisite() && empty( $role ) )
    199199                                continue;
  • wp-admin/user-edit.php

     
    248248<tr><th><label for="role"><?php _e('Role:') ?></label></th>
    249249<td><select name="role" id="role">
    250250<?php
    251 // Get the highest/primary role for this user
    252 // TODO: create a function that does this: wp_get_user_role()
    253 $user_roles = $profileuser->roles;
    254 $user_role = array_shift($user_roles);
    255251
     252$user_role = wp_get_user_role( $profileuser );
     253
    256254// print the full list of roles with the primary one selected.
    257 wp_dropdown_roles($user_role);
     255wp_dropdown_roles( $user_role );
    258256
    259257// print the 'no role' option. Make it selected if the user has no role yet.
    260258if ( $user_role )