Collaboration
2 weeks ago
Apps.php
2 weeks ago
Collection.php
1 month ago
Comments.php
2 months ago
DynamicContent.php
1 month ago
ExportImport.php
2 weeks ago
Form.php
2 weeks ago
Media.php
2 weeks ago
Page.php
2 weeks ago
PageSettings.php
2 weeks ago
RBAC.php
2 weeks ago
Symbol.php
2 weeks ago
Taxonomy.php
2 months ago
TemplateExportImport.php
2 months ago
UserData.php
2 weeks ago
Users.php
2 months ago
Walkthrough.php
2 months ago
WordpressData.php
2 months ago
WpAdmin.php
2 months ago
RBAC.php
217 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Role manager api |
| 4 | * |
| 5 | * @package kirki |
| 6 | */ |
| 7 | |
| 8 | namespace Kirki\Ajax; |
| 9 | |
| 10 | if (!defined('ABSPATH')) { |
| 11 | exit; // Exit if accessed directly. |
| 12 | } |
| 13 | use Kirki\HelperFunctions; |
| 14 | |
| 15 | |
| 16 | /** |
| 17 | * Role manager API Class |
| 18 | */ |
| 19 | class RBAC |
| 20 | { |
| 21 | |
| 22 | /** |
| 23 | * Get members based on role |
| 24 | * |
| 25 | * @return void wp_send_json. |
| 26 | */ |
| 27 | public static function members_based_on_role() |
| 28 | { |
| 29 | $roles = static::get_all_roles(); |
| 30 | $user_count = static::get_user_count_based_on_role($roles); |
| 31 | $user_dps = static::get_user_dp($roles); |
| 32 | $access_levels = static::get_access_level($roles); |
| 33 | |
| 34 | wp_send_json( |
| 35 | array( |
| 36 | 'user_count' => $user_count, |
| 37 | 'roles' => $roles, |
| 38 | 'dp' => $user_dps, |
| 39 | 'access' => $access_levels, |
| 40 | ) |
| 41 | ); |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Update access lavel |
| 46 | * |
| 47 | * @return void wp_send_json. |
| 48 | */ |
| 49 | public static function update_access_level() |
| 50 | { |
| 51 | //phpcs:ignore WordPress.Security.NonceVerification.Missing,WordPress.Security.NonceVerification.Recommended,WordPress.Security.ValidatedSanitizedInput.MissingUnslash,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized |
| 52 | $role_name = HelperFunctions::sanitize_text(isset($_POST['roleName']) ? $_POST['roleName'] : null); |
| 53 | //phpcs:ignore WordPress.Security.NonceVerification.Missing,WordPress.Security.NonceVerification.Recommended,WordPress.Security.ValidatedSanitizedInput.MissingUnslash,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized |
| 54 | $access = HelperFunctions::sanitize_text(isset($_POST['access']) ? $_POST['access'] : null); |
| 55 | |
| 56 | // The access level of `administrator` & `subscriber` role can't be updated. |
| 57 | if (in_array($role_name, array('administrator', 'subscriber'), true)) { |
| 58 | wp_send_json(false); |
| 59 | die(); |
| 60 | } |
| 61 | ; |
| 62 | |
| 63 | if ($role_name && $access) { |
| 64 | $res = update_option('kirki_' . $role_name, $access, false); |
| 65 | if ($res) { |
| 66 | wp_send_json(true); |
| 67 | } |
| 68 | } |
| 69 | wp_send_json(false); |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Get editor access lavel |
| 74 | * |
| 75 | * @return void wp_send_json. |
| 76 | */ |
| 77 | public static function get_editor_access_level() |
| 78 | { |
| 79 | $curr_user = wp_get_current_user(); |
| 80 | |
| 81 | if (HelperFunctions::is_api_call_from_editor_preview() && HelperFunctions::is_api_header_post_editor_preview_token_valid()) { |
| 82 | wp_send_json(KIRKI_ACCESS_LEVELS['FULL_ACCESS']); |
| 83 | } |
| 84 | |
| 85 | if ($curr_user) { |
| 86 | $roles = $curr_user->roles; |
| 87 | $access_arr = array(); |
| 88 | |
| 89 | foreach ($roles as $role) { |
| 90 | $access = get_option('kirki_' . $role); |
| 91 | $access_arr[] = $access; |
| 92 | } |
| 93 | |
| 94 | if (in_array(KIRKI_ACCESS_LEVELS['FULL_ACCESS'], $access_arr, true)) { |
| 95 | wp_send_json(KIRKI_ACCESS_LEVELS['FULL_ACCESS']); |
| 96 | } elseif (in_array(KIRKI_ACCESS_LEVELS['CONTENT_ACCESS'], $access_arr, true)) { |
| 97 | wp_send_json(KIRKI_ACCESS_LEVELS['CONTENT_ACCESS']); |
| 98 | } elseif (in_array(KIRKI_ACCESS_LEVELS['VIEW_ACCESS'], $access_arr, true)) { |
| 99 | wp_send_json(KIRKI_ACCESS_LEVELS['VIEW_ACCESS']); |
| 100 | } else { |
| 101 | wp_send_json(KIRKI_ACCESS_LEVELS['NO_ACCESS']); |
| 102 | } |
| 103 | } else { |
| 104 | wp_send_json(KIRKI_ACCESS_LEVELS['VIEW_ACCESS']); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Get user count based on role |
| 110 | * |
| 111 | * @return int count. |
| 112 | */ |
| 113 | private static function get_user_count_based_on_role() |
| 114 | { |
| 115 | $result = count_users(); |
| 116 | $user_count = $result['avail_roles']; |
| 117 | unset($user_count['none']); // This is not needed. |
| 118 | return $user_count; |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Get user DP |
| 123 | * |
| 124 | * @param array $roles wp roles. |
| 125 | * @return array |
| 126 | */ |
| 127 | private static function get_user_dp($roles = array()) |
| 128 | { |
| 129 | if (count($roles)) { |
| 130 | $dp = array(); |
| 131 | |
| 132 | foreach ($roles as $role) { |
| 133 | $role_id = $role['id']; |
| 134 | |
| 135 | $users = get_users( |
| 136 | array( |
| 137 | 'role' => $role_id, |
| 138 | 'number' => 3, |
| 139 | ) |
| 140 | ); |
| 141 | $user_ids = array(); |
| 142 | |
| 143 | foreach ($users as $user) { |
| 144 | $user_ids[] = $user->ID; |
| 145 | } |
| 146 | |
| 147 | $avatars = array(); |
| 148 | foreach ($user_ids as $user_id) { |
| 149 | $avatar = get_avatar_url($user_id); |
| 150 | $avatars[] = $avatar; |
| 151 | } |
| 152 | |
| 153 | $dp[$role_id] = $avatars; |
| 154 | } |
| 155 | |
| 156 | return $dp; |
| 157 | } |
| 158 | return array(); |
| 159 | } |
| 160 | |
| 161 | /** |
| 162 | * Get all roles |
| 163 | * |
| 164 | * @return array |
| 165 | */ |
| 166 | public static function get_all_roles() |
| 167 | { |
| 168 | global $wp_roles; |
| 169 | $role_list = $wp_roles->roles; |
| 170 | $roles = array(); |
| 171 | |
| 172 | if (is_array($role_list)) { |
| 173 | foreach ($role_list as $key => $val) { |
| 174 | $roles[] = array( |
| 175 | 'id' => $key, |
| 176 | 'name' => $val['name'], |
| 177 | ); |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | return $roles; |
| 182 | } |
| 183 | |
| 184 | /** |
| 185 | * Get access level |
| 186 | * |
| 187 | * @param array $roles wp roles. |
| 188 | * @return array |
| 189 | * |
| 190 | * @deprecated |
| 191 | * @see \Kirki\App\Supports\Role::get_access_levels_by_roles() |
| 192 | */ |
| 193 | public static function get_access_level($roles) |
| 194 | { |
| 195 | $access_levels = array(); |
| 196 | |
| 197 | foreach ($roles as $role) { |
| 198 | $role_id = $role['id']; |
| 199 | $result = get_option('kirki_' . $role_id); |
| 200 | |
| 201 | if ($result) { |
| 202 | $access_levels[$role_id] = $result; |
| 203 | } else { |
| 204 | if (in_array($role_id, KIRKI_USERS_DEFAULT_FULL_ACCESS, true)) { |
| 205 | add_option('kirki_' . $role_id, KIRKI_ACCESS_LEVELS['FULL_ACCESS']); |
| 206 | $access_levels[$role_id] = KIRKI_ACCESS_LEVELS['FULL_ACCESS']; |
| 207 | } else { |
| 208 | add_option('kirki_' . $role_id, KIRKI_ACCESS_LEVELS['NO_ACCESS']); |
| 209 | $access_levels[$role_id] = KIRKI_ACCESS_LEVELS['NO_ACCESS']; |
| 210 | } |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | return $access_levels; |
| 215 | } |
| 216 | } |
| 217 |