class-activation.php
7 months ago
class-admin-menu-organizer.php
7 months ago
class-admin-menu-svg-icon-mask.php
7 months ago
class-auto-publish-posts-with-missed-schedule.php
7 months ago
class-avif-upload.php
7 months ago
class-captcha-protection.php
7 months ago
class-change-login-url.php
7 months ago
class-cleanup-admin-bar.php
7 months ago
class-common-methods.php
7 months ago
class-content-duplication.php
7 months ago
class-content-order.php
7 months ago
class-custom-admin-footer-text.php
7 months ago
class-custom-body-class.php
7 months ago
class-custom-css.php
7 months ago
class-custom-nav-menu-items-in-new-tab.php
7 months ago
class-deactivation.php
7 months ago
class-disable-author-archives.php
7 months ago
class-disable-comments.php
7 months ago
class-disable-dashboard-widgets.php
7 months ago
class-disable-embeds.php
7 months ago
class-disable-feeds.php
7 months ago
class-disable-gutenberg.php
7 months ago
class-disable-rest-api.php
7 months ago
class-disable-smaller-components.php
7 months ago
class-disable-updates.php
7 months ago
class-disable-xml-rpc.php
7 months ago
class-display-system-summary.php
7 months ago
class-email-address-obfuscator.php
7 months ago
class-email-delivery.php
7 months ago
class-enhance-list-tables.php
7 months ago
class-external-permalinks.php
7 months ago
class-heartbeat-control.php
7 months ago
class-hide-admin-bar.php
7 months ago
class-hide-admin-notices.php
7 months ago
class-image-sizes-panel.php
7 months ago
class-image-upload-control.php
7 months ago
class-insert-head-body-footer-code.php
7 months ago
class-last-login-column.php
7 months ago
class-limit-login-attempts.php
7 months ago
class-login-id-type.php
7 months ago
class-login-logout-menu.php
7 months ago
class-maintenance-mode.php
7 months ago
class-manage-ads-appads-txt.php
7 months ago
class-manage-robots-txt.php
7 months ago
class-media-replacement.php
7 months ago
class-multiple-user-roles.php
7 months ago
class-obfuscate-author-slugs.php
7 months ago
class-open-external-links-in-new-tab.php
7 months ago
class-password-protection.php
7 months ago
class-redirect-after-login.php
7 months ago
class-redirect-after-logout.php
7 months ago
class-redirect-fourofour.php
7 months ago
class-registration-date-column.php
7 months ago
class-revisions-control.php
7 months ago
class-search-engines-visibility.php
7 months ago
class-settings-fields-render.php
7 months ago
class-settings-sanitization.php
7 months ago
class-settings-sections-fields.php
7 months ago
class-show-custom-taxonomy-filters.php
7 months ago
class-site-identity-on-login-page.php
7 months ago
class-svg-upload.php
7 months ago
class-various-admin-ui-enhancements.php
7 months ago
class-view-admin-as-role.php
7 months ago
class-wider-admin-menu.php
7 months ago
class-wp-config-transformer.php
7 months ago
class-multiple-user-roles.php
158 lines
| 1 | <?php |
| 2 | |
| 3 | namespace ASENHA\Classes; |
| 4 | |
| 5 | /** |
| 6 | * Class for Multiple User Roles module |
| 7 | * |
| 8 | * @since 6.9.5 |
| 9 | */ |
| 10 | class Multiple_User_Roles { |
| 11 | |
| 12 | /** |
| 13 | * Add UI to enable multiple user roles selection |
| 14 | * |
| 15 | * @since 4.8.0 |
| 16 | */ |
| 17 | public function add_multiple_roles_ui( $user ) { |
| 18 | // Get user roles that the current user is allowed to edit |
| 19 | $roles = get_editable_roles(); |
| 20 | |
| 21 | $current_user = wp_get_current_user(); |
| 22 | $current_user_roles = array_intersect( array_values( $current_user->roles ), array_keys( $roles ) ); // indexed array of role slugs |
| 23 | |
| 24 | // Get the roles of the user being shown / edited / created |
| 25 | if ( ! empty( $user->roles ) ) { |
| 26 | $user_roles = array_intersect( array_values( $user->roles ), array_keys( $roles ) ); // indexed array of role slugs |
| 27 | } else { |
| 28 | $user_roles = array(); |
| 29 | } |
| 30 | |
| 31 | // Only show roles checkboxes for users that can assign roles to other users |
| 32 | if ( current_user_can( 'promote_users', get_current_user_id() ) ) { |
| 33 | |
| 34 | ?> |
| 35 | <div class="asenha-roles-temporary-container"> |
| 36 | <table class="form-table"> |
| 37 | <tr> |
| 38 | <th> |
| 39 | <label>Roles</label> |
| 40 | </th> |
| 41 | <td> |
| 42 | <?php |
| 43 | foreach ( $roles as $role_slug => $role_info ) { |
| 44 | |
| 45 | $checkbox_id = $role_slug . '_role'; |
| 46 | $role_name = translate_user_role( $role_info['name'] ); |
| 47 | if ( ! empty( $user_roles ) && in_array( $role_slug, $user_roles ) ) { |
| 48 | $checked = 'checked="checked"'; |
| 49 | } else { |
| 50 | $checked = ''; |
| 51 | } |
| 52 | |
| 53 | // We disable the 'Administrator' checkbox so it could not be unchecked and cause user to lose administrator priviledges without a way to restore it |
| 54 | $disabled = ''; |
| 55 | |
| 56 | if ( 'administrator' == $role_slug |
| 57 | && is_object( $user ) |
| 58 | && is_object( $current_user ) |
| 59 | ) { |
| 60 | if ( property_exists( $user, 'ID' ) |
| 61 | && property_exists( $current_user, 'ID' ) |
| 62 | && $user->ID == $current_user->ID |
| 63 | ) { |
| 64 | $disabled = 'disabled '; |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | // Output roles checkboxes |
| 69 | ?> |
| 70 | <label for="<?php esc_attr_e( $checkbox_id ); ?>"><input type="checkbox" id="<?php esc_attr_e( $checkbox_id ); ?>" value="<?php esc_attr_e( $role_slug ); ?>" name="asenha_assigned_roles[]" <?php esc_attr_e( $checked ); ?> <?php esc_attr_e( $disabled ); ?>/> <?php esc_html_e( $role_name ); ?></label><br /> |
| 71 | <?php |
| 72 | |
| 73 | } |
| 74 | |
| 75 | wp_nonce_field( 'asenha_set_multiple_roles', 'asenha_multiple_roles_nonce' ); |
| 76 | |
| 77 | ?> |
| 78 | </td> |
| 79 | </tr> |
| 80 | </table> |
| 81 | </div> |
| 82 | <?php |
| 83 | |
| 84 | } |
| 85 | |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Save changes in roles assignment |
| 90 | * |
| 91 | * @since 4.8.0 |
| 92 | */ |
| 93 | public function save_roles_assignment( $user_id ) { |
| 94 | |
| 95 | if ( ! isset( $_POST['asenha_multiple_roles_nonce'] ) ) { |
| 96 | return; |
| 97 | } |
| 98 | |
| 99 | if ( ! current_user_can( 'promote_users', get_current_user_id() ) || ! wp_verify_nonce( $_POST['asenha_multiple_roles_nonce'], 'asenha_set_multiple_roles' ) ) { |
| 100 | return; |
| 101 | } |
| 102 | |
| 103 | // Get user roles that the current user is allowed to edit |
| 104 | $roles = get_editable_roles(); |
| 105 | |
| 106 | $current_user = wp_get_current_user(); |
| 107 | $current_user_roles = array_intersect( array_values( $current_user->roles ), array_keys( $roles ) ); // indexed array of role slugs |
| 108 | |
| 109 | // Get the roles of the user being shown / edited / created |
| 110 | $user = get_user_by( 'id', (int) $user_id ); // WP_User object |
| 111 | $user_roles = array_intersect( array_values( $user->roles ), array_keys( $roles ) ); // Current/existing roles |
| 112 | |
| 113 | if ( ! empty( $_POST['asenha_assigned_roles'] ) ) { |
| 114 | |
| 115 | $assigned_roles = array_map( 'sanitize_text_field', $_POST['asenha_assigned_roles'] ); |
| 116 | |
| 117 | // Make sure only valid roles are processed |
| 118 | $assigned_roles = array_intersect( $assigned_roles, array_keys( $roles ) ); |
| 119 | |
| 120 | $roles_to_remove = array(); |
| 121 | $roles_to_add = array(); |
| 122 | |
| 123 | if ( empty( $assigned_roles ) ) { |
| 124 | |
| 125 | // Remove all current/existing roles |
| 126 | $roles_to_remove = $user_roles; |
| 127 | |
| 128 | } else { |
| 129 | |
| 130 | // Identify and remove roles not present in the newly assigned roles |
| 131 | $roles_to_remove = array_diff( $user_roles, $assigned_roles ); |
| 132 | |
| 133 | if ( ! empty( $roles_to_remove ) ) { |
| 134 | foreach ( $roles_to_remove as $role_to_remove ) { |
| 135 | $user->remove_role( $role_to_remove ); |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | // Identify and add roles not present in the existing roles |
| 140 | $roles_to_add = array_diff( $assigned_roles, $user_roles ); |
| 141 | |
| 142 | if ( $user->ID == $current_user->ID ) { |
| 143 | $roles_to_add[] = 'administrator'; |
| 144 | } |
| 145 | |
| 146 | if ( ! empty( $roles_to_add ) ) { |
| 147 | foreach ( $roles_to_add as $role_to_add ) { |
| 148 | $user->add_role( $role_to_add ); |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | } |
| 153 | |
| 154 | } |
| 155 | |
| 156 | } |
| 157 | |
| 158 | } |