user-other-roles.php
13.5 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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
<?php
/*
* Project: User Role Editor WordPress plugin
* Class for Assigning to a user multiple roles
* Author: Vladimir Garagulya
* Author email: support@role-editor.com
* Author URI: https://www.role-editor.com
* License: GPL v2+
*
*/
class URE_User_Other_Roles {
protected $lib = null;
private static $counter = 0;
function __construct() {
$this->lib = URE_Lib::get_instance();
$this->set_hooks();
}
// end of $lib
public function set_hooks() {
add_filter( 'additional_capabilities_display', array($this, 'additional_capabilities_display'), 10, 1);
add_action( 'admin_print_styles-user-edit.php', array($this, 'load_css') );
add_action( 'admin_print_styles-user-new.php', array($this, 'load_css') );
add_action( 'admin_enqueue_scripts', array($this, 'load_js' ) );
add_action( 'edit_user_profile', array($this, 'edit_user_profile_html'), 10, 1 );
add_action( 'user_new_form', array($this, 'user_new_form'), 10, 1 );
add_action( 'profile_update', array($this, 'update'), 10 );
$multisite = $this->lib->get('multisite');
if ($multisite) {
add_action( 'wpmu_activate_user', array($this, 'add_other_roles'), 10, 1 );
add_action( 'added_existing_user', array($this, 'add_other_roles'), 10, 1);
}
add_action( 'user_register', array($this, 'add_other_roles'), 10, 1 );
}
// end of set_hooks()
public function additional_capabilities_display( $display ) {
$show = apply_filters('ure_show_additional_capabilities_section', true);
if ( empty( $show ) ) {
return $display;
}
if ( !current_user_can('promote_users') ) {
return $display; // No permissions to promote users
}
$display = false;
return $display;
}
// end of additional_capabilities_display()
/*
* Load CSS for the user profile edit page
*/
public function load_css() {
$show = apply_filters('ure_show_additional_capabilities_section', true );
if ( empty( $show ) ) {
return;
}
if ( !current_user_can('promote_users') ) {
return; // No permissions to promote users
}
if ( defined('WP_DEBUG') && !empty( WP_DEBUG ) ) {
$file_name = 'multiple-select.css';
} else {
$file_name = 'multiple-select.min.css';
}
wp_enqueue_style('wp-jquery-ui-dialog');
wp_enqueue_style('ure-jquery-multiple-select', plugins_url('/css/'. $file_name, URE_PLUGIN_FULL_PATH ), array(), false, 'screen');
}
// end of load_css()
public function load_js($hook_suffix) {
if ( !in_array( $hook_suffix, array('user-edit.php', 'user-new.php') ) ) {
return;
}
$show = apply_filters('ure_show_additional_capabilities_section', true );
if ( empty( $show ) ) {
return;
}
if ( !current_user_can('promote_users') ) {
return; // No permissions to promote users
}
if ( defined('WP_DEBUG') && !empty( WP_DEBUG ) ) {
$ms_file_name = 'multiple-select.js';
} else {
$ms_file_name = 'multiple-select.min.js';
}
$select_primary_role = apply_filters('ure_users_select_primary_role', true);
wp_enqueue_script('jquery-ui-dialog', '', array('jquery-ui-core', 'jquery-ui-button', 'jquery'));
wp_register_script('ure-jquery-multiple-select', plugins_url('/js/'. $ms_file_name, URE_PLUGIN_FULL_PATH ), array(), URE_VERSION );
wp_enqueue_script('ure-jquery-multiple-select');
wp_register_script('ure-user-profile-other-roles', plugins_url('/js/user-profile-other-roles.js', URE_PLUGIN_FULL_PATH ), array(), URE_VERSION );
wp_enqueue_script('ure-user-profile-other-roles');
wp_localize_script('ure-user-profile-other-roles', 'ure_data_user_profile_other_roles', array(
'wp_nonce' => wp_create_nonce('user-role-editor'),
'other_roles' => esc_html__('Other Roles', 'user-role-editor'),
'select_roles' => esc_html__('Select additional roles for this user', 'user-role-editor'),
'select_primary_role' => ($select_primary_role || $this->lib->is_super_admin()) ? 1: 0
));
}
// end of load_js()
/**
* Returns list of user roles, except 1st one, and bbPress assigned as they are shown by WordPress and bbPress themselves.
*
* @param type $user WP_User from wp-includes/capabilities.php
* @return array
*/
public function get_roles_array( $user ) {
if ( !is_array( $user->roles ) || count( $user->roles )<=1 ) {
return array();
}
// get bbPress assigned user role
if ( function_exists( 'bbp_filter_blog_editable_roles' ) ) {
$bb_press_role = bbp_get_user_role( $user->ID );
} else {
$bb_press_role = '';
}
$roles = array();
foreach ( $user->roles as $role) {
if (!empty($bb_press_role) && $bb_press_role === $role) {
// exclude bbPress assigned role
continue;
}
$roles[] = $role;
}
array_shift( $roles ); // exclude primary role which is shown by WordPress itself
return $roles;
}
// end of get_roles_array()
private function roles_select_html($user, $context) {
global $wp_roles;
$user_roles = $user->roles;
$primary_role = array_shift($user_roles);
$roles = apply_filters('editable_roles', $wp_roles->roles); // exclude restricted roles if any
$roles = array_reverse( $roles );
if (isset($roles[$primary_role])) { // exclude role assigned to the user as a primary role
unset($roles[$primary_role]);
}
$button_number = (self::$counter>0) ? '_2': '';
echo '<select multiple="multiple" id="ure_select_other_roles'. $button_number .'" name="ure_select_other_roles" style="width: 500px;" >'."\n";
foreach($roles as $key=>$role) {
echo '<option value="'.$key.'" >'.$role['name'].'</option>'."\n";
} // foreach()
echo '</select><br>'."\n";
if ($context=='add-new-user' || $context=='add-existing-user') {
// Get other default roles
$other_roles = $this->lib->get_option('other_default_roles', array());
} else {
$other_roles = $this->get_roles_array($user);
}
if (is_array($other_roles) && count($other_roles) > 0) {
$other_roles_str = implode(',', $other_roles);
} else {
$other_roles_str = '';
}
echo '<input type="hidden" name="ure_other_roles" id="ure_other_roles'. $button_number .'" value="' . $other_roles_str . '" />';
$output = $this->lib->roles_text($other_roles);
echo '<span id="ure_other_roles_list'. $button_number .'">'. $output .'</span>';
self::$counter++;
}
// end of roles_select()
/**
* Returns comma separated string of capabilities directly (not through the roles) assigned to the user
*
* @global WP_Roles $wp_roles
* @param object $user
* @return string
*/
private function get_user_caps_str( $user ) {
global $wp_roles;
$output = '';
foreach ($user->caps as $cap => $value) {
if (!$wp_roles->is_role($cap)) {
if ('' != $output) {
$output .= ', ';
}
$output .= $value ? $cap : sprintf(__('Denied: %s'), $cap);
}
}
return $output;
}
// end of get_user_caps_str()
private function user_profile_capabilities($user) {
$current_user_id = get_current_user_id();
$user_caps = $this->get_user_caps_str( $user );
?>
<tr>
<th>
<?php esc_html_e('Capabilities', 'user-role-editor'); ?>
</th>
<td>
<?php
echo $user_caps .'<br/>';
if ($this->lib->user_is_admin( $current_user_id ) ) {
echo '<a href="' . wp_nonce_url("users.php?page=users-".URE_PLUGIN_FILE."&object=user&user_id={$user->ID}", "ure_user_{$user->ID}") . '">' .
esc_html__('Edit', 'user-role-editor') . '</a>';
}
?>
</td>
</tr>
<?php
}
// end of user_profile_capabilities()
private function display($user, $context) {
?>
<table class="form-table">
<tr>
<th scope="row"><?php esc_html_e('Other Roles', 'user-role-editor'); ?></th>
<td>
<?php
$this->roles_select_html($user, $context);
?>
</td>
</tr>
<?php
if ($context=='user-edit') {
$this->user_profile_capabilities($user);
}
?>
</table>
<?php
}
// end of display()
private function is_user_profile_extention_allowed() {
// Check if we are not at the network admin center
$result = stripos($_SERVER['REQUEST_URI'], 'network/user-edit.php') == false;
return $result;
}
// end of is_user_profile_extention_allowed()
/**
* Add URE stuff to the edit user profile page
*
* @param object $user
* @return void
*/
public function edit_user_profile_html($user) {
if (!$this->is_user_profile_extention_allowed()) {
return;
}
$show = apply_filters('ure_show_additional_capabilities_section', true);
if (empty($show)) {
return;
}
if ( !current_user_can('promote_users') ) {
return; // No permissions to promote users
}
?>
<h3><?php esc_html_e('Additional Capabilities', 'user-role-editor'); ?></h3>
<?php
$this->display($user, 'user-edit');
}
// end of edit_user_profile_html()
public function user_new_form($context) {
$show = apply_filters('ure_show_additional_capabilities_section', true);
if (empty($show)) {
return;
}
if ( !current_user_can('promote_users') ) {
return; // No permissions to promote users
}
$user = new WP_User();
?>
<table>
<?php
$this->display($user, $context);
?>
</table>
<?php
}
// end of user_new_form()
/*
* Save additional user roles when user profile is updated, as WordPress itself doesn't know about them
* Returns different numbers for automatic testing purpose
*/
public function update( $user_id ) {
if ( !current_user_can('promote_users') ) {
return -1; // No permissions to promote users
}
if ( !current_user_can('edit_user', $user_id) ) {
return -1; // No permissions to edit this user
}
if ( !isset( $_POST['ure_other_roles'] ) ) {
return 3; // Add default other roles, there is no related data at the POST
}
if ( empty( $_POST['ure_other_roles'] ) ) {
return 1; // There is no need in processing of other roles. User did not select them
}
$user = get_userdata( $user_id );
$data = explode(',', str_replace(' ', '', $_POST['ure_other_roles'] ) );
$editable_roles = get_editable_roles();
$ure_other_roles = array();
foreach( $data as $role_id ) {
if ( empty( $role_id ) ) {
continue;
}
if ( !isset( $editable_roles[ $role_id ] ) ) {
return -2; // If the role isn't editable by the current user, stop processing - no permission to assign this role.
}
if ( is_array( $user->roles ) && !in_array( $role_id, $user->roles ) ) {
$ure_other_roles[] = $role_id;
}
}
foreach( $ure_other_roles as $role ) {
$user->add_role( $role );
}
return 2;
}
// end of update()
public function add_default_other_roles( $user_id ) {
if ( empty( $user_id ) ) {
return false;
}
$user = get_user_by('id', $user_id );
if ( empty( $user->ID ) ) {
return true;
}
// Get default roles if any
$other_default_roles = $this->lib->get_option('other_default_roles', array() );
if ( count( $other_default_roles ) == 0 ) {
return true;
}
foreach ( $other_default_roles as $role ) {
if ( !isset( $user->caps[$role] ) ) {
$user->add_role( $role );
}
}
}
// end of add_default_other_roles()
public function add_other_roles( $user_id ) {
if ( empty( $user_id ) ) {
return false;
}
$result = $this->update( $user_id );
if ( $result==3 ) { // Other roles were not selected manually
$this->add_default_other_roles( $user_id );
}
}
// end of add_other_roles()
}
// end of URE_User_Other_Roles class