Menu.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 | * Menu object |
| 12 | * |
| 13 | * @since 6.9.19 https://github.com/aamplugin/advanced-access-manager/issues/331 |
| 14 | * https://github.com/aamplugin/advanced-access-manager/issues/334 |
| 15 | * @since 6.9.13 https://github.com/aamplugin/advanced-access-manager/issues/293 |
| 16 | * @since 6.5.0 https://github.com/aamplugin/advanced-access-manager/issues/105 |
| 17 | * @since 6.2.2 Added new filter `aam_backend_menu_is_restricted_filter` so it can |
| 18 | * be integrated with access policy wildcard |
| 19 | * @since 6.0.0 Initial implementation of the method |
| 20 | * |
| 21 | * @package AAM |
| 22 | * @version 6.9.19 |
| 23 | */ |
| 24 | class AAM_Core_Object_Menu extends AAM_Core_Object |
| 25 | { |
| 26 | |
| 27 | /** |
| 28 | * Type of object |
| 29 | * |
| 30 | * @version 6.0.0 |
| 31 | */ |
| 32 | const OBJECT_TYPE = 'menu'; |
| 33 | |
| 34 | /** |
| 35 | * @inheritdoc |
| 36 | * |
| 37 | * @since 6.5.0 https://github.com/aamplugin/advanced-access-manager/issues/105 |
| 38 | * @since 6.0.0 Initial implementation of the method |
| 39 | * |
| 40 | * @version 6.5.0 |
| 41 | */ |
| 42 | protected function initialize() |
| 43 | { |
| 44 | $option = $this->getSubject()->readOption(self::OBJECT_TYPE); |
| 45 | |
| 46 | $this->determineOverwritten($option); |
| 47 | |
| 48 | // Trigger custom functionality that may populate the menu options. For |
| 49 | // example, this hooks is used by Access Policy service |
| 50 | $option = apply_filters('aam_menu_object_option_filter', $option, $this); |
| 51 | |
| 52 | // Making sure that all menu keys are lowercase |
| 53 | $normalized = array(); |
| 54 | foreach($option as $key => $val) { |
| 55 | $normalized[strtolower($key)] = $val; |
| 56 | } |
| 57 | |
| 58 | $this->setOption(is_array($normalized) ? $normalized : array()); |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Check is menu or submenu is restricted |
| 63 | * |
| 64 | * @param string $menu |
| 65 | * |
| 66 | * @return boolean |
| 67 | * |
| 68 | * @since 6.2.2 Added new filter `aam_backend_menu_is_restricted_filter` |
| 69 | * @since 6.0.0 Initial implementation of the method |
| 70 | * |
| 71 | * @access public |
| 72 | * @version 6.5.0 |
| 73 | */ |
| 74 | public function isRestricted($menu) |
| 75 | { |
| 76 | // Decode URL in case of any special characters like & |
| 77 | $s = strtolower(htmlspecialchars_decode($menu)); |
| 78 | |
| 79 | if (!in_array($s, array('index.php', 'menu-index.php'))) { |
| 80 | $options = $this->getOption(); |
| 81 | $parent = $this->getParentMenu($s); |
| 82 | |
| 83 | // Step #1. Check if menu is directly restricted |
| 84 | $direct = !empty($options[$s]); |
| 85 | |
| 86 | // Step #2. Check if whole branch is restricted |
| 87 | $branch = !empty($options['menu-' . $s]); |
| 88 | |
| 89 | // Step #3. Check if dynamic submenu is restricted because of whole branch |
| 90 | $indirect = ($parent && (!empty($options['menu-' . $parent]))); |
| 91 | |
| 92 | $restricted = apply_filters( |
| 93 | 'aam_backend_menu_is_restricted_filter', |
| 94 | $direct || $branch || $indirect, |
| 95 | $s, |
| 96 | $this |
| 97 | ); |
| 98 | } else { |
| 99 | $restricted = false; |
| 100 | } |
| 101 | |
| 102 | return $restricted; |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Get parent menu |
| 107 | * |
| 108 | * @param string $search |
| 109 | * |
| 110 | * @return string|null |
| 111 | * |
| 112 | * @since 6.9.19 https://github.com/aamplugin/advanced-access-manager/issues/331 |
| 113 | * https://github.com/aamplugin/advanced-access-manager/issues/334 |
| 114 | * @since 6.9.13 https://github.com/aamplugin/advanced-access-manager/issues/293 |
| 115 | * @since 6.2.2 Made the method public |
| 116 | * @since 6.0.0 Initial implementation of the method |
| 117 | * |
| 118 | * @access public |
| 119 | * @global array $submenu |
| 120 | * @version 6.9.19 |
| 121 | */ |
| 122 | public function getParentMenu($search) |
| 123 | { |
| 124 | global $submenu; |
| 125 | |
| 126 | $result = $this->findParentInArray($submenu, $search); |
| 127 | |
| 128 | // If we cannot find parent menu in current $submenu array, try to find it |
| 129 | // in the cached menu generated by super admin. This is important to cover |
| 130 | // scenarios where submenus bubble up to menu. E.g. Profile |
| 131 | if (is_null($result)) { |
| 132 | $cache = AAM_Service_AdminMenu::getInstance()->getMenuCache(); |
| 133 | $result = $this->findParentInArray( |
| 134 | isset($cache['submenu']) ? $cache['submenu'] : array(), $search |
| 135 | ); |
| 136 | } |
| 137 | |
| 138 | return $result; |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * Find parent menu from the array of menu items |
| 143 | * |
| 144 | * @param array $array |
| 145 | * @param string $search |
| 146 | * |
| 147 | * @return null|string |
| 148 | * |
| 149 | * @access protected |
| 150 | * @version 6.9.19 |
| 151 | */ |
| 152 | protected function findParentInArray($array, $search) |
| 153 | { |
| 154 | $result = null; |
| 155 | |
| 156 | if (is_array($array)) { |
| 157 | foreach ($array as $parent => $subs) { |
| 158 | foreach ($subs as $sub) { |
| 159 | if (isset($sub[2]) && $sub[2] === $search) { |
| 160 | $result = $parent; |
| 161 | break; |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | if ($result !== null) { |
| 166 | break; |
| 167 | } |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | return $result; |
| 172 | } |
| 173 | |
| 174 | } |