Skip to content

Commit 1053ca7

Browse files
committed
Better handling of users with no role. Props Mark Jaquith. WordPress#2809
git-svn-id: https://develop.svn.wordpress.org/trunk@3859 602fd350-edb4-49c9-b593-d223f7449a82
1 parent a4c408a commit 1053ca7

4 files changed

Lines changed: 26 additions & 7 deletions

File tree

wp-admin/user-edit.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,11 +103,22 @@
103103
<?php
104104
// print_r($profileuser);
105105
echo '<select name="role">';
106+
$role_list = '';
107+
$user_has_role = false;
106108
foreach($wp_roles->role_names as $role => $name) {
107-
$selected = ($profileuser->has_cap($role)) ? ' selected="selected"' : '';
108-
echo "<option value=\"{$role}\"{$selected}>{$name}</option>";
109+
if ( $profileuser->has_cap($role) ) {
110+
$selected = ' selected="selected"';
111+
$user_has_role = true;
112+
} else {
113+
$selected = '';
114+
}
115+
$role_list .= "<option value=\"{$role}\"{$selected}>{$name}</option>";
109116
}
110-
echo '</select>';
117+
if ( $user_has_role )
118+
$role_list .= '<option value="">' . __('&mdash; No role for this blog &mdash;') . '</option>';
119+
else
120+
$role_list .= '<option value="" selected="selected">' . __('&mdash; No role for this blog &mdash;') . '</option>';
121+
echo $role_list . '</select>';
111122
?></label></p>
112123

113124
<p><label><?php _e('First name:') ?><br />

wp-admin/users.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,11 @@
313313
?>
314314

315315
<tr>
316+
<?php if ( !empty($role) ) : ?>
316317
<th colspan="7" align="left"><h3><?php echo $wp_roles->role_names[$role]; ?></h3></th>
318+
<?php else : ?>
319+
<th colspan="7" align="left"><h3><em><?php _e('No role for this blog'); ?></h3></th>
320+
<?php endif; ?>
317321
</tr>
318322
<tr class="thead">
319323
<th style="text-align: left"><?php _e('ID') ?></th>

wp-includes/capabilities.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ function get_role_caps() {
174174

175175
//Build $allcaps from role caps, overlay user's $caps
176176
$this->allcaps = array();
177-
foreach($this->roles as $role) {
177+
foreach( (array) $this->roles as $role) {
178178
$role = $wp_roles->get_role($role);
179179
$this->allcaps = array_merge($this->allcaps, $role->capabilities);
180180
}
@@ -199,8 +199,12 @@ function remove_role($role) {
199199
function set_role($role) {
200200
foreach($this->roles as $oldrole)
201201
unset($this->caps[$oldrole]);
202-
$this->caps[$role] = true;
203-
$this->roles = array($role => true);
202+
if ( !empty($role) ) {
203+
$this->caps[$role] = true;
204+
$this->roles = array($role => true);
205+
} else {
206+
$this->roles = false;
207+
}
204208
update_usermeta($this->id, $this->cap_key, $this->caps);
205209
$this->get_role_caps();
206210
$this->update_user_level_from_caps();

wp-includes/registration-functions.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ function wp_insert_user($userdata) {
102102
update_usermeta( $user_id, 'aim', $aim );
103103
update_usermeta( $user_id, 'yim', $yim );
104104

105-
if ($update && !empty($role)) {
105+
if ( $update ) {
106106
$user = new WP_User($user_id);
107107
$user->set_role($role);
108108
}

0 commit comments

Comments
 (0)