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 | * Backend menu manager |
| 12 | * |
| 13 | * @since 6.7.9 https://github.com/aamplugin/advanced-access-manager/issues/192 |
| 14 | * @since 6.0.0 Initial implementation of the class |
| 15 | * |
| 16 | * @package AAM |
| 17 | * @version 6.7.9 |
| 18 | */ |
| 19 | class AAM_Backend_Feature_Main_Menu |
| 20 | extends AAM_Backend_Feature_Abstract implements AAM_Backend_Feature_ISubjectAware |
| 21 | { |
| 22 | |
| 23 | /** |
| 24 | * Default access capability to the service |
| 25 | * |
| 26 | * @version 6.0.0 |
| 27 | */ |
| 28 | const ACCESS_CAPABILITY = 'aam_manage_admin_menu'; |
| 29 | |
| 30 | /** |
| 31 | * Type of AAM core object |
| 32 | * |
| 33 | * @version 6.0.0 |
| 34 | */ |
| 35 | const OBJECT_TYPE = AAM_Core_Object_Menu::OBJECT_TYPE; |
| 36 | |
| 37 | /** |
| 38 | * HTML template to render |
| 39 | * |
| 40 | * @version 6.0.0 |
| 41 | */ |
| 42 | const TEMPLATE = 'service/menu.php'; |
| 43 | |
| 44 | /** |
| 45 | * Save menu settings |
| 46 | * |
| 47 | * @return string |
| 48 | * |
| 49 | * @since 6.7.9 https://github.com/aamplugin/advanced-access-manager/issues/192 |
| 50 | * @since 6.0.0 Initial implementation of the method |
| 51 | * |
| 52 | * @access public |
| 53 | * @version 6.7.9 |
| 54 | */ |
| 55 | public function save() |
| 56 | { |
| 57 | $status = $this->getFromPost('status'); |
| 58 | |
| 59 | $object = AAM_Backend_Subject::getInstance()->getObject( |
| 60 | self::OBJECT_TYPE, null, true |
| 61 | ); |
| 62 | |
| 63 | foreach (AAM_Core_Request::post('items', array()) as $item) { |
| 64 | $object->updateOptionItem($item, !empty($status)); |
| 65 | } |
| 66 | |
| 67 | $result = $object->save(); |
| 68 | |
| 69 | return wp_json_encode(array('status' => ($result ? 'success' : 'failure'))); |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Get admin menu |
| 74 | * |
| 75 | * Based on the list of capabilities that current subject has, prepare |
| 76 | * complete menu list and return it. |
| 77 | * |
| 78 | * @return array |
| 79 | * |
| 80 | * @access public |
| 81 | * @version 6.0.0 |
| 82 | */ |
| 83 | public function getMenu() |
| 84 | { |
| 85 | $response = array(); |
| 86 | |
| 87 | $cache = AAM_Service_AdminMenu::getInstance()->getMenuCache(); |
| 88 | $subject = AAM_Backend_Subject::getInstance(); |
| 89 | |
| 90 | // Create menu list with submenus |
| 91 | if (!empty($cache)) { |
| 92 | $object = $subject->getObject(self::OBJECT_TYPE); |
| 93 | |
| 94 | foreach ($cache['menu'] as $item) { |
| 95 | if (preg_match('/^separator/', $item[2])) { |
| 96 | continue; //skip separator |
| 97 | } |
| 98 | |
| 99 | $response[] = array( |
| 100 | // Add menu- prefix to define that this is the top level menu. |
| 101 | // WordPress by default gives the same menu id to the first |
| 102 | // submenu |
| 103 | 'id' => 'menu-' . $item[2], |
| 104 | 'uri' => $this->prepareAdminURI($item[2]), |
| 105 | 'name' => $this->filterMenuName($item[0]), |
| 106 | 'submenu' => $this->getSubmenu($item[2], $cache['submenu']), |
| 107 | 'capability' => $item[1], |
| 108 | 'checked' => $object->isRestricted('menu-' . $item[2]) |
| 109 | ); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | return $response; |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Normalize menu item |
| 118 | * |
| 119 | * @param string $menu |
| 120 | * |
| 121 | * @return string |
| 122 | * |
| 123 | * @access protected |
| 124 | * @version 6.0.0 |
| 125 | */ |
| 126 | protected function normalizeItem($menu) |
| 127 | { |
| 128 | if (strpos($menu, 'customize.php') === 0) { |
| 129 | $menu = 'customize.php'; |
| 130 | } |
| 131 | |
| 132 | return $menu; |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * Prepare filtered submenu |
| 137 | * |
| 138 | * @param string $menu |
| 139 | * |
| 140 | * @return array |
| 141 | * |
| 142 | * @access protected |
| 143 | * @version 6.0.0 |
| 144 | */ |
| 145 | protected function getSubmenu($menu, $submenu) |
| 146 | { |
| 147 | $response = array(); |
| 148 | |
| 149 | $object = AAM_Backend_Subject::getInstance()->getObject(self::OBJECT_TYPE); |
| 150 | |
| 151 | if (array_key_exists($menu, $submenu) && is_array($submenu[$menu])) { |
| 152 | foreach ($submenu[$menu] as $item) { |
| 153 | $id = $this->normalizeItem($item[2]); |
| 154 | |
| 155 | $response[] = array( |
| 156 | 'id' => $id, |
| 157 | 'uri' => $this->prepareAdminURI($item[2]), |
| 158 | 'name' => $this->filterMenuName($item[0]), |
| 159 | 'capability' => $item[1], |
| 160 | 'checked' => $object->isRestricted($id) |
| 161 | ); |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | return $response; |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * Prepare admin URI for the menu item |
| 170 | * |
| 171 | * @param string $resource |
| 172 | * |
| 173 | * @return string |
| 174 | * |
| 175 | * @access protected |
| 176 | * @version 6.0.0 |
| 177 | */ |
| 178 | protected function prepareAdminURI($resource) |
| 179 | { |
| 180 | $hook = get_plugin_page_hook($resource, 'admin.php'); |
| 181 | $uri = (!empty($hook) ? 'admin.php?page=' . $resource : $resource); |
| 182 | |
| 183 | return '/wp-admin/' . $uri; |
| 184 | } |
| 185 | |
| 186 | /** |
| 187 | * Filter menu name |
| 188 | * |
| 189 | * Strip any HTML tags from the menu name and also remove the trailing |
| 190 | * numbers in case of Plugin or Comments menu name. |
| 191 | * |
| 192 | * @param string $name |
| 193 | * |
| 194 | * @return string |
| 195 | * |
| 196 | * @access protected |
| 197 | * @version 6.0.0 |
| 198 | */ |
| 199 | protected function filterMenuName($name) |
| 200 | { |
| 201 | $filtered = trim(wp_strip_all_tags( |
| 202 | preg_replace('@<(span)[^>]*?>.*?</\\1>@si', '', $name), |
| 203 | true |
| 204 | )); |
| 205 | |
| 206 | return preg_replace('/([\d]+)$/', '', $filtered); |
| 207 | } |
| 208 | |
| 209 | /** |
| 210 | * Check if there is at least one submenu restricted |
| 211 | * |
| 212 | * @param array $subs |
| 213 | * |
| 214 | * @return boolean |
| 215 | * |
| 216 | * @access protected |
| 217 | * @version 6.0.0 |
| 218 | */ |
| 219 | protected function hasSubmenuChecked($subs) |
| 220 | { |
| 221 | $has = false; |
| 222 | |
| 223 | if (!empty($subs)) { |
| 224 | foreach ($subs as $submenu) { |
| 225 | if ($submenu['checked']) { |
| 226 | $has = true; |
| 227 | break; |
| 228 | } |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | return $has; |
| 233 | } |
| 234 | |
| 235 | /** |
| 236 | * Register Admin Menu feature |
| 237 | * |
| 238 | * @return void |
| 239 | * |
| 240 | * @access public |
| 241 | * @version 6.0.0 |
| 242 | */ |
| 243 | public static function register() |
| 244 | { |
| 245 | AAM_Backend_Feature::registerFeature((object) array( |
| 246 | 'uid' => 'admin_menu', |
| 247 | 'position' => 5, |
| 248 | 'title' => __('Backend Menu', AAM_KEY), |
| 249 | 'capability' => self::ACCESS_CAPABILITY, |
| 250 | 'type' => 'main', |
| 251 | 'subjects' => array( |
| 252 | AAM_Core_Subject_Role::UID, |
| 253 | AAM_Core_Subject_User::UID, |
| 254 | AAM_Core_Subject_Default::UID |
| 255 | ), |
| 256 | 'view' => __CLASS__ |
| 257 | )); |
| 258 | } |
| 259 | |
| 260 | } |