Make WordPress Core

Ticket #17924: wp-3.6-alpha-multi-role-diff.patch

File wp-3.6-alpha-multi-role-diff.patch, 13.2 KB (added by mobius5150, 12 years ago)

A patch prepared against trunk of WP 3.6-alpha

  • 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() {
     
    140142        <div class="alignleft actions">
    141143                <?php if ( current_user_can( 'promote_users' ) ) : ?>
    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
    148152                        submit_button( __( 'Change' ), 'button', 'changeit', false );
    149153                endif;
     
    197201
    198202                $style = '';
    199203                foreach ( $this->items as $userid => $user_object ) {
    200                         if ( count( $user_object->roles ) <= 1 ) {
    201                                 $role = reset( $user_object->roles );
    202                         } elseif ( $roles = array_intersect( array_values( $user_object->roles ), $editable_roles ) ) {
    203                                 $role = reset( $roles );
    204                         } else {
    205                                 $role = reset( $user_object->roles );
    206                         }
    207 
    208204                        if ( is_multisite() && empty( $user_object->allcaps ) )
    209205                                continue;
    210206
     
    267263                } else {
    268264                        $edit = '<strong>' . $user_object->user_login . '</strong>';
    269265                }
    270                 $role_name = isset( $wp_roles->role_names[$role] ) ? translate_user_role( $wp_roles->role_names[$role] ) : __( 'None' );
     266
     267                if ( !isset( $role ) || $role == '' )
     268                    $role = $user_object->roles;
     269                if ( !is_array($role) )
     270                    $role = array( $role );
     271               
     272                $role_name = apply_filters( 'user_role_name' , $role, $role );
     273               
    271274                $avatar = get_avatar( $user_object->ID, 32 );
    272275
    273276                $r = "<tr id='user-$user_object->ID'$style>";
     
    321324
    322325                return $r;
    323326        }
     327
     328        /**
     329         * Formats the role name for the WP_Users_List table role column.
     330         *
     331         * @param type string $role the string that is being filtered.
     332         * @param type array $roles the array of roles
     333         * @return type string
     334         */
     335        static function wp_user_role_name( $role, $roles ) {
     336            global $wp_roles;
     337            return isset( $wp_roles->role_names[$roles[0]] ) ? translate_user_role( $wp_roles->role_names[$roles[0]] ) : __( 'None' );
    324338}
     339}
  • wp-admin/includes/template.php

     
    749749}
    750750
    751751/**
    752  * Print out <option> html elements for role selectors
     752 * Get the <option> html elements for role selectors
    753753 *
    754754 * @since 2.1.0
    755  *
     755 * @deprecated 3.6 this function is deprecated. User wp_user_role_selector instead.
     756 * @uses serves as a wrapper for wp_get_dropdown_roles
    756757 * @param string $selected slug for the role that should be already selected
    757758 */
    758759function wp_dropdown_roles( $selected = false ) {
     760        echo wp_get_dropdown_roles( $selected );
     761}
     762
     763
     764/**
     765 * Get the <option> html elements for role selectors
     766 *
     767 * @since 3.6
     768 *
     769 * @param string $selected slug for the role that should be already selected
     770 * @return string
     771 */
     772function wp_get_dropdown_roles( $selected = false ) {
     773
    759774        $p = '';
    760775        $r = '';
    761776
    762777        $editable_roles = get_editable_roles();
    763778
    764779        foreach ( $editable_roles as $role => $details ) {
    765                 $name = translate_user_role($details['name'] );
    766                 if ( $selected == $role ) // preselect specified role
     780                $name = translate_user_role( $details['name'] );
     781               
     782                // preselect specified role
     783                if ( (is_string($selected) && $selected == $role) || (is_array($selected) && in_array($role, $selected)) )
    767784                        $p = "\n\t<option selected='selected' value='" . esc_attr($role) . "'>$name</option>";
    768785                else
    769786                        $r .= "\n\t<option value='" . esc_attr($role) . "'>$name</option>";
    770787        }
    771         echo $p . $r;
     788       
     789        return $p . $r;
    772790}
    773791
    774792/**
     793 * Print out the dropdown role selector
     794 *
     795 * selected - (string) The role that is to be selected in the dropdown
     796 * name - (string) The html name element of the selector
     797 * id - (string) The html id element of the selector
     798 * class - (string) The classes to be applied to the selector
     799 * no_role - (bool) Whether an option should be shown to select no role
     800 * before_list - (string) Html to be inserted after the <select> and
     801 *      before the first option.
     802 * after_list - (string) Html to be inserted after the last <option> and
     803 *      before </select>
     804 *
     805 * @since 3.6
     806 *
     807 * @param string $options an array of options for the dropdown
     808 */
     809function wp_user_role_selector( $args = array() ) {
     810        $defaults = array(
     811            'selected' => false,
     812            'name' => 'role',
     813            'id' => 'role',
     814            'class' => false,
     815            'no_role' => true,
     816            'before_list' => false,
     817            'after_list' => false
     818        );
     819       
     820        $args = wp_parse_args( $args, $defaults );
     821        extract( $args );
     822       
     823        $p = "<select name='$name' id='$id'" . ( $class ? '' : " class='$class'" ) . '>' . $before_list;
     824        $r = '';
     825
     826        if ( $no_role ) {
     827            if ( $selected !== false )
     828                    $r = '<option value="">' . __('&mdash; No role for this site &mdash;') . '</option>';
     829            else
     830                    $r = '<option value="" selected="selected">' . __('&mdash; No role for this site &mdash;') . '</option>';
     831        }
     832       
     833        $r .= '</select>' . $after_list;
     834       
     835        echo apply_filters( 'user_role_selector', $p . wp_get_dropdown_roles( $selected ) . $r, $args );
     836}
     837
     838
     839/**
    775840 * Outputs the form used by the importers to accept the data to be imported
    776841 *
    777842 * @since 2.0.0
  • wp-admin/includes/user.php

     
    66 * @subpackage Administration
    77 */
    88
     9function wp_sanitize_user_role( $role, $user_id ){
     10    global $wp_roles;
     11    if ( is_string( $role ) ) {
     12        $r = false;
     13        $new_role = sanitize_text_field( $role );
     14        $potential_role = isset($wp_roles->role_objects[$new_role]) ? $wp_roles->role_objects[$new_role] : false;
     15        // Don't let anyone with 'edit_users' (admins) edit their own role to something without it.
     16        // Multisite super admins can freely edit their blog roles -- they possess all caps.
     17        if ( ( is_multisite() && current_user_can( 'manage_sites' ) ) || $user_id != get_current_user_id() || ($potential_role && $potential_role->has_cap( 'edit_users' ) ) )
     18                $r = $new_role;
     19
     20        // If the new role isn't editable by the logged-in user die with error
     21        $editable_roles = get_editable_roles();
     22        if ( ! empty( $new_role ) && empty( $editable_roles[$new_role] ) )
     23                wp_die(__('You can&#8217;t give users that role.'));
     24       
     25        return $r;
     26    } else {
     27        //Can't do anything, so return the input
     28        return $role;
     29    }
     30}
     31
    932/**
     33 * Add the sanitize_user_role filter
     34 */
     35add_filter( 'sanitize_user_role', 'wp_sanitize_user_role', 5, 2 );
     36
     37/**
    1038 * Creates a new user from the "Users" form using $_POST information.
    1139 *
    1240 * @since 2.0
     
    4977                $pass2 = $_POST['pass2'];
    5078
    5179        if ( isset( $_POST['role'] ) && current_user_can( 'edit_users' ) ) {
    52                 $new_role = sanitize_text_field( $_POST['role'] );
    53                 $potential_role = isset($wp_roles->role_objects[$new_role]) ? $wp_roles->role_objects[$new_role] : false;
    54                 // Don't let anyone with 'edit_users' (admins) edit their own role to something without it.
    55                 // Multisite super admins can freely edit their blog roles -- they possess all caps.
    56                 if ( ( is_multisite() && current_user_can( 'manage_sites' ) ) || $user_id != get_current_user_id() || ($potential_role && $potential_role->has_cap( 'edit_users' ) ) )
    57                         $user->role = $new_role;
    58 
    59                 // If the new role isn't editable by the logged-in user die with error
    60                 $editable_roles = get_editable_roles();
    61                 if ( ! empty( $new_role ) && empty( $editable_roles[$new_role] ) )
    62                         wp_die(__('You can&#8217;t give users that role.'));
     80                $user->role = apply_filters( 'sanitize_user_role', $_POST['role'], $user_id );
    6381        }
    6482
    6583        if ( isset( $_POST['email'] ))
  • wp-admin/options-general.php

     
    125125<tr valign="top">
    126126<th scope="row"><label for="default_role"><?php _e('New User Default Role') ?></label></th>
    127127<td>
    128 <select name="default_role" id="default_role"><?php wp_dropdown_roles( get_option('default_role') ); ?></select>
     128    <?php wp_user_role_selector( array(
     129            'selected' => get_option('default_role'),
     130            'name' => 'default_role',
     131            'id' => 'default_role',
     132            'no_role' => true
     133        ) ); ?>
    129134</td>
    130135</tr>
    131136<?php } else { ?>
  • wp-admin/user-edit.php

     
    247247
    248248<?php if ( !IS_PROFILE_PAGE && !is_network_admin() ) : ?>
    249249<tr><th><label for="role"><?php _e('Role') ?></label></th>
    250 <td><select name="role" id="role">
     250<td>
    251251<?php
    252252// Compare user role against currently editable roles
    253253// TODO: create a function that does this: wp_get_user_role()
    254254$user_roles = array_intersect( array_values( $profileuser->roles ), array_keys( get_editable_roles() ) );
    255 $user_role  = array_shift( $user_roles );
    256255
    257256// print the full list of roles with the primary one selected.
    258 wp_dropdown_roles($user_role);
    259 
    260 // print the 'no role' option. Make it selected if the user has no role yet.
    261 if ( $user_role )
    262         echo '<option value="">' . __('&mdash; No role for this site &mdash;') . '</option>';
    263 else
    264         echo '<option value="" selected="selected">' . __('&mdash; No role for this site &mdash;') . '</option>';
     257wp_user_role_selector(array(
     258    'selected' => $user_roles
     259));
    265260?>
    266 </select></td></tr>
    267 <?php endif; //!IS_PROFILE_PAGE
     261</td>
     262<?php
     263endif; //!IS_PROFILE_PAGE
    268264
    269265if ( is_multisite() && is_network_admin() && ! IS_PROFILE_PAGE && current_user_can( 'manage_network_options' ) && !isset($super_admins) ) { ?>
    270266<tr><th><?php _e('Super Admin'); ?></th>
  • wp-admin/user-new.php

     
    278278        </tr>
    279279        <tr class="form-field">
    280280                <th scope="row"><label for="adduser-role"><?php _e('Role'); ?></label></th>
    281                 <td><select name="role" id="adduser-role">
    282                         <?php wp_dropdown_roles( get_option('default_role') ); ?>
    283                         </select>
     281                <td>
     282                        <?php
     283                             wp_user_role_selector( array(
     284                                 'selected' => get_option('default_role'),
     285                                 'name' => 'role',
     286                                 'id' => 'adduser-role',
     287                                 'no_role' => false
     288                             ));
     289                        ?>
     290                       
    284291                </td>
    285292        </tr>
    286293<?php if ( is_super_admin() ) { ?>
     
    358365<?php } // !is_multisite ?>
    359366        <tr class="form-field">
    360367                <th scope="row"><label for="role"><?php _e('Role'); ?></label></th>
    361                 <td><select name="role" id="role">
     368                <td>
    362369                        <?php
    363370                        if ( !$new_user_role )
    364371                                $new_user_role = !empty($current_role) ? $current_role : get_option('default_role');
    365                         wp_dropdown_roles($new_user_role);
     372                            wp_user_role_selector(array(
     373                                'selected' => $new_user_role,
     374                                'no_role' => false
     375                            ));
    366376                        ?>
    367                         </select>
     377               
    368378                </td>
    369379        </tr>
    370380        <?php if ( is_multisite() && is_super_admin() ) { ?>
  • wp-includes/user.php

     
    14081408        }
    14091409
    14101410        if ( isset($role) )
    1411                 $user->set_role($role);
     1411                do_action( 'apply_user_role' , apply_filters( 'pre_user_role', $role ), $user );
    14121412        elseif ( !$update )
    1413                 $user->set_role(get_option('default_role'));
     1413                do_action( 'apply_user_role' , get_option('default_role'), $user );
    14141414
    14151415        wp_cache_delete($user_id, 'users');
    14161416        wp_cache_delete($user_login, 'userlogins');
     
    14241424}
    14251425
    14261426/**
     1427 * Hooks into the apply_user_role action to set the users role
     1428 *
     1429 * @param type $role the role to set
     1430 * @param type $user the user to set the role on
     1431 */
     1432function wp_apply_user_role( $role, $user ) {
     1433    if ( is_string( $role ) ) {
     1434        $user->set_role($role);
     1435    }
     1436}
     1437
     1438/**
     1439 * Register the action for the apply_user_role hook
     1440 */
     1441add_action( 'apply_user_role', 'wp_apply_user_role', 5, 2);
     1442
     1443/**
    14271444 * Update an user in the database.
    14281445 *
    14291446 * It is possible to update a user's password by specifying the 'user_pass'
     1447 *
     1448 * It is possible to update a user's password by specifying the 'user_pass'
    14301449 * value in the $userdata parameter array.
    14311450 *
    14321451 * If $userdata does not contain an 'ID' key, then a new user will be created