Make WordPress Core

Ticket #17924: Replace_Admin_Role_Dropdown.patch

File Replace_Admin_Role_Dropdown.patch, 9.3 KB (added by mobius5150, 14 years ago)

The first patch.

  • wp-admin/includes/class-wp-users-list-table.php

     
    7272                        'total_items' => $wp_user_search->get_total(),
    7373                        'per_page' => $users_per_page,
    7474                ) );
     75               
     76                add_filter( 'user_role_name', array( &$this, 'wp_user_role_name' ), 1, 2 );
    7577        }
    7678
    7779        function no_items() {
     
    140142?>
    141143        <div class="alignleft actions">
    142144                <label class="screen-reader-text" for="new_role"><?php _e( 'Change role to&hellip;' ) ?></label>
    143                 <select name="new_role" id="new_role">
    144                         <option value=''><?php _e( 'Change role to&hellip;' ) ?></option>
    145                         <?php wp_dropdown_roles(); ?>
    146                 </select>
     145                <?php wp_user_role_selector(array(
     146                    'name' => 'new_role',
     147                    'id' => 'new_role',
     148                    'no_role' => false,
     149                    'before_list' => '<option value="" selected="true">' . __( 'Change role to&hellip;' ) . '</option>'
     150                )) ?>
    147151                <?php submit_button( __( 'Change' ), 'secondary', 'changeit', false ); ?>
    148152        </div>
    149153<?php
     
    192196
    193197                $style = '';
    194198                foreach ( $this->items as $userid => $user_object ) {
    195                         $role = reset( $user_object->roles );
    196199
    197200                        if ( is_multisite() && empty( $role ) )
    198201                                continue;
     
    260263                } else {
    261264                        $edit = '<strong>' . $user_object->user_login . '</strong>';
    262265                }
    263                 $role_name = isset( $wp_roles->role_names[$role] ) ? translate_user_role( $wp_roles->role_names[$role] ) : __( 'None' );
     266                if ( !isset( $role ) || $role == '' )
     267                    $role = $user_object->roles;
     268                if ( !is_array($role) )
     269                    $role = array( $role );
     270                $role_name = apply_filters( 'user_role_name' , $role, $role );
    264271                $avatar = get_avatar( $user_object->ID, 32 );
    265272
    266273                $r = "<tr id='user-$user_object->ID'$style>";
     
    314321
    315322                return $r;
    316323        }
     324       
     325        /**
     326         * Formats the role name for the WP_Users_List table role column.
     327         *
     328         * @param type string $role the string that is being filtered.
     329         * @param type array $roles the array of roles
     330         * @return type string
     331         */
     332        static function wp_user_role_name( $role, $roles ) {
     333            global $wp_roles;
     334            return isset( $wp_roles->role_names[$roles[0]] ) ? translate_user_role( $wp_roles->role_names[$roles[0]] ) : __( 'None' );
     335        }
     336       
    317337}
    318338
    319339?>
  • wp-admin/includes/template.php

     
    752752
    753753
    754754/**
    755  * Print out <option> html elements for role selectors
     755 * Get the <option> html elements for role selectors
    756756 *
    757757 * @since 2.1.0
     758 * @deprecated 3.1.2 this function is deprecated. User wp_user_role_selector instead.
     759 * @uses serves as a wrapper for wp_get_dropdown_roles
     760 * @param string $selected slug for the role that should be already selected
     761 */
     762function wp_dropdown_roles( $selected = false ) {
     763        echo wp_get_dropdown_roles( $selected );
     764}
     765
     766
     767/**
     768 * Get the <option> html elements for role selectors
    758769 *
     770 * @since 3.1.2
     771 *
    759772 * @param string $selected slug for the role that should be already selected
     773 * @return string
    760774 */
    761 function wp_dropdown_roles( $selected = false ) {
     775function wp_get_dropdown_roles( $selected = false ) {
    762776        $p = '';
    763777        $r = '';
    764778
    765779        $editable_roles = get_editable_roles();
     780       
     781        if ( is_array( $selected ) )
     782            $selected = $selected[0];
    766783
    767784        foreach ( $editable_roles as $role => $details ) {
    768                 $name = translate_user_role($details['name'] );
     785                $name = translate_user_role( $details['name'] );
    769786                if ( $selected == $role ) // preselect specified role
    770787                        $p = "\n\t<option selected='selected' value='" . esc_attr($role) . "'>$name</option>";
    771788                else
    772789                        $r .= "\n\t<option value='" . esc_attr($role) . "'>$name</option>";
    773790        }
    774         echo $p . $r;
     791        return $p . $r;
    775792}
    776793
     794
    777795/**
     796 * Print out the dropdown role selector
     797 *
     798 * selected - (string) The role that is to be selected in the dropdown
     799 * name - (string) The html name element of the selector
     800 * id - (string) The html id element of the selector
     801 * class - (string) The classes to be applied to the selector
     802 * no_role - (bool) Whether an option should be shown to select no role
     803 * before_list - (string) Html to be inserted after the <select> and
     804 *      before the first option.
     805 * after_list - (string) Html to be inserted after the last <option> and
     806 *      before </select>
     807 *
     808 * @since 3.1.2
     809 *
     810 * @param string $options an array of options for the dropdown
     811 */
     812function wp_user_role_selector( $args = array() ) {
     813        $defaults = array(
     814            'selected' => false,
     815            'name' => 'role',
     816            'id' => 'role',
     817            'class' => false,
     818            'no_role' => true,
     819            'before_list' => false,
     820            'after_list' => false
     821        );
     822       
     823        $args = wp_parse_args( $args, $defaults );
     824        extract( $args );
     825       
     826        $p = "<select name='$name' id='$id'" . ( $class ? '' : " class='$class'" ) . '>' . $before_list;
     827        $r = '';
     828
     829        if ( $no_role ) {
     830            if ( $selected )
     831                    $r = '<option value="">' . __('&mdash; No role for this site &mdash;') . '</option>';
     832            else
     833                    $r = '<option value="" selected="selected">' . __('&mdash; No role for this site &mdash;') . '</option>';
     834        }
     835       
     836        $r .= '</select>' . $after_list;
     837       
     838        echo apply_filters( 'user_role_selector', $p . wp_get_dropdown_roles( $selected ) . $r, $args );
     839}
     840
     841/**
    778842 * {@internal Missing Short Description}}
    779843 *
    780844 * @since 2.3.0
  • wp-admin/options-general.php

     
    115115<tr valign="top">
    116116<th scope="row"><label for="default_role"><?php _e('New User Default Role') ?></label></th>
    117117<td>
    118 <select name="default_role" id="default_role"><?php wp_dropdown_roles( get_option('default_role') ); ?></select>
     118    <?php wp_user_role_selector( array(
     119            'selected' => get_option('default_role'),
     120            'name' => 'default_role',
     121            'id' => 'default_role',
     122            'no_role' => true
     123        ) ); ?>
    119124</td>
    120125</tr>
    121126<?php } else { ?>
  • wp-admin/user-edit.php

     
    233233
    234234<?php if ( !IS_PROFILE_PAGE && !is_network_admin() ) : ?>
    235235<tr><th><label for="role"><?php _e('Role:') ?></label></th>
    236 <td><select name="role" id="role">
     236<td>
    237237<?php
    238238// Get the highest/primary role for this user
    239239// TODO: create a function that does this: wp_get_user_role()
    240240$user_roles = $profileuser->roles;
    241 $user_role = array_shift($user_roles);
    242241
    243242// print the full list of roles with the primary one selected.
    244 wp_dropdown_roles($user_role);
    245 
    246 // print the 'no role' option. Make it selected if the user has no role yet.
    247 if ( $user_role )
    248         echo '<option value="">' . __('&mdash; No role for this site &mdash;') . '</option>';
    249 else
    250         echo '<option value="" selected="selected">' . __('&mdash; No role for this site &mdash;') . '</option>';
     243wp_user_role_selector(array(
     244    'selected' => $user_roles
     245));
    251246?>
    252 </select>
    253 <?php endif; //!IS_PROFILE_PAGE
     247</td>
     248<?php
     249endif; //!IS_PROFILE_PAGE
    254250
    255251if ( is_multisite() && is_network_admin() && ! IS_PROFILE_PAGE && current_user_can( 'manage_network_options' ) && !isset($super_admins) ) { ?>
    256252<tr><th><label for="role"><?php _e('Super Admin'); ?></label></th>
  • wp-admin/user-new.php

     
    243243        </tr>
    244244        <tr class="form-field">
    245245                <th scope="row"><label for="adduser-role"><?php _e('Role'); ?></label></th>
    246                 <td><select name="role" id="adduser-role">
    247                         <?php wp_dropdown_roles( get_option('default_role') ); ?>
    248                         </select>
     246                <td>
     247                        <?php
     248                             wp_user_role_selector( array(
     249                                 'selected' => get_option('default_role'),
     250                                 'name' => 'role',
     251                                 'id' => 'adduser-role',
     252                                 'no_role' => false
     253                             ));
     254                        ?>
     255                       
    249256                </td>
    250257        </tr>
    251258<?php if ( is_super_admin() ) { ?>
     
    323330<?php } // !is_multisite ?>
    324331        <tr class="form-field">
    325332                <th scope="row"><label for="role"><?php _e('Role'); ?></label></th>
    326                 <td><select name="role" id="role">
     333                <td>
    327334                        <?php
    328335                        if ( !$new_user_role )
    329336                                $new_user_role = !empty($current_role) ? $current_role : get_option('default_role');
    330                         wp_dropdown_roles($new_user_role);
     337                            wp_user_role_selector(array(
     338                                'selected' => $new_user_role,
     339                                'no_role' => false
     340                            ));
    331341                        ?>
    332                         </select>
     342               
    333343                </td>
    334344        </tr>
    335345        <?php if ( is_multisite() && is_super_admin() ) { ?>