1 | <?php |
---|
2 | /* |
---|
3 | Plugin Name:lesscaps |
---|
4 | Plugin URI: http://www.creighton.edu |
---|
5 | Description: I'll fill you in on this later. Don't worry about it, it'll be fine. |
---|
6 | Author: Joel vanBrandwijk |
---|
7 | Version: .00a |
---|
8 | Author URI: http://www.creighton.edu |
---|
9 | */ |
---|
10 | |
---|
11 | function userEdit_cap_map($cap, $user_id, $args){ |
---|
12 | $args=$args[0]; |
---|
13 | if($cap == 'edit_users'){ |
---|
14 | //if we don't have any arguments, then we're asking a situation-free question. Assume the best |
---|
15 | if(count($args) === 0){ |
---|
16 | return Array(); |
---|
17 | } |
---|
18 | |
---|
19 | //get current and target user objects for later use |
---|
20 | global $current_user; |
---|
21 | $target_user = new WP_User($args['target_id']); |
---|
22 | |
---|
23 | switch($args['action']){ |
---|
24 | case 'adduser': |
---|
25 | if(compare_caps($current_user->roles, $args['target_state']) === 1){ |
---|
26 | return Array(); |
---|
27 | } |
---|
28 | break; |
---|
29 | case 'promote': |
---|
30 | if(compare_caps($current_user->roles, $args['target_state']) === 1){ |
---|
31 | if($target_user->id === 0){ |
---|
32 | return Array(); |
---|
33 | } elseif (compare_caps($current_user->roles, $target_user->roles) === 1){ |
---|
34 | return Array(); |
---|
35 | } |
---|
36 | } |
---|
37 | break; |
---|
38 | case 'edit': |
---|
39 | if(compare_caps($current_user->roles, $target_user->roles) === 1){ |
---|
40 | return Array(); |
---|
41 | } |
---|
42 | break; |
---|
43 | case 'delete': |
---|
44 | case 'dodelete': |
---|
45 | default: |
---|
46 | if(compare_caps($current_user->roles, $target_user->roles) === 1) { |
---|
47 | return Array(); |
---|
48 | } |
---|
49 | break; |
---|
50 | } |
---|
51 | } else { |
---|
52 | return false; |
---|
53 | } |
---|
54 | } |
---|
55 | |
---|
56 | function compare_caps($role1, $role2){ |
---|
57 | //make life easy on ourselves... |
---|
58 | $caps1 = array(); |
---|
59 | $caps2 = array(); |
---|
60 | global $wp_roles; |
---|
61 | |
---|
62 | if(is_array($role1)){ |
---|
63 | foreach($role1 as $r1){ |
---|
64 | $caps1 = array_merge($wp_roles->roles[$r1]['capabilities'], $caps1); |
---|
65 | } |
---|
66 | } else { |
---|
67 | $caps1 = $wp_roles->roles[$role1]['capabilities']; |
---|
68 | } |
---|
69 | if(is_array($role2)){ |
---|
70 | foreach($role2 as $r2){ |
---|
71 | $caps2 = array_merge($wp_roles->roles[$r2]['capabilities'], $caps2); |
---|
72 | } |
---|
73 | } else { |
---|
74 | $caps2 = $wp_roles->roles[$role2]['capabilities']; |
---|
75 | } |
---|
76 | |
---|
77 | //if role1 and role2 are equal, return 0 |
---|
78 | if(array_diff_assoc($caps1, $caps2) == array() && array_diff_assoc($caps2, $caps1) == array()){ |
---|
79 | return 0; |
---|
80 | } |
---|
81 | |
---|
82 | //if role2 is a proper subset of role1, return 1 |
---|
83 | if(array_diff_assoc($caps2, array_intersect_assoc($caps1, $caps2)) == array()){ |
---|
84 | return 1; |
---|
85 | } |
---|
86 | |
---|
87 | //if role1 is a proper subset of role2, return -1 |
---|
88 | if(array_diff_assoc($caps1, array_intersect_assoc($caps2, $caps1)) == array()){ |
---|
89 | return -1; |
---|
90 | } |
---|
91 | |
---|
92 | //if role1 and role2 have exclusive elements, return 0 |
---|
93 | return 0; |
---|
94 | } |
---|
95 | |
---|
96 | add_filter('maps_cap', 'userEdit_cap_map', 1, 3); |
---|
97 | ?> |
---|