capability.php
7.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
<?php
/**
* Class to work with user capability
*
* @package User-Role-Editor
* @subpackage Admin
* @author Vladimir Garagulya <support@role-editor.com>
* @copyright Copyright (c) 2010 - 2021, Vladimir Garagulya
**/
class URE_Capability {
public static function escape( $cap_id ) {
$search = array(' ', '/', '|', '{', '}', '$');
$replace = array('_', '_', '_', '_', '_', '_');
$cap_id_esc = str_replace( $search, $replace, $cap_id );
return $cap_id_esc;
}
// end escape()
// Sanitize user input for security
// do not allow to use internally used capabilities
public static function validate( $cap_id_raw ) {
$match = array();
$found = preg_match('/[A-Za-z0-9_\-]*/', $cap_id_raw, $match );
if ( !$found || ($found && ( $match[0]!=$cap_id_raw ) ) ) { // some non-alphanumeric charactes found!
$data = array(
'result'=>false,
'message'=>esc_html__('Error: Capability name must contain latin characters and digits only!', 'user-role-editor'),
'cap_id'=>''
);
return $data;
}
$cap_id = strtolower( $match[0] );
if ( $cap_id=='do_not_allow' ) {
$data = array(
'result'=>false,
'message'=>esc_html__('Error: this capability is used internally by WordPress', 'user-role-editor'),
'cap_id'=>'do_not_allow'
);
return $data;
}
if ( $cap_id=='administrator' ) {
$data = array(
'result'=>false,
'message'=>esc_html__('Error: this word is used by WordPress as a role ID', 'user-role-editor'),
'cap_id'=>'administrator'
);
return $data;
}
$data = array(
'result'=>true,
'message'=>'Success',
'cap_id'=>$cap_id
);
return $data;
}
// end of validate()
/**
* Add new user capability
*
* @global WP_Roles $wp_roles
* @return string
*/
public static function add( $ure_object ) {
global $wp_roles;
$response = array(
'result'=>'error',
'capability_id'=>'',
'html'=>'',
'message'=>''
);
if ( !current_user_can( 'ure_create_capabilities' ) ) {
$response['message'] = esc_html__( 'Insufficient permissions to work with User Role Editor', 'user-role-editor' );
return $response;
}
$mess = '';
if ( !isset( $_POST['capability_id'] ) || empty( $_POST['capability_id'] ) ) {
$response['message'] = esc_html__( 'Wrong Request', 'user-role-editor' );
return $response;
}
$data = self::validate( $_POST['capability_id'] );
if ( !$data['result'] ) {
$response['message'] = $data['message'];
return $response;
}
$cap_id = $data['cap_id'];
$lib = URE_Lib::get_instance();
$full_capabilities = $lib->init_full_capabilities( $ure_object );
if ( !isset( $full_capabilities[$cap_id] ) ) {
$admin_role = $lib->get_admin_role();
$use_db = $wp_roles->use_db;
$wp_roles->use_db = true;
$wp_roles->add_cap( $admin_role, $cap_id );
$wp_roles->use_db = $use_db;
$response['result'] = 'success';
$response['message'] = sprintf( esc_html__( 'Capability %s was added successfully', 'user-role-editor' ), $cap_id );
} else {
$response['message'] = sprintf( esc_html__( 'Capability %s exists already', 'user-role-editor' ), $cap_id );
}
return $response;
}
// end of add()
/**
* Extract capabilities selected for deletion from the $_POST global
*
* @return array
*/
private static function get_caps_for_deletion_from_post( $caps_allowed_to_remove ) {
if ( isset( $_POST['values'] ) ) {
$input_buff = $_POST['values'];
} else {
$input_buff = $_POST;
}
$caps = array();
foreach( $input_buff as $key=>$value ) {
if ( substr( $key, 0, 3 )!=='rm_' ) {
continue;
}
if ( !isset( $caps_allowed_to_remove[$value]) ) {
continue;
}
$caps[] = $value;
}
return $caps;
}
// end of get_caps_for_deletion_from_post()
private static function revoke_caps_from_user( $user_id, $caps ) {
$user = get_user_to_edit( $user_id );
foreach( $caps as $cap_id ) {
if ( !isset( $user->caps[$cap_id] ) ) {
continue;
}
// Prevent sudden revoke role 'administrator' from a user during 'administrator' capability deletion.
if ( $cap_id=='administrator') {
continue;
}
$user->remove_cap( $cap_id );
}
}
// end of revoke_caps_from_user()
private static function revoke_caps_from_role( $wp_role, $caps ) {
foreach( $caps as $cap_id ) {
if ( $wp_role->has_cap( $cap_id ) ) {
$wp_role->remove_cap( $cap_id );
}
}
}
// end of revoke_caps_from_role()
private static function revoke_caps( $caps ) {
global $wpdb, $wp_roles;
// remove caps from users
$users_ids = $wpdb->get_col("SELECT $wpdb->users.ID FROM $wpdb->users");
foreach ( $users_ids as $user_id ) {
self::revoke_caps_from_user( $user_id, $caps );
}
// remove caps from roles
foreach ( $wp_roles->role_objects as $wp_role ) {
self::revoke_caps_from_role( $wp_role, $caps );
}
}
// end of revoke_caps()
/**
* Delete capability
*
* @global WP_Roles $wp_roles
* @return string - information message
*/
public static function delete() {
if ( !current_user_can( 'ure_delete_capabilities' ) ) {
return esc_html__( 'Insufficient permissions to work with User Role Editor','user-role-editor' );
}
$capabilities = URE_Capabilities::get_instance();
$mess = '';
$caps_allowed_to_remove = $capabilities->get_caps_to_remove();
if ( !is_array( $caps_allowed_to_remove ) || count( $caps_allowed_to_remove )==0 ) {
return esc_html__( 'There are no capabilities available for deletion!', 'user-role-editor' );
}
$caps = self::get_caps_for_deletion_from_post( $caps_allowed_to_remove );
if ( empty( $caps ) ) {
return esc_html__( 'There are no capabilities available for deletion!', 'user-role-editor' );
}
self::revoke_caps( $caps );
if ( count( $caps )==1 ) {
$mess = sprintf( esc_html__( 'Capability %s was removed successfully', 'user-role-editor' ), $caps[0] );
} else {
$lib = URE_Lib::get_instance();
$short_list_str = $lib->get_short_list_str( $caps );
$mess = count( $caps ) .' '. esc_html__( 'capabilities were removed successfully', 'user-role-editor' ) .': '.
$short_list_str;
}
// Escape every capability ID to remove from the HTML markup related div by ID
$esc_caps = array();
foreach( $caps as $key=>$cap ) {
$esc_caps[$key] = self::escape( $cap );
}
return array('message'=>$mess, 'deleted_caps'=>$esc_caps);
}
// end of delete()
}
// end of class URE_Capability