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 caps_are_subset($user_caps, $query_caps, $args) { |
---|
12 | $managed_cap = 'external_edit_users_real'; |
---|
13 | |
---|
14 | global $current_user; |
---|
15 | $target_user = new WP_User($args[2]); |
---|
16 | $action = $args[3]; |
---|
17 | $target_state = $args[4]; |
---|
18 | |
---|
19 | if(in_array($managed_cap, $query_caps)){ |
---|
20 | switch($action){ |
---|
21 | case 'adduser': |
---|
22 | if(compare_caps($current_user->roles, $target_state) === 1){ |
---|
23 | $user_caps[$managed_cap] = 1; |
---|
24 | } |
---|
25 | break; |
---|
26 | case 'promote': |
---|
27 | if(compare_caps($current_user->roles, $target_state) === 1){ |
---|
28 | if($target_user->id === 0){ |
---|
29 | $user_caps[$managed_cap] = 1; |
---|
30 | } elseif (compare_caps($current_user->roles, $target_user->roles) === 1){ |
---|
31 | $user_caps[$managed_cap] = 1; |
---|
32 | } |
---|
33 | } |
---|
34 | break; |
---|
35 | case 'edit': |
---|
36 | if(compare_caps($current_user->roles, $target_user->roles) === 1){ |
---|
37 | $user_caps[$managed_cap] = 1; |
---|
38 | } |
---|
39 | break; |
---|
40 | case 'delete': |
---|
41 | case 'dodelete': |
---|
42 | default: |
---|
43 | if(compare_caps($current_user->roles, $target_user->roles) === 1){ |
---|
44 | $user_caps[$managed_cap] = 1; |
---|
45 | } |
---|
46 | break; |
---|
47 | } |
---|
48 | |
---|
49 | return $user_caps; |
---|
50 | } else { |
---|
51 | return $user_caps; |
---|
52 | } |
---|
53 | |
---|
54 | return $args; |
---|
55 | } |
---|
56 | |
---|
57 | function compare_caps($role1, $role2){ |
---|
58 | //make life easy on ourselves... |
---|
59 | $caps1 = array(); |
---|
60 | $caps2 = array(); |
---|
61 | global $wp_roles; |
---|
62 | |
---|
63 | if(is_array($role1)){ |
---|
64 | foreach($role1 as $r1){ |
---|
65 | $caps1 = array_merge($wp_roles->roles[$r1]['capabilities'], $caps1); |
---|
66 | } |
---|
67 | } else { |
---|
68 | $caps1 = $wp_roles->roles[$role1]['capabilities']; |
---|
69 | } |
---|
70 | if(is_array($role2)){ |
---|
71 | foreach($role2 as $r2){ |
---|
72 | $caps2 = array_merge($wp_roles->roles[$r2]['capabilities'], $caps2); |
---|
73 | } |
---|
74 | } else { |
---|
75 | $caps2 = $wp_roles->roles[$role2]['capabilities']; |
---|
76 | } |
---|
77 | |
---|
78 | //if role1 and role2 are equal, return 0 |
---|
79 | if(array_diff_assoc($caps1, $caps2) == array() && array_diff_assoc($caps2, $caps1) == array()){ |
---|
80 | return 0; |
---|
81 | } |
---|
82 | |
---|
83 | //if role2 is a proper subset of role1, return 1 |
---|
84 | if(array_diff_assoc($caps2, array_intersect_assoc($caps1, $caps2)) == array()){ |
---|
85 | return 1; |
---|
86 | } |
---|
87 | |
---|
88 | //if role1 is a proper subset of role2, return -1 |
---|
89 | if(array_diff_assoc($caps1, array_intersect_assoc($caps2, $caps1)) == array()){ |
---|
90 | return -1; |
---|
91 | } |
---|
92 | |
---|
93 | //if role1 and role2 have exclusive elements, return 0 |
---|
94 | return 0; |
---|
95 | } |
---|
96 | |
---|
97 | // Now we set that function up to execute when the admin_footer action is called |
---|
98 | add_filter('user_has_cap', 'caps_are_subset', 1, 3); |
---|
99 | |
---|
100 | ?> |
---|