ajax-processor.php
10.2 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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
<?php
/*
* User Role Editor WordPress plugin
* Author: Vladimir Garagulya
* Email: support@role-editor.com
* License: GPLv2 or later
*/
/**
* Process AJAX request from User Role Editor
*
* @author vladimir
*/
class URE_Ajax_Processor {
protected $lib = null;
protected $action = null;
protected $debug = null;
public function __construct( ) {
$this->lib = URE_Lib::get_instance();
$this->debug = ( defined('WP_PHP_UNIT_TEST') && WP_PHP_UNIT_TEST==true );
}
// end of __construct()
protected function get_action() {
$action = $this->lib->get_request_var( 'sub_action', 'post' );
if ( empty( $action ) ) {
$action = $this->lib->get_request_var( 'sub_action', 'get' );
}
return $action;
}
// end of get_action()
protected function get_required_cap() {
if ( $this->action=='grant_roles' || $this->action=='get_user_roles' ) {
$cap = 'promote_users';
} else {
$cap = URE_Own_Capabilities::get_key_capability();
}
return $cap;
}
// end of get_required_cap()
protected function valid_nonce() {
if ( !isset( $_REQUEST['wp_nonce'] ) || !wp_verify_nonce( $_REQUEST['wp_nonce'], 'user-role-editor' ) ) {
echo json_encode( array('result'=>'error', 'message'=>'URE: Wrong or expired request') );
return false;
} else {
return true;
}
}
// end of check_nonce()
protected function user_can() {
$capability = $this->get_required_cap();
if ( !current_user_can( $capability ) ) {
echo json_encode( array('result'=>'error', 'message'=>'URE: Insufficient permissions') );
return false;
} else {
return true;
}
}
// end of check_user_cap()
protected function add_role() {
$editor = URE_Editor::get_instance();
$response = $editor->add_new_role();
$answer = array(
'result'=>$response['result'],
'role_id'=>$response['role_id'],
'role_name'=>$response['role_name'],
'message'=>$response['message']
);
return $answer;
}
// end of add_role()
protected function update_role() {
$editor = URE_Editor::get_instance();
$response = $editor->update_role();
$answer = array(
'result'=>$response['result'],
'role_id'=>$response['role_id'],
'role_name'=>$response['role_name'],
'message'=>$response['message']
);
return $answer;
}
// end of add_role()
protected function add_capability() {
$response = URE_Capability::add( 'role' );
$editor = URE_Editor::get_instance();
$editor->init1();
$message = $editor->init_current_role_name();
if ( empty( $message ) ) {
$view = new URE_View();
$html = $view->_show_capabilities( true, true );
} else {
$html = '';
$response['result'] = 'error';
$response['message'] = $message;
}
$answer = array('result'=>$response['result'], 'html'=>$html, 'message'=>$response['message']);
return $answer;
}
// end of add_capability()
protected function delete_capability() {
$result = URE_Capability::delete();
if ( is_array( $result ) ) {
$notification = $result['message'];
$deleted_caps = $result['deleted_caps'];
} else {
$notification = $result;
$deleted_caps = array();
}
$answer = array('result'=>'success', 'deleted_caps'=>$deleted_caps, 'message'=>$notification);
return $answer;
}
// end of delete_cap()
protected function delete_role() {
$editor = URE_Editor::get_instance();
$response = $editor->delete_role();
$answer = array(
'result'=>$response['result'],
'message'=>$response['message'],
'deleted_roles'=> $response['deleted_roles']
);
return $answer;
}
// end of delete_role()
protected function rename_role() {
$editor = URE_Editor::get_instance();
$response = $editor->rename_role();
$answer = array(
'result'=>$response['result'],
'message'=>$response['message'],
'role_id'=> $response['role_id'],
'role_name'=>$response['role_name']
);
return $answer;
}
// end of rename_role()
protected function get_caps_to_remove() {
$html = URE_Role_View::caps_to_remove_html();
$answer = array('result'=>'success', 'html'=>$html, 'message'=>'success');
return $answer;
}
// end of get_caps_to_remove()
protected function get_users_without_role() {
$new_role = $this->lib->get_request_var( 'new_role', 'post' );
if ( empty( $new_role ) ) {
$answer = array('result'=>'error', 'message'=>'Provide new role');
return $answer;
}
$assign_role = $this->lib->get_assign_role();
if ( $new_role==='no_rights') {
$assign_role->create_no_rights_role();
}
$wp_roles = wp_roles();
if ( !isset( $wp_roles->roles[$new_role] ) ) {
$answer = array('result'=>'error', 'message'=>'Selected new role does not exist');
return $answer;
}
$users = $assign_role->get_users_without_role();
$answer = array(
'result'=>'success',
'users'=>$users,
'new_role'=>$new_role,
'message'=>'success'
);
return $answer;
}
// end of get_users_without_role()
protected function grant_roles() {
$answer = URE_Grant_Roles::grant_roles();
return $answer;
}
// end of grant_roles()
protected function get_user_roles() {
$answer = URE_Grant_Roles::get_user_roles();
return $answer;
}
// end of get_user_roles()
protected function get_role_caps() {
$role = $this->lib->get_request_var('role', 'post' );
if ( empty( $role ) ) {
$answer = array('result'=>'error', 'message'=>'Provide role ID');
return $answer;
}
$wp_roles = wp_roles();
if ( !isset( $wp_roles->roles[$role] ) ) {
$answer = array('result'=>'error', 'message'=>'Requested role does not exist');
return $answer;
}
$active_items = URE_Role_Additional_Options::get_active_items();
if ( isset( $active_items[$role] ) ) {
$role_options = $active_items[$role];
} else {
$role_options = array();
}
$caps = array();
foreach( $wp_roles->roles[$role]['capabilities'] as $cap_id=>$allowed ) {
$cap = URE_Capability::escape( $cap_id );
$caps[$cap] = $allowed;
}
$answer = array(
'result'=>'success',
'message'=>'Role capabilities retrieved successfully',
'role_id'=>$role,
'role_name'=>$wp_roles->roles[$role]['name'],
'caps'=>$caps,
'options'=>$role_options
);
return $answer;
}
// end of get_role_caps()
protected function hide_pro_banner() {
$this->lib->put_option('ure_hide_pro_banner', 1);
$this->lib->flush_options();
$answer = array(
'result'=>'success',
'message'=>'Pro banner was hidden'
);
return $answer;
}
// end of hide_pro_banner()
protected function _dispatch() {
switch ( $this->action ) {
case 'update_role':
$answer = $this->update_role();
break;
case 'add_role':
$answer = $this->add_role();
break;
case 'add_capability':
$answer = $this->add_capability();
break;
case 'delete_capability':
$answer = $this->delete_capability();
break;
case 'delete_role':
$answer = $this->delete_role();
break;
case 'get_caps_to_remove':
$answer = $this->get_caps_to_remove();
break;
case 'get_users_without_role':
$answer = $this->get_users_without_role();
break;
case 'grant_roles':
$answer = $this->grant_roles();
break;
case 'get_user_roles':
$answer = $this->get_user_roles();
break;
case 'get_role_caps':
$answer = $this->get_role_caps();
break;
case 'rename_role':
$answer = $this->rename_role();
break;
case 'hide_pro_banner':
$answer = $this->hide_pro_banner();
break;
default:
$answer = array('result' => 'error', 'message' => 'Unknown action "' . $this->action . '"');
}
return $answer;
}
// end of _dispatch()
/**
* AJAX requests dispatcher
*/
public function dispatch() {
$this->action = $this->get_action();
if ( !$this->valid_nonce() || !$this->user_can() ) {
die;
}
$answer = $this->_dispatch();
$json_answer = json_encode($answer);
echo $json_answer;
die;
}
// end of dispatch()
}
// end of URE_Ajax_Processor