Make WordPress Core

Ticket #17924: Replace_Admin_Role_Dropdown_3.2.1.patch

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

The first patch.

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

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

     
    754754<?php
    755755}
    756756
    757 
    758757/**
    759  * Print out <option> html elements for role selectors
     758 * Get the <option> html elements for role selectors
    760759 *
    761760 * @since 2.1.0
     761 * @deprecated 3.1.2 this function is deprecated. User wp_user_role_selector instead.
     762 * @uses serves as a wrapper for wp_get_dropdown_roles
     763 * @param string $selected slug for the role that should be already selected
     764 */
     765function wp_dropdown_roles( $selected = false ) {
     766        echo wp_get_dropdown_roles( $selected );
     767}
     768
     769
     770/**
     771 * Get the <option> html elements for role selectors
    762772 *
     773 * @since 3.1.2
     774 *
    763775 * @param string $selected slug for the role that should be already selected
     776 * @return string
    764777 */
    765 function wp_dropdown_roles( $selected = false ) {
     778function wp_get_dropdown_roles( $selected = false ) {
    766779        $p = '';
    767780        $r = '';
    768781
    769782        $editable_roles = get_editable_roles();
     783       
     784        if ( is_array( $selected ) )
     785            $selected = $selected[0];
    770786
    771787        foreach ( $editable_roles as $role => $details ) {
    772                 $name = translate_user_role($details['name'] );
     788                $name = translate_user_role( $details['name'] );
    773789                if ( $selected == $role ) // preselect specified role
    774790                        $p = "\n\t<option selected='selected' value='" . esc_attr($role) . "'>$name</option>";
    775791                else
    776792                        $r .= "\n\t<option value='" . esc_attr($role) . "'>$name</option>";
    777793        }
    778         echo $p . $r;
     794        return $p . $r;
    779795}
    780796
     797
    781798/**
     799 * Print out the dropdown role selector
     800 *
     801 * selected - (string) The role that is to be selected in the dropdown
     802 * name - (string) The html name element of the selector
     803 * id - (string) The html id element of the selector
     804 * class - (string) The classes to be applied to the selector
     805 * no_role - (bool) Whether an option should be shown to select no role
     806 * before_list - (string) Html to be inserted after the <select> and
     807 *      before the first option.
     808 * after_list - (string) Html to be inserted after the last <option> and
     809 *      before </select>
     810 *
     811 * @since 3.1.2
     812 *
     813 * @param string $options an array of options for the dropdown
     814 */
     815function wp_user_role_selector( $args = array() ) {
     816        $defaults = array(
     817            'selected' => false,
     818            'name' => 'role',
     819            'id' => 'role',
     820            'class' => false,
     821            'no_role' => true,
     822            'before_list' => false,
     823            'after_list' => false
     824        );
     825       
     826        $args = wp_parse_args( $args, $defaults );
     827        extract( $args );
     828       
     829        $p = "<select name='$name' id='$id'" . ( $class ? '' : " class='$class'" ) . '>' . $before_list;
     830        $r = '';
     831
     832        if ( $no_role ) {
     833            if ( $selected )
     834                    $r = '<option value="">' . __('&mdash; No role for this site &mdash;') . '</option>';
     835            else
     836                    $r = '<option value="" selected="selected">' . __('&mdash; No role for this site &mdash;') . '</option>';
     837        }
     838       
     839        $r .= '</select>' . $after_list;
     840       
     841        echo apply_filters( 'user_role_selector', $p . wp_get_dropdown_roles( $selected ) . $r, $args );
     842}
     843
     844/**
    782845 * {@internal Missing Short Description}}
    783846 *
    784847 * @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

     
    240240
    241241<?php if ( !IS_PROFILE_PAGE && !is_network_admin() ) : ?>
    242242<tr><th><label for="role"><?php _e('Role:') ?></label></th>
    243 <td><select name="role" id="role">
     243<td>
    244244<?php
    245245// Get the highest/primary role for this user
    246246// TODO: create a function that does this: wp_get_user_role()
    247247$user_roles = $profileuser->roles;
    248 $user_role = array_shift($user_roles);
    249248
    250249// print the full list of roles with the primary one selected.
    251 wp_dropdown_roles($user_role);
    252 
    253 // print the 'no role' option. Make it selected if the user has no role yet.
    254 if ( $user_role )
    255         echo '<option value="">' . __('&mdash; No role for this site &mdash;') . '</option>';
    256 else
    257         echo '<option value="" selected="selected">' . __('&mdash; No role for this site &mdash;') . '</option>';
     250wp_user_role_selector(array(
     251    'selected' => $user_roles
     252));
    258253?>
    259 </select>
    260 <?php endif; //!IS_PROFILE_PAGE
     254</td>
     255<?php
     256endif; //!IS_PROFILE_PAGE
    261257
    262258if ( is_multisite() && is_network_admin() && ! IS_PROFILE_PAGE && current_user_can( 'manage_network_options' ) && !isset($super_admins) ) { ?>
    263259<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() ) { ?>