| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 22 |
|
|---|
| 23 |
function check_user_editable_roles($role_names) { |
|---|
| 24 |
global $wp_roles; |
|---|
| 25 |
foreach ($wp_roles->roles as $role => $details) : |
|---|
| 26 |
foreach ($details['capabilities'] as $capability => $value) : |
|---|
| 27 |
if (!current_user_can($capability)) : |
|---|
| 28 |
unset ($role_names[$role]); |
|---|
| 29 |
break; |
|---|
| 30 |
endif; |
|---|
| 31 |
endforeach; |
|---|
| 32 |
endforeach; |
|---|
| 33 |
return $role_names; |
|---|
| 34 |
} |
|---|
| 35 |
add_filter('role_names_listing', 'check_user_editable_roles'); |
|---|
| 36 |
|
|---|
| 37 |
|
|---|
| 38 |
|
|---|
| 39 |
|
|---|
| 40 |
|
|---|
| 41 |
|
|---|
| 42 |
|
|---|
| 43 |
|
|---|
| 44 |
|
|---|
| 45 |
|
|---|
| 46 |
|
|---|
| 47 |
|
|---|
| 48 |
|
|---|
| 49 |
|
|---|
| 50 |
function check_user_editable($allcaps, $caps, $args) { |
|---|
| 51 |
|
|---|
| 52 |
// also, only if there is a second argument in $args (the second, edited, user) |
|---|
| 53 |
if ($caps[0] == 'edit_users' && $args[2]) : |
|---|
| 54 |
|
|---|
| 55 |
|
|---|
| 56 |
global $user_object, $wp_roles; |
|---|
| 57 |
|
|---|
| 58 |
|
|---|
| 59 |
// note: that means we are on user_edit.php and not users.php |
|---|
| 60 |
|
|---|
| 61 |
if (!$user_object) $user_object = new wp_user($args[2]); |
|---|
| 62 |
|
|---|
| 63 |
if ($user_object->ID != $args[2]) return $allcaps; |
|---|
| 64 |
|
|---|
| 65 |
$edited_user_caps = $user_object->allcaps; |
|---|
| 66 |
$edited_user_roles = $user_object->roles; |
|---|
| 67 |
$checked_roles = array(); |
|---|
| 68 |
|
|---|
| 69 |
|
|---|
| 70 |
foreach ($edited_user_roles as $role => $name) : |
|---|
| 71 |
$rolecaps = $wp_roles->roles[$name]['capabilities']; |
|---|
| 72 |
foreach($rolecaps as $capability => $value) : |
|---|
| 73 |
if (!current_user_can($capability)) : |
|---|
| 74 |
unset ($allcaps['edit_users']); |
|---|
| 75 |
return $allcaps; |
|---|
| 76 |
endif; |
|---|
| 77 |
endforeach; |
|---|
| 78 |
// add the role to a list of checked roles if there are no problems |
|---|
| 79 |
$checked_roles[] = $name; |
|---|
| 80 |
endforeach; |
|---|
| 81 |
|
|---|
| 82 |
// This only runs if there were no conflicts while checking roles |
|---|
| 83 |
// go through the edited users caps and check if current user has them all |
|---|
| 84 |
foreach ($edited_user_caps as $capability => $value) : |
|---|
| 85 |
if (in_array($capability, $checked_roles)) : |
|---|
| 86 |
continue; |
|---|
| 87 |
elseif (!current_user_can($capability)) : |
|---|
| 88 |
unset ($allcaps['edit_users']); |
|---|
| 89 |
return $allcaps; |
|---|
| 90 |
endif; |
|---|
| 91 |
endforeach; |
|---|
| 92 |
endif; |
|---|
| 93 |
|
|---|
| 94 |
return $allcaps; |
|---|
| 95 |
} |
|---|
| 96 |
|
|---|
| 97 |
add_filter('user_has_cap', 'check_user_editable', 10, 3) |
|---|
| 98 |
|
|---|
| 99 |
|
|---|
| 100 |
?> |
|---|