AuditCheckTrait.php
1 year ago
CoreUserRoleOptionIntegrityCheck.php
1 year ago
EditableFileSystemCheck.php
7 months ago
ElevatedCoreRoleCheck.php
7 months ago
EmptyUnusedRoleCheck.php
7 months ago
HighPrivilegeContentModeratorCheck.php
7 months ago
HighPrivilegeOrElevatedUserCheck.php
7 months ago
HighPrivilegeRoleCheck.php
7 months ago
HighPrivilegeUserCountCheck.php
7 months ago
RestfulAutoDiscoverEndpointCheck.php
7 months ago
RoleCapabilityNamingConventionCheck.php
7 months ago
RoleIntegrityCheck.php
7 months ago
RoleTransparencyCheck.php
7 months ago
XmlRpcEndpointCheck.php
7 months ago
EmptyUnusedRoleCheck.php
180 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * ====================================================================== |
| 5 | * LICENSE: This file is subject to the terms and conditions defined in * |
| 6 | * file 'license.txt', which is part of this source code package. * |
| 7 | * ====================================================================== |
| 8 | */ |
| 9 | |
| 10 | /** |
| 11 | * Detect empty roles |
| 12 | * |
| 13 | * @package AAM |
| 14 | * @version 7.0.0 |
| 15 | */ |
| 16 | class AAM_Audit_EmptyUnusedRoleCheck |
| 17 | { |
| 18 | |
| 19 | use AAM_Audit_AuditCheckTrait; |
| 20 | |
| 21 | /** |
| 22 | * Step ID |
| 23 | * |
| 24 | * @version 7.0.0 |
| 25 | */ |
| 26 | const ID = 'empty_unused_roles_detection'; |
| 27 | |
| 28 | /** |
| 29 | * List of WordPress core roles |
| 30 | * |
| 31 | * @return array |
| 32 | * |
| 33 | * @version 7.0.0 |
| 34 | */ |
| 35 | const CORE_ROLES = [ |
| 36 | 'administrator', |
| 37 | 'editor', |
| 38 | 'author', |
| 39 | 'contributor', |
| 40 | 'subscriber' |
| 41 | ]; |
| 42 | |
| 43 | /** |
| 44 | * Run the check |
| 45 | * |
| 46 | * @return array |
| 47 | * |
| 48 | * @access public |
| 49 | * @static |
| 50 | * |
| 51 | * @version 7.0.0 |
| 52 | */ |
| 53 | public static function run() |
| 54 | { |
| 55 | $issues = []; |
| 56 | $response = [ 'is_completed' => true ]; |
| 57 | |
| 58 | try { |
| 59 | array_push( |
| 60 | $issues, |
| 61 | ...self::_detect_empty_roles(self::_read_role_key_option()) |
| 62 | ); |
| 63 | |
| 64 | array_push( |
| 65 | $issues, |
| 66 | ...self::_detect_unused_roles(self::_read_role_key_option()) |
| 67 | ); |
| 68 | } catch (Exception $e) { |
| 69 | array_push($issues, self::_format_issue( |
| 70 | 'APPLICATION_ERROR', |
| 71 | [ |
| 72 | 'message' => $e->getMessage() |
| 73 | ], |
| 74 | 'error' |
| 75 | )); |
| 76 | } |
| 77 | |
| 78 | if (count($issues) > 0) { |
| 79 | $response['issues'] = $issues; |
| 80 | } |
| 81 | |
| 82 | // Determine final status for the check |
| 83 | self::_determine_check_status($response); |
| 84 | |
| 85 | return $response; |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Get a collection of error messages for current step |
| 90 | * |
| 91 | * @return array |
| 92 | * @access private |
| 93 | * @static |
| 94 | * |
| 95 | * @version 7.0.0 |
| 96 | */ |
| 97 | private static function _get_message_templates() |
| 98 | { |
| 99 | return [ |
| 100 | 'EMPTY_ROLE' => __( |
| 101 | 'Detected role %s (%s) with no capabilities', |
| 102 | 'advanced-access-manager' |
| 103 | ), |
| 104 | 'UNUSED_CUSTOM_ROLE' => __( |
| 105 | 'Detected unused custom role %s (%s)', |
| 106 | 'advanced-access-manager' |
| 107 | ) |
| 108 | ]; |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Detect empty roles |
| 113 | * |
| 114 | * @param array $db_roles |
| 115 | * |
| 116 | * @return array |
| 117 | * |
| 118 | * @access private |
| 119 | * @static |
| 120 | * |
| 121 | * @version 7.0.0 |
| 122 | */ |
| 123 | private static function _detect_empty_roles($db_roles) |
| 124 | { |
| 125 | $response = []; |
| 126 | |
| 127 | foreach($db_roles as $role_id => $role) { |
| 128 | if (empty($role['capabilities'])) { |
| 129 | array_push($response, self::_format_issue( |
| 130 | 'EMPTY_ROLE', |
| 131 | [ |
| 132 | 'name' => translate_user_role( |
| 133 | !empty($role['name']) ? $role['name'] : $role_id |
| 134 | ), |
| 135 | 'slug' => $role_id |
| 136 | ] |
| 137 | )); |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | return $response; |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * Detect unused roles |
| 146 | * |
| 147 | * @param array $db_roles |
| 148 | * |
| 149 | * @return array |
| 150 | * |
| 151 | * @access private |
| 152 | * @static |
| 153 | * |
| 154 | * @version 7.0.0 |
| 155 | */ |
| 156 | private static function _detect_unused_roles($db_roles) |
| 157 | { |
| 158 | $response = []; |
| 159 | $stats = count_users(); |
| 160 | |
| 161 | foreach($db_roles as $role_id => $role) { |
| 162 | if (empty($stats['avail_roles'][$role_id]) |
| 163 | && !in_array($role_id, self::CORE_ROLES, true) |
| 164 | ) { |
| 165 | array_push($response, self::_format_issue( |
| 166 | 'UNUSED_CUSTOM_ROLE', |
| 167 | [ |
| 168 | 'name' => translate_user_role( |
| 169 | !empty($role['name']) ? $role['name'] : $role_id |
| 170 | ), |
| 171 | 'slug' => $role_id |
| 172 | ] |
| 173 | )); |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | return $response; |
| 178 | } |
| 179 | |
| 180 | } |