User.php
| 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 | * User access level |
| 12 | * |
| 13 | * @property array $caps |
| 14 | * @property array $allcaps |
| 15 | * @property array $roles |
| 16 | * |
| 17 | * @package AAM |
| 18 | * @version 7.0.0 |
| 19 | */ |
| 20 | class AAM_Framework_AccessLevel_User implements AAM_Framework_AccessLevel_Interface |
| 21 | { |
| 22 | |
| 23 | use AAM_Framework_AccessLevel_BaseTrait; |
| 24 | |
| 25 | /** |
| 26 | * @inheritDoc |
| 27 | */ |
| 28 | protected $type = AAM_Framework_Type_AccessLevel::USER; |
| 29 | |
| 30 | /** |
| 31 | * Get parent access level |
| 32 | * |
| 33 | * @return AAM_Framework_AccessLevel_Interface |
| 34 | * @access public |
| 35 | * |
| 36 | * @version 7.0.0 |
| 37 | */ |
| 38 | public function get_parent() |
| 39 | { |
| 40 | $roles = $this->roles; |
| 41 | $primary_role = array_shift($roles); |
| 42 | |
| 43 | // It is possible that user either does not have any roles or the assigned |
| 44 | // role no longer exists. |
| 45 | // Note! AAM does not allow deleting roles if there is at least one user |
| 46 | // assigned. However, it can be delete through other plugin or custom code |
| 47 | if (wp_roles()->is_role($primary_role)) { |
| 48 | $parent = AAM_Framework_Manager::_()->access_levels->get( |
| 49 | AAM_Framework_Type_AccessLevel::ROLE, $primary_role |
| 50 | ); |
| 51 | |
| 52 | // If multi-role support is enabled & there are multiple roles assigned |
| 53 | // to user, then fetch them all |
| 54 | $mu_role_support = AAM_Framework_Manager::_()->config->get( |
| 55 | 'core.settings.multi_access_levels' |
| 56 | ); |
| 57 | |
| 58 | if ($mu_role_support && count($roles)) { |
| 59 | foreach ($roles as $role) { |
| 60 | $parent->add_sibling(AAM_Framework_Manager::_()->access_levels->get( |
| 61 | AAM_Framework_Type_AccessLevel::ROLE, $role |
| 62 | )); |
| 63 | } |
| 64 | } |
| 65 | } else { // In this case - the Default access level is parent |
| 66 | $parent = AAM_Framework_Manager::_()->access_levels->get( |
| 67 | AAM_Framework_Type_AccessLevel::DEFAULT |
| 68 | ); |
| 69 | } |
| 70 | |
| 71 | return $parent; |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * @inheritDoc |
| 76 | */ |
| 77 | public function get_id() |
| 78 | { |
| 79 | return $this->_proxy_instance->ID; |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * @inheritDoc |
| 84 | */ |
| 85 | public function get_display_name() |
| 86 | { |
| 87 | return $this->_proxy_instance->display_name; |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Initialize the access level |
| 92 | * |
| 93 | * @param WP_User $core_instance |
| 94 | * |
| 95 | * @return void |
| 96 | * @access protected |
| 97 | * |
| 98 | * @version 7.0.0 |
| 99 | */ |
| 100 | protected function initialize($core_instance) |
| 101 | { |
| 102 | $this->_proxy_instance = new AAM_Framework_Proxy_User( |
| 103 | $core_instance |
| 104 | ); |
| 105 | } |
| 106 | |
| 107 | } |