PluginProbe
User Profile Builder – Beautiful User Registration Forms, User Profiles & User Role Editor / 3.16.0
User Profile Builder – Beautiful User Registration Forms, User Profiles & User Role Editor v3.16.0
4.0.3 4.0.2 4.0.1 4.0.0 3.16.6 3.16.5 3.16.4 3.16.3 3.16.2 3.16.1 3.16.0 3.15.9 3.9.9 3.9.5 3.9.6 3.9.7 3.9.8 1.1.7 1.1.8 1.1.9 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 All 341 releases
profile-builder / features / roles-editor / roles-editor.php

roles-editor.php in User Profile Builder – Beautiful User Registration Forms, User Profiles & User Role Editor 3.16.0, at features/roles-editor/roles-editor.php

1,433 lines 60.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
3
4 class WPPB_Roles_Editor {
5
6 function __construct() {
7
8 // Create Roles Editor CPT
9 add_action( 'init', array( $this, 'create_roles_editor_cpt' ) );
10
11 // Add User Roles entry under PB menu as well
12 add_action( 'init', array( $this, 'submenu_page' ), 25 );
13
14 add_action( 'admin_init', array( $this, 'redirect_to_roles_editor' ), 25 );
15
16 // Create a Roles Editor CPT post for every existing role
17 add_action( 'current_screen', array( $this, 'create_post_for_role' ) );
18
19 // Edit CPT page
20 add_filter( 'manage_wppb-roles-editor_posts_columns', array( $this, 'add_extra_column_for_roles_editor_cpt' ) );
21 add_action( 'manage_wppb-roles-editor_posts_custom_column', array( $this, 'custom_column_content_for_roles_editor_cpt' ), 10, 2 );
22
23 // Add and remove meta boxes
24 add_action( 'add_meta_boxes', array( $this, 'register_meta_boxes' ), 1 );
25
26 // Edit Publish meta box
27 add_action( 'post_submitbox_misc_actions', array( $this, 'edit_publish_meta_box' ) );
28
29 // Enqueue scripts and styles
30 add_action( 'admin_enqueue_scripts', array( $this, 'scripts_admin' ) );
31
32 // Add role slug to the created post
33 add_action( 'save_post', array( $this, 'add_post_meta' ), 10, 2 );
34
35 add_filter( 'wp_insert_post_data', array( $this, 'modify_post_title'), '99', 1 );
36
37 add_action( 'wp_ajax_delete_capability_permanently', array( $this, 'delete_capability_permanently' ) );
38 add_action( 'wp_ajax_update_role_capabilities', array( $this, 'update_role_capabilities' ) );
39 add_action( 'wp_ajax_get_role_capabilities', array( $this, 'get_role_capabilities' ) );
40
41 add_filter( 'months_dropdown_results', array( $this, 'remove_filter_by_month_dropdown' ), 10, 2 );
42 add_filter( 'post_row_actions', array( $this, 'modify_list_row_actions' ), 10, 2 );
43
44 add_action( 'before_delete_post', array( $this, 'delete_role_permanently' ), 10 );
45
46 add_filter( 'bulk_actions-edit-wppb-roles-editor', '__return_empty_array' );
47 add_filter( 'views_edit-wppb-roles-editor', array( $this, 'edit_cpt_quick_links' ) );
48
49 add_filter( 'enter_title_here', array( $this, 'change_title_text' ) );
50 add_filter( 'post_updated_messages', array( $this, 'change_post_updated_messages' ) );
51
52 // Add multiple roles checkbox to back-end Add / Edit User (as admin)
53 add_action( 'load-user-new.php', array( $this, 'actions_on_user_new' ) );
54 add_action( 'load-user-edit.php', array( $this, 'actions_on_user_edit' ) );
55
56 add_action( 'load-profile.php', array( $this, 'actions_on_user_new' ) );
57 add_action( 'load-profile.php', array( $this, 'actions_on_user_edit' ) );
58
59 // Intercept account activation on multisites
60 if ( is_multisite() ) {
61 // Add the user to the blog
62 add_action( 'add_user_to_blog', array( $this, 'ms_add_user_to_blog' ), 10, 3 );
63 }
64 }
65
66 function submenu_page(){
67
68 $args = array(
69 'menu_title' => __( 'Roles Editor', 'profile-builder' ),
70 'page_title' => __( 'Roles Editor', 'profile-builder' ),
71 'menu_slug' => 'wppb-roles-editor',
72 'page_type' => 'submenu_page',
73 'capability' => 'delete_users',
74 'priority' => 12,
75 'parent_slug' => 'profile-builder'
76 );
77
78 if( class_exists( 'WCK_Page_Creator_PB' ) ) {
79 new WCK_Page_Creator_PB( $args );
80 }
81
82 }
83
84 function redirect_to_roles_editor(){
85 if( isset( $_GET['page'] ) && $_GET['page'] == 'wppb-roles-editor' ){
86 wp_redirect( admin_url( 'edit.php?post_type=wppb-roles-editor' ) );
87 die();
88 }
89 }
90
91 function scripts_admin() {
92
93 global $post_type;
94 global $current_screen;
95 global $post;
96
97 if( $post_type == 'wppb-roles-editor' ) {
98 wp_enqueue_script( 'wppb_select2_js', WPPB_PLUGIN_URL .'assets/js/select2/select2.min.js', array( 'jquery' ), PROFILE_BUILDER_VERSION );
99 wp_enqueue_style( 'wppb_select2_css', WPPB_PLUGIN_URL .'assets/css/select2/select2.min.css', array(), PROFILE_BUILDER_VERSION );
100
101 wp_enqueue_script( 'wppb_roles_editor_js', plugin_dir_url( __FILE__ ) .'assets/js/roles-editor.js', array( 'jquery', 'wppb_select2_js' ), PROFILE_BUILDER_VERSION );
102 wp_enqueue_style( 'wppb_roles_editor_css', plugin_dir_url( __FILE__ ) .'assets/css/roles-editor.css', array(), PROFILE_BUILDER_VERSION );
103
104 if( $current_screen->id == 'wppb-roles-editor' ) {
105 $role_slug = $this->sanitize_role( get_post_meta( $post->ID, 'wppb_role_slug', true ) );
106 $current_role = get_role( $role_slug );
107 $current_user = wp_get_current_user();
108
109 if( isset( $current_role ) && is_array( $current_role->capabilities ) ) {
110 $role_capabilities = $current_role->capabilities;
111
112 // True if current user got this role
113 if( isset( $role_slug ) && in_array( $role_slug, $current_user->roles ) ) {
114 $current_user_role = TRUE;
115 } else {
116 $current_user_role = FALSE;
117 }
118
119 // Get current role users count
120 $current_role_users_count = $this->count_role_users( $current_role->name );
121 } else {
122 $role_capabilities = NULL;
123 $current_role_users_count = NULL;
124 $current_user_role = FALSE;
125 }
126 } else {
127 $role_capabilities = NULL;
128 $current_role_users_count = NULL;
129 $current_user_role = FALSE;
130 }
131
132 // Remove old WordPress levels system
133 // Use filter and return FALSE if you need the old levels capability system
134 $remove_old_levels = apply_filters( 'wppb_remove_old_levels', TRUE );
135 if( $remove_old_levels === TRUE ) {
136 $role_capabilities = $this->remove_old_labels( $role_capabilities );
137 }
138
139 $admin_capabilities = array(
140 'manage_options',
141 'activate_plugins',
142 'delete_plugins',
143 'install_plugins',
144 'manage_network_options',
145 'manage_network',
146 'manage_network_plugins',
147 'upload_plugins'
148 );
149
150 $group_capabilities = $this->group_capabilities();
151 $hidden_capabilities = NULL;
152
153 $remove_hidden_capabilities = apply_filters( 'wppb_re_remove_hidden_caps', TRUE );
154 if( $remove_hidden_capabilities === TRUE ) {
155 $group_capabilities['general']['capabilities'] = array_diff( $group_capabilities['general']['capabilities'], $this->get_hidden_capabilities() );
156 $group_capabilities['appearance']['capabilities'] = array_diff( $group_capabilities['appearance']['capabilities'], $this->get_hidden_capabilities() );
157 $group_capabilities['plugins']['capabilities'] = array_diff( $group_capabilities['plugins']['capabilities'], $this->get_hidden_capabilities() );
158 $group_capabilities['post_types']['attachment']['capabilities'] = array_diff( $group_capabilities['post_types']['attachment']['capabilities'], $this->get_hidden_capabilities() );
159
160 if( $role_capabilities !== NULL ) {
161 $role_capabilities = array_diff_key( $role_capabilities, $this->get_hidden_capabilities() );
162 }
163
164 $hidden_capabilities = $this->get_hidden_capabilities();
165 if( empty( $hidden_capabilities ) ) {
166 $hidden_capabilities = NULL;
167 }
168 }
169
170 $all_capabilities = $this->get_all_capabilities();
171
172 $custom_capabilities = get_option( 'wppb_roles_editor_capabilities', 'not_set' );
173 if( $custom_capabilities != 'not_set' && ! empty( $custom_capabilities['custom']['capabilities'] ) ) {
174 foreach( $custom_capabilities['custom']['capabilities'] as $custom_capability_key => $custom_capability ) {
175 if( ! in_array( $custom_capability, $all_capabilities ) ) {
176 $all_capabilities[$custom_capability] = $custom_capability;
177 }
178 }
179 }
180
181 $vars_array = array(
182 'ajaxUrl' => admin_url( 'admin-ajax.php' ),
183 'current_screen_action' => $current_screen->action,
184 'capabilities' => $group_capabilities,
185 'current_role_capabilities' => $role_capabilities,
186 'current_role_users_count' => $current_role_users_count,
187 'all_capabilities' => $all_capabilities,
188 'current_user_role' => $current_user_role,
189 'admin_capabilities' => $admin_capabilities,
190 'hidden_capabilities' => $hidden_capabilities,
191 'default_role_text' => esc_html__( 'Default Role', 'profile-builder' ),
192 'your_role_text' => esc_html__( 'Your Role', 'profile-builder' ),
193 'role_name_required_error_text' => esc_html__( 'Role name is required.', 'profile-builder' ),
194 'no_capabilities_found_text' => esc_html__( 'No capabilities found.', 'profile-builder' ),
195 'select2_placeholder_text' => esc_html__( 'Select capabilities', 'profile-builder' ),
196 'delete_permanently_text' => esc_html__( 'Delete Permanently', 'profile-builder' ),
197 'capability_perm_delete_text' => esc_html__( "This will permanently delete the capability from your site and from every user role.\n\nIt can't be undone!", 'profile-builder' ),
198 'new_cap_update_title_text' => esc_html__( 'This capability is not saved until you click Update!', 'profile-builder' ),
199 'new_cap_publish_title_text' => esc_html__( 'This capability is not saved until you click Publish!', 'profile-builder' ),
200 'delete_text' => esc_html__( 'Delete', 'profile-builder' ),
201 'cancel_text' => esc_html__( 'Cancel', 'profile-builder' ),
202 'add_new_capability_text' => esc_html__( 'Add New Capability', 'profile-builder' ),
203 'capability_text' => esc_html__( 'Capability', 'profile-builder' ),
204 'cap_no_delete_text' => esc_html__( 'You can\'t delete this capability from your role.', 'profile-builder' )
205 );
206
207 wp_localize_script( 'wppb_roles_editor_js', 'wppb_roles_editor_data', $vars_array );
208 }
209
210 }
211
212 function count_role_users( $current_role_name ) {
213
214 // Get current role users count
215 $user_count = count_users();
216
217 if( isset( $user_count['avail_roles'][$current_role_name] ) ) {
218 $current_role_users_count = $user_count['avail_roles'][$current_role_name];
219 } else {
220 $current_role_users_count = NULL;
221 }
222
223 return $current_role_users_count;
224 }
225
226 function get_role_capabilities() {
227
228 if( ! current_user_can( 'delete_users' ) ) {
229 die();
230 }
231
232 check_ajax_referer( 'wppb-re-ajax-nonce', 'security' );
233
234 $role = isset( $_POST['role'] ) ? get_role( sanitize_text_field( $_POST['role'] ) ) : null;
235
236 if( isset( $role ) && ! empty( $role->capabilities ) ) {
237 $role_capabilities = $role->capabilities;
238
239 // Remove old WordPress levels system
240 // Use filter and return FALSE if you need the old levels capability system
241 $remove_old_levels = apply_filters( 'wppb_remove_old_levels', TRUE );
242 if( $remove_old_levels === TRUE ) {
243 $role_capabilities = $this->remove_old_labels( $role_capabilities );
244 }
245
246 die( json_encode( $role_capabilities ) );
247 }
248
249 die( 'no_caps' );
250 }
251
252 function edit_cpt_quick_links( $views ) {
253
254 $edited_views = array();
255 $edited_views['all'] = $views['all'];
256
257 return $edited_views;
258
259 }
260
261 function create_roles_editor_cpt(){
262
263 if( is_admin() && current_user_can( 'delete_users' ) ) {
264 $labels = array(
265 'name' => esc_html__( 'Roles Editor', 'profile-builder' ),
266 'singular_name' => esc_html__( 'Roles Editor', 'profile-builder' ),
267 'add_new' => esc_html__( 'Add New Role', 'profile-builder' ),
268 'add_new_item' => esc_html__( 'Add New Role', 'profile-builder' ),
269 'edit_item' => esc_html__( 'Edit Role', 'profile-builder' ),
270 'new_item' => esc_html__( 'New Role', 'profile-builder' ),
271 'all_items' => esc_html__( 'Roles Editor', 'profile-builder' ),
272 'view_item' => esc_html__( 'View Role', 'profile-builder' ),
273 'search_items' => esc_html__( 'Search the Roles Editor', 'profile-builder' ),
274 'not_found' => esc_html__( 'No roles found', 'profile-builder' ),
275 'not_found_in_trash' => esc_html__( 'No roles found in trash', 'profile-builder' ),
276 'name_admin_bar' => esc_html__( 'Role', 'profile-builder' ),
277 'parent_item_colon' => '',
278 'menu_name' => esc_html__( 'Roles Editor', 'profile-builder' )
279 );
280
281 $args = array(
282 'labels' => $labels,
283 'public' => false,
284 'publicly_queryable' => false,
285 'show_ui' => true,
286 'query_var' => true,
287 'show_in_menu' => 'users.php',
288 'has_archive' => false,
289 'hierarchical' => false,
290 'capability_type' => 'post',
291 'supports' => array( 'title' )
292 );
293
294 register_post_type( 'wppb-roles-editor', $args );
295 }
296
297 }
298
299 function change_title_text( $title ) {
300
301 $screen = get_current_screen();
302
303 if( $screen->post_type == 'wppb-roles-editor' ) {
304 $title = esc_html__( 'Enter role name here', 'profile-builder' );
305 }
306
307 return $title;
308
309 }
310
311 function change_post_updated_messages( $messages ) {
312
313 global $post;
314
315 $messages['wppb-roles-editor'] = array(
316 0 => '',
317 1 => esc_html__( 'Role updated.', 'profile-builder' ),
318 2 => esc_html__( 'Custom field updated.', 'profile-builder' ),
319 3 => esc_html__( 'Custom field deleted.', 'profile-builder' ),
320 4 => esc_html__( 'Role updated.', 'profile-builder' ),
321 5 => isset( $_GET['revision'] ) ? sprintf( esc_html__( 'Role restored to revision from %s', 'profile-builder' ), wp_post_revision_title( (int) sanitize_text_field( $_GET['revision'] ), false ) ) : false,
322 6 => esc_html__( 'Role created.', 'profile-builder' ),
323 7 => esc_html__( 'Role saved.', 'profile-builder' ),
324 8 => esc_html__( 'Role submitted.', 'profile-builder' ),
325 9 => sprintf( esc_html__( 'Role scheduled for: <strong>%1$s</strong>', 'profile-builder' ), date_i18n( __( 'M j, Y @ G:i', 'profile-builder' ), strtotime( $post->post_date ) ) ),
326 10 => esc_html__( 'Role draft updated.', 'profile-builder' ),
327 );
328
329 return $messages;
330
331 }
332
333 function create_post_for_role() {
334
335 $screen = get_current_screen();
336
337 if( $screen->id == 'edit-wppb-roles-editor' ) {
338 global $wpdb;
339 global $wp_roles;
340
341 $added_posts = array();
342
343 $args = array(
344 'numberposts' => -1,
345 'post_type' => 'wppb-roles-editor'
346 );
347 $posts = get_posts( $args );
348
349 foreach( $posts as $key => $value ) {
350 $post_id = intval( $value->ID );
351 $role_slug_meta = $this->sanitize_role( get_post_meta( $post_id, 'wppb_role_slug', true ) );
352 if( ! empty( $role_slug_meta ) ) {
353 if( ! array_key_exists( $role_slug_meta, $wp_roles->role_names ) ) {
354 $post_meta = get_post_meta( $post_id );
355 foreach( $post_meta as $post_meta_key => $post_meta_value ) {
356 delete_post_meta( $post_id, $post_meta_key );
357 }
358
359 $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->posts WHERE post_type = %s AND ID = %d", "wppb-roles-editor", $post_id ) );
360 } else {
361 $added_posts[] = $role_slug_meta;
362 }
363 }
364 }
365
366 foreach( $wp_roles->role_names as $role_slug => $role_display_name ) {
367 if( ! in_array( $role_slug, $added_posts ) ) {
368 $id = wp_insert_post( array(
369 'post_title' => $role_display_name,
370 'post_type' => 'wppb-roles-editor',
371 'post_content' => '',
372 'post_status' => 'publish'
373 ) );
374
375 update_post_meta( $id, 'wppb_role_slug', $role_slug );
376 }
377 }
378 }
379
380 }
381
382 function add_extra_column_for_roles_editor_cpt( $columns ) {
383
384 $columns = array(
385 'title' => esc_html__( 'Role Name', 'profile-builder' ),
386 'role' => esc_html__( 'Role Slug', 'profile-builder' ),
387 'capabilities' => esc_html__( 'Capabilities', 'profile-builder' ),
388 'users' => esc_html__( 'Users', 'profile-builder' )
389 );
390
391 return apply_filters( 'wppb_manage_roles_columns', $columns );
392
393 }
394
395 function custom_column_content_for_roles_editor_cpt( $column_name, $post_id ) {
396
397 $role_slug = $this->sanitize_role( get_post_meta( $post_id, 'wppb_role_slug', true ) );
398
399 if( isset( $role_slug ) ) {
400 $role = get_role( $role_slug );
401
402 if( $column_name == 'role' ) {
403 echo '<input readonly spellcheck="false" type="text" class="wppb-role-slug-input input" value="'. esc_attr( $role_slug ) .'" />';
404 }
405
406 if( $column_name == 'capabilities' && isset( $role ) ) {
407 // Remove old WordPress levels system
408 // Use filter and return FALSE if you need the old levels capability system
409 $remove_old_levels = apply_filters( 'wppb_remove_old_levels', TRUE );
410 if( $remove_old_levels === TRUE ) {
411 $role_capabilities = $this->remove_old_labels( $role->capabilities );
412 } else {
413 $role_capabilities = $role->capabilities;
414 }
415
416 echo count( $role_capabilities );
417 }
418
419 if( $column_name == 'users' && isset( $role ) ) {
420 $role_users_count = $this->count_role_users( $role->name );
421
422 if( ! isset( $role_users_count ) ) {
423 $role_users_count = 0;
424 }
425
426 echo esc_html( $role_users_count );
427 }
428 }
429 }
430
431 function register_meta_boxes() {
432
433 remove_meta_box( 'slugdiv', 'wppb-roles-editor', 'normal' );
434 add_meta_box( 'wppb_edit_role_capabilities', esc_html__( 'Edit Role Capabilities', 'profile-builder' ), array( $this, 'edit_role_capabilities_meta_box_callback' ), 'wppb-roles-editor', 'normal', 'high' );
435
436 }
437
438 function edit_role_capabilities_meta_box_callback() {
439
440 ?>
441 <div id="wppb-role-edit-caps-div" style="margin: 15px 0 5px; 0;">
442 <div id="wppb-role-edit-add-caps " class="cozmoslabs-form-field-wrapper">
443 <select style="width: 40%; display: none;" class="wppb-capabilities-select" multiple="multiple"></select>
444
445 <input class="wppb-add-new-cap-input" type="text" placeholder="<?php esc_html_e( 'Add a new capability', 'profile-builder' ) ?>">
446
447 <a href="javascript:void(0)" class="button-primary" onclick="wppb_re_add_capability()">
448 <span><?php esc_html_e( 'Add Capability', 'profile-builder' ) ?></span>
449 </a>
450
451 <div id="wppb-add-new-cap-link">
452 <a class="wppb-add-new-cap-link" href="javascript:void(0)"><?php esc_html_e( 'Add New Capability', 'profile-builder' ) ?></a>
453 </div>
454
455 <span id="wppb-add-capability-error"><?php esc_html_e( 'Please select an existing capability or add a new one!', 'profile-builder' ) ?></span>
456 <span id="wppb-hidden-capability-error"><?php esc_html_e( 'You can\'t add a hidden capability!', 'profile-builder' ) ?></span>
457 <span id="wppb-duplicate-capability-error"><?php esc_html_e( 'This capability already exists!', 'profile-builder' ) ?></span>
458 </div>
459
460 <div class="wppb-role-edit-caps">
461 <ul id="wppb-capabilities-tabs">
462 <li class="wppb-role-editor-tab-title wppb-role-editor-tab-active">
463 <a href="javascript:void(0)" class="wppb-role-editor-tab wppb-role-editor-all" data-wppb-re-tab="all"><i class="dashicons dashicons-plus"></i> <span class="wppb-role-editor-tab-label"><?php esc_html_e( 'All', 'profile-builder' ) ?></span></a>
464 </li>
465
466 <li class="wppb-role-editor-tab-title">
467 <a href="javascript:void(0)" class="wppb-role-editor-tab wppb-role-editor-general" data-wppb-re-tab="general"><i class="dashicons dashicons-wordpress"></i> <span class="wppb-role-editor-tab-label"><?php esc_html_e( 'General', 'profile-builder' ) ?></span></a>
468 </li>
469
470 <li class="wppb-role-editor-tab-title">
471 <a href="javascript:void(0)" class="wppb-role-editor-tab wppb-role-editor-posts" data-wppb-re-tab="post"><i class="dashicons dashicons-admin-post"></i> <span class="wppb-role-editor-tab-label"><?php esc_html_e( 'Posts', 'profile-builder' ) ?></span></a>
472 </li>
473
474 <li class="wppb-role-editor-tab-title">
475 <a href="javascript:void(0)" class="wppb-role-editor-tab wppb-role-editor-pages" data-wppb-re-tab="page"><i class="dashicons dashicons-admin-page"></i> <span class="wppb-role-editor-tab-label"><?php esc_html_e( 'Pages', 'profile-builder' ) ?></span></a>
476 </li>
477
478 <li class="wppb-role-editor-tab-title">
479 <a href="javascript:void(0)" class="wppb-role-editor-tab wppb-role-editor-media" data-wppb-re-tab="attachment"><i class="dashicons dashicons-admin-media"></i> <span class="wppb-role-editor-tab-label"><?php esc_html_e( 'Media', 'profile-builder' ) ?></span></a>
480 </li>
481
482 <li class="wppb-role-editor-tab-title">
483 <a href="javascript:void(0)" class="wppb-role-editor-tab wppb-role-editor-taxonomies" data-wppb-re-tab="taxonomies"><i class="dashicons dashicons-tag"></i> <span class="wppb-role-editor-tab-label"><?php esc_html_e( 'Taxonomies', 'profile-builder' ) ?></span></a>
484 </li>
485
486 <li class="wppb-role-editor-tab-title">
487 <a href="javascript:void(0)" class="wppb-role-editor-tab wppb-role-editor-appearance" data-wppb-re-tab="appearance"><i class="dashicons dashicons-admin-appearance"></i> <span class="wppb-role-editor-tab-label"><?php esc_html_e( 'Appearance', 'profile-builder' ) ?></span></a>
488 </li>
489
490 <li class="wppb-role-editor-tab-title">
491 <a href="javascript:void(0)" class="wppb-role-editor-tab wppb-role-editor-plugins" data-wppb-re-tab="plugins"><i class="dashicons dashicons-admin-plugins"></i> <span class="wppb-role-editor-tab-label"><?php esc_html_e( 'Plugins', 'profile-builder' ) ?></span></a>
492 </li>
493
494 <li class="wppb-role-editor-tab-title">
495 <a href="javascript:void(0)" class="wppb-role-editor-tab wppb-role-editor-users" data-wppb-re-tab="users"><i class="dashicons dashicons-admin-users"></i> <span class="wppb-role-editor-tab-label"><?php esc_html_e( 'Users', 'profile-builder' ) ?></span></a>
496 </li>
497
498 <li class="wppb-role-editor-tab-title">
499 <a href="javascript:void(0)" class="wppb-role-editor-tab wppb-role-editor-custom" data-wppb-re-tab="custom"><i class="dashicons dashicons-admin-generic"></i> <span class="wppb-role-editor-tab-label"><?php esc_html_e( 'Custom', 'profile-builder' ) ?></span></a>
500 </li>
501 </ul>
502
503 <div id="wppb-role-edit-table">
504 <div class="wppb-re-spinner-container"><i class="icon-wppb-re-spinner wppb-re-spin"></i></div>
505 <div id="wppb-role-edit-caps-clear"></div>
506 </div>
507 <div id="wppb-role-edit-divs-clear"></div>
508 </div>
509
510 <input type="hidden" id="wppb-role-slug-hidden" name="wppb-role-slug-hidden" value="">
511 <input type="hidden" name="wppb-re-ajax-nonce" id="wppb-re-ajax-nonce" value="<?php echo esc_attr( wp_create_nonce( 'wppb-re-ajax-nonce' ) ) ?>" />
512 </div>
513 <?php
514
515 }
516
517 function edit_publish_meta_box( $post ) {
518
519 global $current_screen;
520
521 $post_type = 'wppb-roles-editor';
522
523 if( $post->post_type == $post_type ) {
524 $role_slug = $this->sanitize_role( get_post_meta( $post->ID, 'wppb_role_slug', true ) );
525
526 ?>
527 <style type="text/css">
528 .misc-pub-section.misc-pub-post-status,
529 .misc-pub-section.misc-pub-visibility,
530 .misc-pub-section.curtime.misc-pub-curtime,
531 #minor-publishing-actions,
532 #major-publishing-actions #delete-action {
533 display: none;
534 }
535 </style>
536
537 <div class="misc-pub-section misc-pub-section-users">
538 <i class="icon-wppb-re-users"></i>
539 <span><?php esc_html_e( 'Users', 'profile-builder' ) ?>: <strong>0</strong></span>
540 </div>
541
542 <div class="misc-pub-section misc-pub-section-capabilities">
543 <i class="icon-wppb-re-caps"></i>
544 <span><?php esc_html_e( 'Capabilities', 'profile-builder' ) ?>: <strong>0</strong></span>
545 </div>
546
547 <div class="misc-pub-section misc-pub-section-edit-slug">
548 <i class="icon-wppb-re-slug"></i>
549 <span>
550 <label for="wppb-role-slug"><?php esc_html_e( 'Role Slug', 'profile-builder' ) ?>: </label>
551 <input type="text" id="wppb-role-slug" value="<?php echo $current_screen->action == 'add' ? '' : esc_attr( $role_slug ) ?>" <?php echo $current_screen->action == 'add' ? '' : 'disabled'; ?>>
552 </span>
553 </div>
554 <?php
555 }
556
557 }
558
559 function remove_old_labels( $capabilities ) {
560
561 $old_levels = array( 'level_0', 'level_1', 'level_2', 'level_3', 'level_4', 'level_5', 'level_6', 'level_7', 'level_8', 'level_9', 'level_10' );
562
563 foreach( $old_levels as $key => $value ) {
564 unset( $capabilities[$value] );
565 }
566
567 return $capabilities;
568
569 }
570
571 function modify_post_title( $data ) {
572
573 if( 'wppb-roles-editor' != $data['post_type'] || $data['post_status'] == 'auto-draft' ) {
574 return $data;
575 }
576
577 if( ! current_user_can( 'delete_users' ) ) {
578 return $data;
579 }
580
581 if( isset( $data['post_title'] ) ) {
582 $data['post_title'] = wp_strip_all_tags( $data['post_title'] );
583 }
584
585 return $data;
586
587 }
588
589 function add_post_meta( $post_id, $post ) {
590
591 $post_type = get_post_type( $post_id );
592
593 if( 'wppb-roles-editor' != $post_type || $post->post_status == 'auto-draft' ) {
594 return;
595 }
596
597 if( ! current_user_can( 'delete_users' ) ) {
598 return;
599 }
600
601 if( !isset( $_POST['wppb-re-ajax-nonce'] ) || !wp_verify_nonce( sanitize_text_field( $_POST['wppb-re-ajax-nonce'] ), 'wppb-re-ajax-nonce' ) )
602 return;
603
604 if( isset( $_POST['wppb-role-slug-hidden'] ) ) {
605 $role_slug = $this->sanitize_role( trim( $_POST['wppb-role-slug-hidden'] ) ); /* phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized */
606
607 update_post_meta( $post_id, 'wppb_role_slug', $role_slug );
608 }
609
610 }
611
612 function update_role_capabilities() {
613
614 if( ! current_user_can( 'delete_users' ) ) {
615 die();
616 }
617
618 check_ajax_referer( 'wppb-re-ajax-nonce', 'security' );
619
620 $role_slug = isset( $_POST['role'] ) ? $this->sanitize_role( $_POST['role'] ) : ''; /* phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized */
621
622 $role = get_role( $role_slug );
623
624 if( $role ) {
625 if( isset( $_POST['new_capabilities'] ) ) {
626 foreach( $_POST['new_capabilities'] as $key => $value ) { /* phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized */
627 $role->add_cap( sanitize_text_field( $key ) );
628 }
629 }
630
631 if( isset( $_POST['capabilities_to_delete'] ) ) {
632 foreach( $_POST['capabilities_to_delete'] as $key => $value ) { /* phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized */
633 $role->remove_cap( sanitize_text_field( $key ) );
634 }
635 }
636 } else {
637 $capabilities = array();
638
639 if( isset( $_POST['all_capabilities'] ) ) {
640 foreach( $_POST['all_capabilities'] as $key => $value ) { /* phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized */
641 $capabilities[sanitize_text_field( $key )] = true;
642 };
643 }
644
645 $role_display_name = isset( $_POST['role_display_name'] ) ? sanitize_text_field( $_POST['role_display_name'] ) : '';
646
647 add_role( $role_slug, $role_display_name, $capabilities );
648
649 do_action( 'wppb_user_role_added', $role_slug, $role_display_name, $capabilities );
650
651 }
652
653 die( 'role_capabilities_updated' );
654
655 }
656
657 function group_capabilities() {
658
659 $capabilities = get_option( 'wppb_roles_editor_capabilities', 'not_set' );
660
661 if( $capabilities == 'not_set' ) {
662 // We remove non-custom capabilities from this array later on
663 $custom_capabilities = $this->get_all_capabilities();
664 $custom_capabilities = $this->remove_old_labels( $custom_capabilities );
665
666 // General capabilities
667 $general_capabilities = array(
668 'label' => 'General',
669 'icon' => 'dashicons-wordpress',
670 'capabilities' => array( 'edit_dashboard', 'edit_files', 'export', 'import', 'manage_links', 'manage_options', 'moderate_comments', 'read', 'unfiltered_html', 'update_core' )
671 );
672
673 // Themes management capabilities
674 $appearance_capabilities = array(
675 'label' => 'Appearance',
676 'icon' => 'dashicons-admin-appearance',
677 'capabilities' => array( 'delete_themes', 'edit_theme_options', 'edit_themes', 'install_themes', 'switch_themes', 'update_themes' )
678 );
679
680 // Plugins management capabilities
681 $plugins_capabilities = array(
682 'label' => 'Plugins',
683 'icon' => 'dashicons-admin-plugins',
684 'capabilities' => array( 'activate_plugins', 'delete_plugins', 'edit_plugins', 'install_plugins', 'update_plugins' )
685 );
686
687 // Users management capabilities
688 $users_capabilities = array(
689 'label' => 'Users',
690 'icon' => 'dashicons-admin-users',
691 'capabilities' => array( 'add_users', 'create_roles', 'create_users', 'delete_roles', 'delete_users', 'edit_roles', 'edit_users', 'list_roles', 'list_users', 'promote_users', 'remove_users' )
692 );
693
694 // Taxonomies capabilities - part 1
695 $taxonomies_capabilities = array();
696 $taxonomies = get_taxonomies( array(), 'objects' );
697 foreach( $taxonomies as $taxonomy ) {
698 $taxonomies_capabilities = array_merge( $taxonomies_capabilities, array_values( (array) $taxonomy->cap ) );
699 }
700
701 // Post types capabilities
702 $post_types_capabilities = array();
703 foreach( get_post_types( array(), 'objects' ) as $type ) {
704
705 if( in_array( $type->name, array( 'revision', 'nav_menu_item', 'custom_css', 'customize_changeset', 'wppb-rf-cpt', 'wppb-epf-cpt', 'wppb-roles-editor' ) ) ) {
706 continue;
707 }
708
709 $post_type_capabilities = $this->post_type_group_capabilities( $type->name );
710 if( empty( $post_type_capabilities ) ) {
711 continue;
712 }
713
714 $post_type_icon = $type->hierarchical ? 'dashicons-admin-page' : 'dashicons-admin-post';
715 if( is_string( $type->menu_icon ) && preg_match( '/dashicons-/i', $type->menu_icon ) ) {
716 $post_type_icon = $type->menu_icon;
717 } else if( 'attachment' === $type->name ) {
718 $post_type_icon = 'dashicons-admin-media';
719 } else if( 'download' === $type->name ) {
720 $post_type_icon = 'dashicons-download';
721 } else if( 'product' === $type->name ) {
722 $post_type_icon = 'dashicons-cart';
723 }
724
725 $post_types_capabilities[$type->name] = array(
726 'label' => $type->labels->name,
727 'icon' => $post_type_icon,
728 'capabilities' => $post_type_capabilities
729 );
730
731 $taxonomies_capabilities = array_diff( $taxonomies_capabilities, $post_type_capabilities );
732 $custom_capabilities = array_diff( $custom_capabilities, $post_type_capabilities );
733 }
734
735 // Taxonomies capabilities - part 2
736 $taxonomies_capabilities = array_diff( $taxonomies_capabilities, $general_capabilities['capabilities'], $appearance_capabilities['capabilities'], $plugins_capabilities['capabilities'], $users_capabilities['capabilities'] );
737 $taxonomies_capabilities = array(
738 'label' => 'Taxonomies',
739 'icon' => '',
740 'capabilities' => array_unique( $taxonomies_capabilities )
741 );
742
743 // Custom capabilities
744 $custom_capabilities = array_diff( $custom_capabilities, $general_capabilities['capabilities'], $appearance_capabilities['capabilities'], $appearance_capabilities['capabilities'], $plugins_capabilities['capabilities'], $users_capabilities['capabilities'], $taxonomies_capabilities['capabilities'] );
745 $custom_capabilities = array_values( $custom_capabilities );
746 $custom_capabilities = array(
747 'label' => 'Custom',
748 'icon' => '',
749 'capabilities' => array_unique( $custom_capabilities )
750 );
751
752 // Create capabilities array
753 $capabilities = array(
754 'general' => $general_capabilities,
755 'post_types' => $post_types_capabilities,
756 'taxonomies' => $taxonomies_capabilities,
757 'appearance' => $appearance_capabilities,
758 'plugins' => $plugins_capabilities,
759 'users' => $users_capabilities,
760 'custom' => $custom_capabilities
761 );
762
763 update_option( 'wppb_roles_editor_capabilities', $capabilities, false );
764 } else {
765 $custom_capabilities = $this->get_all_capabilities();
766 $custom_capabilities = $this->remove_old_labels( $custom_capabilities );
767
768 foreach( $capabilities['post_types'] as $key => $value ) {
769 $custom_capabilities = array_diff( $custom_capabilities, $value['capabilities'] );
770 }
771
772 foreach( $capabilities as $key => $value ) {
773 if( $key != 'post_types' && $key != 'custom' ) {
774 $custom_capabilities = array_diff( $custom_capabilities, $value['capabilities'] );
775 }
776 }
777
778 $custom_capabilities = array_values( $custom_capabilities );
779 $custom_capabilities = array_unique( $custom_capabilities );
780 $custom_capabilities = array_diff( $custom_capabilities, $capabilities['custom']['capabilities'] );
781
782 if( ! empty( $custom_capabilities ) ) {
783 $capabilities['custom']['capabilities'] = array_merge( $capabilities['custom']['capabilities'], $custom_capabilities );
784
785 update_option( 'wppb_roles_editor_capabilities', $capabilities, false );
786 }
787 }
788
789 return $capabilities;
790
791 }
792
793 function post_type_group_capabilities( $post_type = 'post' ) {
794
795 $capabilities = (array) get_post_type_object( $post_type )->cap;
796
797 unset( $capabilities['edit_post'] );
798 unset( $capabilities['read_post'] );
799 unset( $capabilities['delete_post'] );
800
801 $capabilities = array_values( $capabilities );
802
803 if( ! in_array( $post_type, array( 'post', 'page' ) ) ) {
804 // Get the post and page capabilities
805 $post_caps = array_values( (array) get_post_type_object( 'post' )->cap );
806 $page_caps = array_values( (array) get_post_type_object( 'page' )->cap );
807
808 // Remove post/page capabilities from the current post type capabilities
809 $capabilities = array_diff( $capabilities, $post_caps, $page_caps );
810 }
811
812 if( 'attachment' === $post_type ) {
813 $capabilities[] = 'unfiltered_upload';
814 }
815
816 return array_unique( $capabilities );
817
818 }
819
820 function get_all_capabilities() {
821
822 global $wp_roles;
823
824 $capabilities = array();
825
826 foreach( $wp_roles->role_objects as $key => $role ) {
827 if( is_array( $role->capabilities ) ) {
828 foreach( $role->capabilities as $capability => $granted ) {
829 $capabilities[$capability] = $capability;
830 }
831 }
832 }
833
834 return array_unique( $capabilities );
835
836 }
837
838 function delete_capability_permanently() {
839
840 if( ! current_user_can( 'delete_users' ) ) {
841 die();
842 }
843
844 check_ajax_referer( 'wppb-re-ajax-nonce', 'security' );
845
846 global $wp_roles;
847
848 if( !empty( $_POST['capability'] ) ) {
849
850 foreach ($wp_roles->role_names as $role_slug => $role_display_name) {
851 $role = get_role($role_slug);
852 $role->remove_cap(sanitize_text_field($_POST['capability']));
853 }
854
855 $capabilities = get_option( 'wppb_roles_editor_capabilities', 'not_set' );
856
857 if ($capabilities != 'not_set' && ($key = array_search(sanitize_text_field($_POST['capability']), $capabilities['custom']['capabilities'])) !== false) {
858 unset($capabilities['custom']['capabilities'][$key]);
859 $capabilities['custom']['capabilities'] = array_values($capabilities['custom']['capabilities']);
860
861 update_option( 'wppb_roles_editor_capabilities', $capabilities, false );
862 }
863 }
864
865 die();
866
867 }
868
869 function remove_filter_by_month_dropdown( $months, $post_type = NULL ) {
870
871 if( isset( $post_type ) && $post_type == 'wppb-roles-editor' ) {
872 return __return_empty_array();
873 } else {
874 return $months;
875 }
876
877 }
878
879 function modify_list_row_actions( $actions, $post ) {
880 global $wp_roles;
881
882 if( $post->post_type == 'wppb-roles-editor' ) {
883 $current_user = wp_get_current_user();
884 $default_role = get_option( 'default_role' );
885 $role_slug = get_post_meta( $post->ID, 'wppb_role_slug', true );
886
887 $url = admin_url( 'post.php?post=' . $post->ID );
888
889 $edit_link = add_query_arg( array( 'action' => 'edit' ), $url );
890
891 $actions = array(
892 'edit' => sprintf(
893 '<a href="%1$s">%2$s</a>',
894 esc_url( $edit_link ),
895 esc_html__( 'Edit', 'profile-builder' )
896 )
897 );
898
899 $clone_url = admin_url( 'post-new.php?post_type=wppb-roles-editor' );
900 $clone_link = add_query_arg( array( 'action' => 'wppb_re_clone', 'wppb_re_clone' => $this->sanitize_role( $role_slug ) ), $clone_url );
901
902 $actions = array_merge( $actions, array(
903 'clone' => sprintf(
904 '<a href="%1$s">%2$s</a>',
905 esc_url( $clone_link ),
906 esc_html__( 'Clone', 'profile-builder' )
907 )
908 )
909 );
910
911 if( in_array( $role_slug, $current_user->roles ) && ( ! is_multisite() || ( is_multisite() && ! is_super_admin() ) ) && ( !empty( $wp_roles->roles[$role_slug]['capabilities'] ) && array_key_exists( 'manage_options', $wp_roles->roles[$role_slug]['capabilities'] ) ) ) {
912 $actions = array_merge( $actions, array(
913 'delete_notify your_role' => '<span title="'. esc_html__( 'You can\'t delete your role.', 'profile-builder' ) .'">'. esc_html__( 'Delete', 'profile-builder' ) .'</span>'
914 )
915 );
916 } elseif( $role_slug == $default_role ) {
917 $actions = array_merge( $actions, array(
918 'default_role' => sprintf(
919 '<a href="%s">%s</a>',
920 esc_url( admin_url( 'options-general.php#default_role' ) ),
921 esc_html__( 'Change Default', 'profile-builder' ) ),
922 'delete_notify' => '<span title="'. esc_html__( 'You can\'t delete the default role. Change it first.', 'profile-builder' ) .'">'. esc_html__( 'Delete', 'profile-builder' ) .'</span>'
923 )
924 );
925 } else {
926 $delete_link = wp_nonce_url( add_query_arg( array( 'action' => 'delete' ), $url ), 'delete-post_'. $post->ID );
927
928 $actions = array_merge( $actions, array(
929 'delete' => sprintf(
930 '<a href="%1$s" onclick="return confirm( \'%2$s\' );">%3$s</a>',
931 esc_url( $delete_link ),
932 esc_html__( 'Are you sure?\nThis will permanently delete the role and cannot be undone!\nUsers assigned only on this role will be moved to the default role.', 'profile-builder' ),
933 esc_html__( 'Delete', 'profile-builder' )
934 )
935 )
936 );
937 }
938 }
939
940 return $actions;
941
942 }
943
944 function sanitize_role( $role ) {
945
946 $role = strtolower( $role );
947 $role = wp_strip_all_tags( $role );
948 $role = preg_replace( '/[^a-z0-9_\-\s]/', '', $role );
949 $role = str_replace( ' ', '_', $role );
950 $role = sanitize_text_field( $role );
951
952 return $role;
953
954 }
955
956 function delete_role_permanently( $post_id ) {
957
958 global $post_type;
959 if( $post_type != 'wppb-roles-editor' ) {
960 return;
961 }
962
963 check_admin_referer( 'delete-post_'. $post_id );
964
965 $role_slug = get_post_meta( $post_id, 'wppb_role_slug', true );
966 $role_slug = $this->sanitize_role( $role_slug );
967
968 $default_role = get_option( 'default_role' );
969
970 if( $role_slug == $default_role ) {
971 return;
972 }
973
974 $users = get_users( array( 'role' => $role_slug ) );
975
976 if( is_array( $users ) ) {
977 foreach( $users as $user ) {
978 if( $user->has_cap( $role_slug ) && count( $user->roles ) <= 1 ) {
979 $user->set_role( $default_role );
980 } elseif( $user->has_cap( $role_slug ) ) {
981 $user->remove_role( $role_slug );
982 }
983 }
984 }
985
986 remove_role( $role_slug );
987
988 }
989
990 function get_hidden_capabilities() {
991
992 $capabilities = array();
993
994 if( is_multisite() || ! defined( 'ALLOW_UNFILTERED_UPLOADS' ) || ! ALLOW_UNFILTERED_UPLOADS ) {
995 $capabilities['unfiltered_upload'] = 'unfiltered_upload';
996 }
997
998 if( is_multisite() || ( defined( 'DISALLOW_UNFILTERED_HTML' ) && DISALLOW_UNFILTERED_HTML ) ) {
999 $capabilities['unfiltered_html'] = 'unfiltered_html';
1000 }
1001
1002 if( is_multisite() || ( defined( 'DISALLOW_FILE_EDIT' ) && DISALLOW_FILE_EDIT ) ) {
1003 $capabilities['edit_files'] = 'edit_files';
1004 $capabilities['edit_plugins'] = 'edit_plugins';
1005 $capabilities['edit_themes'] = 'edit_themes';
1006 }
1007
1008 if( is_multisite() || ( defined( 'DISALLOW_FILE_MODS' ) && DISALLOW_FILE_MODS ) ) {
1009 $capabilities['edit_files'] = 'edit_files';
1010 $capabilities['edit_plugins'] = 'edit_plugins';
1011 $capabilities['edit_themes'] = 'edit_themes';
1012 $capabilities['update_plugins'] = 'update_plugins';
1013 $capabilities['delete_plugins'] = 'delete_plugins';
1014 $capabilities['install_plugins'] = 'install_plugins';
1015 $capabilities['upload_plugins'] = 'upload_plugins';
1016 $capabilities['update_themes'] = 'update_themes';
1017 $capabilities['delete_themes'] = 'delete_themes';
1018 $capabilities['install_themes'] = 'install_themes';
1019 $capabilities['upload_themes'] = 'upload_themes';
1020 $capabilities['update_core'] = 'update_core';
1021 }
1022
1023 return array_unique( $capabilities );
1024
1025 }
1026
1027 // Add actions on Add User back-end page
1028 function actions_on_user_new() {
1029
1030 $this->scripts_and_styles_actions( 'user_new' );
1031
1032 add_action( 'user_new_form', array( $this, 'roles_field_user_new' ) );
1033
1034 if ( is_multisite() ) {
1035 // Set up the wp-signups for new users that will receive confirmation emails
1036 add_filter( 'signup_user_meta', array( $this, 'ms_signup_user_meta' ) );
1037
1038 // Intercept the invite for existing users that will receive confirmation emails
1039 add_action( 'invite_user', array( $this, 'ms_invite_user' ), 10, 3 );
1040 } else {
1041 add_action( 'user_register', array( $this, 'roles_update_user_new' ) );
1042 }
1043 }
1044
1045 // Add actions on Edit User back-end page
1046 function actions_on_user_edit() {
1047
1048 $this->scripts_and_styles_actions( 'user_edit' );
1049
1050 add_action( 'personal_options', array( $this, 'roles_field_user_edit' ) );
1051
1052 add_action( 'profile_update', array( $this, 'roles_update_user_edit' ), 10, 2 );
1053
1054 }
1055
1056 // Roles Edit checkboxes for Add User back-end page
1057 function roles_field_user_new() {
1058
1059 if( ! current_user_can( 'promote_users' ) ) {
1060 return;
1061 }
1062
1063 $user_roles = apply_filters( 'wppb_default_user_roles', array( get_option( 'default_role' ) ) );
1064
1065 if( isset( $_POST['createuser'] ) && ! empty( $_POST['wppb_re_user_roles'] ) ) {
1066 $user_roles = array_map( array( $this, 'sanitize_role' ), $_POST['wppb_re_user_roles'] );// phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
1067 }
1068
1069 wp_nonce_field( 'new_user_roles', 'wppb_re_new_user_roles_nonce' );
1070
1071 if( apply_filters( 'wppb_backend_allow_multiple_user_roles_selection', true ) ) {
1072 $this->roles_field_display( $user_roles );
1073 }
1074
1075 }
1076
1077 // Roles Edit checkboxes for Edit User back-end page
1078 function roles_field_user_edit( $user ) {
1079
1080 if( ! current_user_can( 'promote_users' ) || ! current_user_can( 'edit_user', $user->ID ) ) {
1081 return;
1082 }
1083
1084 $user_roles = (array) $user->roles;
1085
1086 wp_nonce_field( 'new_user_roles', 'wppb_re_new_user_roles_nonce' );
1087
1088 if( apply_filters( 'wppb_backend_allow_multiple_user_roles_selection', true ) ) {
1089 $this->roles_field_display( $user_roles );
1090 }
1091 }
1092
1093 // Output roles edit checkboxes
1094 function roles_field_display( $user_roles ) {
1095
1096 $wppb_roles = get_editable_roles();
1097
1098 ?>
1099 <table class="form-table">
1100 <tr class="wppb-re-edit-user">
1101 <th><?php esc_html_e( 'Edit User Roles', 'profile-builder' ); ?></th>
1102
1103 <td>
1104 <div>
1105 <ul style="margin: 5px 0;">
1106 <?php foreach( $wppb_roles as $role_slug => $role_details ) { ?>
1107 <li>
1108 <label>
1109 <input type="checkbox" name="wppb_re_user_roles[]" value="<?php echo esc_attr( $role_slug ); ?>" <?php checked( in_array( $role_slug, $user_roles ) ); ?> />
1110 <?php echo esc_html( translate_user_role( $role_details['name'] ) ); ?>
1111 </label>
1112 </li>
1113 <?php } ?>
1114 </ul>
1115 </div>
1116 </td>
1117 </tr>
1118 </table>
1119 <?php
1120 }
1121
1122 function roles_update_user_edit( $user_id, $old_user_data ) {
1123
1124 if( ! current_user_can( 'promote_users' ) || ! current_user_can( 'edit_user', $user_id ) ) {
1125 return;
1126 }
1127
1128 if( ! isset( $_POST['wppb_re_new_user_roles_nonce'] ) || ! wp_verify_nonce( sanitize_text_field( $_POST['wppb_re_new_user_roles_nonce'] ), 'new_user_roles' ) ) {
1129 return;
1130 }
1131
1132 $this->roles_update_user_new_and_edit( $old_user_data );
1133
1134 }
1135
1136 function roles_update_user_new( $user_id ) {
1137
1138 if( ! current_user_can( 'promote_users' ) ) {
1139 return;
1140 }
1141
1142 if( ! isset( $_POST['wppb_re_new_user_roles_nonce'] ) || ! wp_verify_nonce( sanitize_text_field( $_POST['wppb_re_new_user_roles_nonce'] ), 'new_user_roles' ) ) {
1143 return;
1144 }
1145
1146 $user = new \WP_User( $user_id );
1147
1148 $this->roles_update_user_new_and_edit( $user );
1149
1150 }
1151
1152 function roles_update_user_new_and_edit( $user ) {
1153
1154 if( ! empty( $_POST['wppb_re_user_roles'] ) || !empty( $_POST['role'] ) ) {
1155
1156 $old_roles = (array) $user->roles;
1157
1158 if( isset( $_POST['wppb_re_user_roles'] ) )
1159 $new_roles = array_map( array( $this, 'sanitize_role' ), $_POST['wppb_re_user_roles'] );//phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
1160 else
1161 $new_roles = array( $this->sanitize_role( $_POST['role'] ) ); //phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
1162
1163 foreach( $new_roles as $new_role ) {
1164 if( ! in_array( $new_role, (array) $user->roles ) ) {
1165 $user->add_role( $new_role );
1166 }
1167 }
1168
1169 foreach( $old_roles as $old_role ) {
1170 if( ! in_array( $old_role, $new_roles ) ) {
1171 $user->remove_role( $old_role );
1172 }
1173 }
1174 } else {
1175 foreach( (array) $user->roles as $old_role ) {
1176 $user->remove_role( $old_role );
1177 }
1178 }
1179
1180 }
1181
1182 // Add the user roles to the signup meta for new users that will receive confirmation emails
1183 public function ms_signup_user_meta( $meta ) {
1184 if ( ! current_user_can( 'promote_users' ) ) {
1185 return;
1186 }
1187
1188 if ( ! isset( $_POST['wppb_re_new_user_roles_nonce'] ) || ! wp_verify_nonce( sanitize_text_field( $_POST['wppb_re_new_user_roles_nonce'] ), 'new_user_roles' ) ) {
1189 return;
1190 }
1191
1192 if ( ! empty( $_POST['wppb_re_user_roles'] ) ) {
1193 $new_roles = array_map( array( $this, 'sanitize_role' ), $_POST['wppb_re_user_roles'] );// phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
1194
1195 $meta['wppb_re_user_roles'] = array();
1196
1197 foreach ( $new_roles as $new_role ) {
1198 $meta['wppb_re_user_roles'][] = $new_role;
1199 }
1200
1201 // Having a single role is required to set up the user signup.
1202 // Any extra roles will be added when the account is activated.
1203 if ( empty( $_REQUEST['role'] ) || ! $meta['new_role'] && isset( $meta['wppb_re_user_roles'][0] ) ) {
1204 $meta['new_role'] = $meta['wppb_re_user_roles'][0];
1205 }
1206 }
1207
1208 return $meta;
1209 }
1210
1211 // Add the user roles to the usermeta for existing users that will receive confirmation emails
1212 public function ms_invite_user( $user_id, $role, $newuser_key ) {
1213 if ( ! current_user_can( 'promote_users' ) || ! current_user_can( 'edit_user', $user_id ) ) {
1214 return;
1215 }
1216
1217 if ( ! isset( $_POST['wppb_re_new_user_roles_nonce'] ) || ! wp_verify_nonce( sanitize_text_field( $_POST['wppb_re_new_user_roles_nonce'] ), 'new_user_roles' ) ) {
1218 return;
1219 }
1220
1221 if ( ! empty( $_POST['wppb_re_user_roles'] ) ) {
1222 update_user_meta($user_id, 'wppb_subsite_' . get_current_blog_id() . '_user_roles', array_map( array( $this, 'sanitize_role' ), $_POST['wppb_re_user_roles'] ));// phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
1223 }
1224 }
1225
1226 // Add the extra roles to the user when they are added to the blog
1227 public function ms_add_user_to_blog( $user_id, $role, $blog_id ) {
1228
1229 $user = new \WP_User( $user_id );
1230
1231 if ( empty( $_POST['wppb_re_user_roles'] ) ) {
1232
1233 // Add New User - Confirmation Email
1234
1235 global $wpdb;
1236 $key = $wpdb->get_var( $wpdb->prepare( "SELECT activation_key FROM {$wpdb->signups} WHERE user_login = %s", $user->data->user_login ) );
1237 if ( $key ) {
1238 $signup = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->signups WHERE activation_key = %s", $key ) );
1239 if ( $signup ) {
1240 $meta = maybe_unserialize( $signup->meta );
1241 $this->update_roles( $user, $meta['wppb_re_user_roles'] );
1242 return;
1243 }
1244 }
1245
1246 // Add Existing User - Confirmation Email
1247
1248 $meta_key = 'wppb_subsite_' . get_current_blog_id() . '_user_roles';
1249 $roles = get_user_meta( $user_id, $meta_key, true );
1250 delete_user_meta( $user_id, $meta_key );
1251 $this->update_roles( $user, $roles );
1252 return;
1253 }
1254
1255 // Add New User & Add Existing User - Skip Confirmation Email
1256 // In these cases the user role data can be found in $_POST['wppb_re_user_roles']
1257
1258 if ( ! current_user_can( 'promote_users' ) || ! current_user_can( 'edit_user', $user_id ) ) {
1259 return;
1260 }
1261
1262 if ( ! isset( $_POST['wppb_re_new_user_roles_nonce'] ) || ! wp_verify_nonce( sanitize_text_field( $_POST['wppb_re_new_user_roles_nonce'] ), 'new_user_roles' ) ) {
1263 return;
1264 }
1265
1266 $this->roles_update_user_new_and_edit( $user );
1267 }
1268
1269 function update_roles( $user, $roles ) {
1270 if ( !empty( $roles ) ) {
1271 $old_roles = (array) $user->roles;
1272
1273 $new_roles = array_map( array( $this, 'sanitize_role' ), $roles );
1274
1275 foreach ( $new_roles as $new_role ) {
1276 // Add the missing roles.
1277 if ( ! in_array( $new_role, (array) $user->roles ) ) {
1278 $user->add_role( $new_role );
1279 }
1280 }
1281
1282 foreach ( $old_roles as $old_role ) {
1283 // Remove the extra roles.
1284 if ( ! in_array( $old_role, $new_roles ) ) {
1285 $user->remove_role( $old_role );
1286 }
1287 }
1288 }
1289 }
1290
1291 function scripts_and_styles_actions( $location ) {
1292
1293 // Enqueue jQuery on both Add User and Edit User back-end pages
1294 add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_jquery' ) );
1295
1296 // Actions for Add User back-end page
1297 if( $location == 'user_new' && apply_filters( 'wppb_backend_allow_multiple_user_roles_selection', true ) ) {
1298 add_action( 'admin_footer', array( $this, 'print_scripts_user_new' ), 25 );
1299 }
1300
1301 // Actions for Edit User back-end page
1302 if( $location == 'user_edit' && apply_filters( 'wppb_backend_allow_multiple_user_roles_selection', true ) ) {
1303 add_action( 'admin_head', array( $this, 'print_styles_user_edit' ) );
1304 add_action( 'admin_footer', array( $this, 'print_scripts_user_edit' ), 25 );
1305 }
1306
1307 }
1308
1309 // Enqueue jQuery where needed (use action)
1310 function enqueue_jquery() {
1311
1312 wp_enqueue_script( 'jquery' );
1313
1314 }
1315
1316 // Print scripts on Add User back-end page
1317 function print_scripts_user_new() {
1318
1319 if ( ! is_multisite() ) {
1320 ?>
1321 <script>
1322 jQuery( document ).ready( function() {
1323 // Remove WordPress default Role Select
1324 var roles_dropdown = jQuery( 'select#role' );
1325 roles_dropdown.closest( 'tr' ).remove();
1326 } );
1327 </script>
1328 <?php
1329 } else {
1330 ?>
1331 <script>
1332 jQuery( document ).ready( function() {
1333 wppb_multisite_re_checkboxes( 'select#adduser-role' );
1334 wppb_multisite_re_checkboxes( 'select#role' );
1335
1336 function wppb_multisite_re_checkboxes( selector ) {
1337 var roles_dropdown_new = jQuery( selector );
1338
1339 if ( roles_dropdown_new.length ) {
1340 // Hide WordPress default Role Select
1341 roles_dropdown_new.closest('tr').hide();
1342
1343 var checkboxes_new = roles_dropdown_new.closest('form').find('input[name="wppb_re_user_roles[]"]');
1344 var selectedCheckboxes_new = checkboxes_new.filter(':checked');
1345
1346 // At least one role must be selected
1347 if (selectedCheckboxes_new.length === 1) {
1348 selectedCheckboxes_new.prop('disabled', true);
1349 }
1350
1351 // Detect selected checkboxes on change
1352 checkboxes_new.on('change', function() {
1353 selectedCheckboxes_new = checkboxes_new.filter(':checked');
1354
1355 if (selectedCheckboxes_new.length === 1) {
1356 selectedCheckboxes_new.prop('disabled', true);
1357 } else {
1358 checkboxes_new.prop('disabled', false);
1359 }
1360
1361 // Select a role in the hidden default dropdown
1362 selectedCheckboxes_new.each(function() {
1363 var selectedRoleNew = jQuery(this).val();
1364 roles_dropdown_new.find('option').each(function() {
1365 if (jQuery(this).val() === selectedRoleNew) {
1366 jQuery(this).prop('selected', true);
1367 //return false; // Break the loop after selecting the first match
1368 }
1369 });
1370 });
1371 });
1372 }
1373 }
1374 } );
1375 </script>
1376 <?php
1377 }
1378 }
1379
1380 // Print scripts on Add Existing User back-end page
1381 function print_scripts_user_existing() {
1382
1383 ?>
1384 <script>
1385 jQuery( document ).ready( function() {
1386 // Remove WordPress default Role Select
1387 var roles_dropdown = jQuery( 'select#role' );
1388 roles_dropdown.closest( 'tr' ).remove();
1389 } );
1390 </script>
1391
1392 <?php
1393 }
1394
1395 // Print scripts on Edit User back-end page
1396 function print_scripts_user_edit() {
1397
1398 ?>
1399 <script>
1400 jQuery( document ).ready(
1401 // Remove WordPress default Role Select
1402 function() {
1403 jQuery( '.user-role-wrap' ).remove();
1404 }
1405 );
1406 </script>
1407
1408 <?php
1409 }
1410
1411 // Print scripts on Edit User back-end page
1412 function print_styles_user_edit() {
1413
1414 ?>
1415 <style type="text/css">
1416 /* Hide WordPress default Role Select */
1417 .user-role-wrap {
1418 display: none !important;
1419 }
1420 </style>
1421
1422 <?php
1423 }
1424
1425 }
1426
1427 $wppb_generalSettings = get_option( 'wppb_general_settings', 'not_found' );
1428 if( $wppb_generalSettings != 'not_found' ) {
1429 if( ! empty( $wppb_generalSettings['rolesEditor'] ) && ( $wppb_generalSettings['rolesEditor'] == 'yes' ) ) {
1430 $wppb_role_editor_instance = new WPPB_Roles_Editor();
1431 }
1432 }
1433