Feature
1 year ago
View
1 year ago
Widget
1 year ago
tmpl
3 months ago
AccessLevel.php
1 year ago
Feature.php
1 year ago
Manager.php
3 months ago
View.php
1 year ago
AccessLevel.php
298 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 | * Backend access level instance |
| 12 | * |
| 13 | * Currently managed access level. Based on the HTTP request data, define which |
| 14 | * access level is currently managed with AAM UI. |
| 15 | * |
| 16 | * @package AAM |
| 17 | * @version 7.0.0 |
| 18 | */ |
| 19 | class AAM_Backend_AccessLevel |
| 20 | { |
| 21 | |
| 22 | /** |
| 23 | * Single instance of itself |
| 24 | * |
| 25 | * @var AAM_Backend_AccessLevel |
| 26 | * @access private |
| 27 | * |
| 28 | * @version 7.0.0 |
| 29 | */ |
| 30 | private static $_instance = null; |
| 31 | |
| 32 | /** |
| 33 | * Access Level |
| 34 | * |
| 35 | * @var AAM_Framework_AccessLevel_Interface |
| 36 | * @access private |
| 37 | * |
| 38 | * @version 7.0.0 |
| 39 | */ |
| 40 | private $_access_level = null; |
| 41 | |
| 42 | /** |
| 43 | * Constructor |
| 44 | * |
| 45 | * @return void |
| 46 | * @access protected |
| 47 | * |
| 48 | * @version 7.0.0 |
| 49 | */ |
| 50 | protected function __construct() |
| 51 | { |
| 52 | $access_level_type = strtolower(AAM::api()->misc->get( |
| 53 | $_POST, 'access_level', '' |
| 54 | )); |
| 55 | |
| 56 | if ($access_level_type === AAM_Framework_Type_AccessLevel::ROLE) { |
| 57 | $access_level_id = AAM::api()->misc->get($_POST, 'role_id'); |
| 58 | } elseif ($access_level_type === AAM_Framework_Type_AccessLevel::USER) { |
| 59 | $access_level_id = AAM::api()->misc->get($_POST, 'user_id'); |
| 60 | } else { |
| 61 | $access_level_id = null; |
| 62 | } |
| 63 | |
| 64 | if (empty($access_level_type)) { |
| 65 | $al = $this->_get_last_managed_access_level(); |
| 66 | |
| 67 | if (!empty($al)) { |
| 68 | $access_level_type = $al['type']; |
| 69 | $access_level_id = !empty($al['id']) ? $al['id']: null; |
| 70 | } |
| 71 | } else { // Persist the last managed access level |
| 72 | AAM::api()->cache->set( |
| 73 | 'managed_access_level_by_' . get_current_user_id(), |
| 74 | [ |
| 75 | 'type' => $access_level_type, |
| 76 | 'id' => $access_level_id |
| 77 | ], |
| 78 | 2592000 // 30 days |
| 79 | ); |
| 80 | } |
| 81 | |
| 82 | if ($access_level_type) { |
| 83 | $this->_init_access_level($access_level_type, $access_level_id); |
| 84 | } else { |
| 85 | $this->_init_fallback_access_level(); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Check if current access level is role |
| 91 | * |
| 92 | * @return boolean |
| 93 | * @access public |
| 94 | * |
| 95 | * @version 7.0.0 |
| 96 | */ |
| 97 | public function is_role() |
| 98 | { |
| 99 | return $this->_access_level->type === AAM_Framework_Type_AccessLevel::ROLE; |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Check if current access level is user |
| 104 | * |
| 105 | * @return boolean |
| 106 | * @access public |
| 107 | * |
| 108 | * @version 7.0.0 |
| 109 | */ |
| 110 | public function is_user() |
| 111 | { |
| 112 | return $this->_access_level->type === AAM_Framework_Type_AccessLevel::USER; |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Check if current access level is visitor |
| 117 | * |
| 118 | * @return boolean |
| 119 | * @access public |
| 120 | * |
| 121 | * @version 7.0.0 |
| 122 | */ |
| 123 | public function is_visitor() |
| 124 | { |
| 125 | return $this->_access_level->type === AAM_Framework_Type_AccessLevel::VISITOR; |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Check if current access level is default |
| 130 | * |
| 131 | * @return boolean |
| 132 | * @access public |
| 133 | * |
| 134 | * @version 7.0.0 |
| 135 | */ |
| 136 | public function is_default() |
| 137 | { |
| 138 | return $this->_access_level->type === AAM_Framework_Type_AccessLevel::ALL; |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * Get last managed access level |
| 143 | * |
| 144 | * @return array|null |
| 145 | * @access private |
| 146 | * |
| 147 | * @version 7.0.0 |
| 148 | */ |
| 149 | private function _get_last_managed_access_level() |
| 150 | { |
| 151 | $level = AAM::api()->cache->get( |
| 152 | 'managed_access_level_by_' . get_current_user_id() |
| 153 | ); |
| 154 | |
| 155 | if (!is_null($level)) { |
| 156 | // Verifying that access level exists and is accessible |
| 157 | if ($level['type'] === 'role') { |
| 158 | if (!AAM::api()->roles->is_editable_role($level['id'])) { |
| 159 | $level = null; |
| 160 | } |
| 161 | } elseif ($level['type'] === 'user') { |
| 162 | $user = apply_filters('aam_get_user', get_user_by('id', $level['id'])); |
| 163 | |
| 164 | if ($user === false |
| 165 | || is_wp_error($user) |
| 166 | || !current_user_can('edit_user', $user->ID) |
| 167 | ) { |
| 168 | $level = null; |
| 169 | } |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | return $level; |
| 174 | } |
| 175 | |
| 176 | /** |
| 177 | * Initialize requested access level |
| 178 | * |
| 179 | * @param string $type |
| 180 | * @param int|string $id |
| 181 | * |
| 182 | * @return void |
| 183 | * @access private |
| 184 | * |
| 185 | * @version 7.0.0 |
| 186 | */ |
| 187 | private function _init_access_level($type, $id = null) |
| 188 | { |
| 189 | $this->_access_level = AAM::api()->access_levels->get($type, $id); |
| 190 | } |
| 191 | |
| 192 | /** |
| 193 | * Initialize fallback access level |
| 194 | * |
| 195 | * Based on user permissions, pick the first available access level that current |
| 196 | * user can manage |
| 197 | * |
| 198 | * @return void |
| 199 | * @access private |
| 200 | * |
| 201 | * @version 7.0.0 |
| 202 | */ |
| 203 | private function _init_fallback_access_level() |
| 204 | { |
| 205 | if (current_user_can('aam_manage_roles')) { |
| 206 | $roles = array_keys(get_editable_roles()); |
| 207 | $this->_init_access_level( |
| 208 | AAM_Framework_Type_AccessLevel::ROLE, array_pop($roles) |
| 209 | ); |
| 210 | } elseif (current_user_can('aam_manage_users')) { |
| 211 | $this->_init_access_level( |
| 212 | AAM_Framework_Type_AccessLevel::USER, get_current_user_id() |
| 213 | ); |
| 214 | } elseif (current_user_can('aam_manage_visitors')) { |
| 215 | $this->_init_access_level(AAM_Framework_Type_AccessLevel::VISITOR); |
| 216 | } elseif (current_user_can('aam_manage_default')) { |
| 217 | $this->_init_access_level(AAM_Framework_Type_AccessLevel::ALL); |
| 218 | } else { |
| 219 | AAM::api()->redirect->do_redirect([ |
| 220 | 'type' => 'custom_message', |
| 221 | 'message' => __('You do not have permission to manage any access levels', 'advanced-access-manager') |
| 222 | ]); |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | /** |
| 227 | * Get access level property |
| 228 | * |
| 229 | * @return mixed |
| 230 | * @access public |
| 231 | * |
| 232 | * @version 7.0.0 |
| 233 | */ |
| 234 | public function __get($name) |
| 235 | { |
| 236 | return $this->_access_level->$name; |
| 237 | } |
| 238 | |
| 239 | /** |
| 240 | * Call access level method |
| 241 | * |
| 242 | * @param string $name |
| 243 | * @param array $args |
| 244 | * |
| 245 | * @return mixed |
| 246 | * @access public |
| 247 | * |
| 248 | * @version 7.0.0 |
| 249 | */ |
| 250 | public function __call($name, $args) |
| 251 | { |
| 252 | return call_user_func_array(array($this->_access_level, $name), $args); |
| 253 | } |
| 254 | |
| 255 | /** |
| 256 | * Get AAM core subject |
| 257 | * |
| 258 | * @return AAM_Framework_AccessLevel_Interface |
| 259 | * @access public |
| 260 | * |
| 261 | * @version 7.0.0 |
| 262 | */ |
| 263 | public function get_access_level() |
| 264 | { |
| 265 | return $this->_access_level; |
| 266 | } |
| 267 | |
| 268 | /** |
| 269 | * Bootstrap the object |
| 270 | * |
| 271 | * @return AAM_Backend_AccessLevel |
| 272 | * @access public |
| 273 | * |
| 274 | * @version 7.0.0 |
| 275 | */ |
| 276 | public static function bootstrap() |
| 277 | { |
| 278 | if (is_null(self::$_instance)) { |
| 279 | self::$_instance = new self; |
| 280 | } |
| 281 | |
| 282 | return self::$_instance; |
| 283 | } |
| 284 | |
| 285 | /** |
| 286 | * Get single instance of itself |
| 287 | * |
| 288 | * @return AAM_Backend_AccessLevel |
| 289 | * @access public |
| 290 | * |
| 291 | * @version 7.0.0 |
| 292 | */ |
| 293 | public static function get_instance() |
| 294 | { |
| 295 | return self::bootstrap(); |
| 296 | } |
| 297 | |
| 298 | } |