Shortcode
4 months ago
AccessDeniedRedirect.php
1 year ago
AdminToolbar.php
1 year ago
ApiRoute.php
1 year ago
BackendMenu.php
1 year ago
BaseTrait.php
1 year ago
Capability.php
1 year ago
Content.php
4 months ago
Core.php
8 months ago
Hooks.php
1 year ago
Identity.php
1 month ago
Jwt.php
1 month ago
LoginRedirect.php
1 year ago
LogoutRedirect.php
1 month ago
Metaboxes.php
1 year ago
NotFoundRedirect.php
1 year ago
Policies.php
1 year ago
SecureLogin.php
1 year ago
SecurityAudit.php
1 year ago
Shortcodes.php
4 months ago
Urls.php
1 year ago
Welcome.php
1 year ago
Widgets.php
1 year ago
BackendMenu.php
207 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 | * Admin Menu service |
| 12 | * |
| 13 | * @package AAM |
| 14 | * @version 7.0.0 |
| 15 | */ |
| 16 | class AAM_Service_BackendMenu |
| 17 | { |
| 18 | use AAM_Service_BaseTrait; |
| 19 | |
| 20 | /** |
| 21 | * Constructor |
| 22 | * |
| 23 | * @return void |
| 24 | * @access protected |
| 25 | * |
| 26 | * @version 7.0.4 |
| 27 | */ |
| 28 | protected function __construct() |
| 29 | { |
| 30 | // Register RESTful API endpoints |
| 31 | AAM_Restful_BackendMenu::bootstrap(); |
| 32 | |
| 33 | add_action('init', function() { |
| 34 | $this->initialize_hooks(); |
| 35 | }, PHP_INT_MAX); |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * Initialize Admin Menu hooks |
| 40 | * |
| 41 | * @return void |
| 42 | * @access protected |
| 43 | * |
| 44 | * @version 7.0.4 |
| 45 | */ |
| 46 | protected function initialize_hooks() |
| 47 | { |
| 48 | if (is_admin()) { |
| 49 | // Hook that initialize the AAM UI part of the service |
| 50 | add_action('aam_initialize_ui_action', function () { |
| 51 | AAM_Backend_Feature_Main_BackendMenu::register(); |
| 52 | }); |
| 53 | |
| 54 | // Filter the admin menu only when we are not on the AAM page and user |
| 55 | // does not have the ability to manage admin menu through AAM UI |
| 56 | add_filter('parent_file', function($parent_file) { |
| 57 | if (is_admin() |
| 58 | && filter_input(INPUT_GET, 'page') === 'aam' |
| 59 | && current_user_can('aam_manage_backend_menu') |
| 60 | ) { |
| 61 | AAM::api()->backend_menu()->get_items(); |
| 62 | } else { |
| 63 | $this->filter_menu(); |
| 64 | } |
| 65 | |
| 66 | return $parent_file; |
| 67 | }, PHP_INT_MAX); |
| 68 | } |
| 69 | |
| 70 | // Control admin area |
| 71 | if (!defined('DOING_AJAX') || !DOING_AJAX) { |
| 72 | add_action('admin_init', function() { |
| 73 | $this->_admin_init(); |
| 74 | }, PHP_INT_MAX); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Filter Admin Menu |
| 80 | * |
| 81 | * Keep in mind that this function only filter the menu items but does not |
| 82 | * restrict access to them. |
| 83 | * |
| 84 | * @return void |
| 85 | * @access public |
| 86 | * |
| 87 | * @version 7.0.0 |
| 88 | */ |
| 89 | public function filter_menu() |
| 90 | { |
| 91 | global $menu, $submenu; |
| 92 | |
| 93 | $service = AAM::api()->backend_menu(); |
| 94 | |
| 95 | foreach ($menu as $id => $item) { |
| 96 | $menu_slug = $item[2]; |
| 97 | $is_restricted = $service->is_denied('menu/' . $menu_slug); |
| 98 | |
| 99 | // If top level menu has submenu items - filter them out as well |
| 100 | if (!empty($submenu[$menu_slug])) { |
| 101 | $submenus = $this->_filter_submenu( |
| 102 | $submenu[$menu_slug], $service |
| 103 | ); |
| 104 | |
| 105 | // If all submenu items are restricted, there is no need to |
| 106 | // render the top level menu because the top level menu always |
| 107 | // points to the first submenu item |
| 108 | if (count($submenus) === 0) { |
| 109 | unset($submenu[$menu_slug]); |
| 110 | unset($menu[$id]); |
| 111 | } else { |
| 112 | $submenu[$menu_slug] = $submenus; |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | // Remove the top level menu item if it is restricted and no other |
| 117 | // sub menu items are available |
| 118 | if ($is_restricted |
| 119 | && empty($submenu[$menu_slug]) |
| 120 | && $menu_slug !== 'index.php' |
| 121 | ) { |
| 122 | unset($menu[$id]); |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | // Remove duplicated separators |
| 127 | $count = 0; |
| 128 | |
| 129 | foreach ($menu as $id => $item) { |
| 130 | if (preg_match('/^separator/', $item[2])) { |
| 131 | if ($count === 0) { |
| 132 | $count++; |
| 133 | } else { |
| 134 | unset($menu[$id]); |
| 135 | } |
| 136 | } else { |
| 137 | $count = 0; |
| 138 | } |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * Check screen direct access |
| 144 | * |
| 145 | * @return void |
| 146 | * @access public |
| 147 | * |
| 148 | * @version 7.0.0 |
| 149 | */ |
| 150 | private function _admin_init() |
| 151 | { |
| 152 | global $plugin_page; |
| 153 | |
| 154 | // Compile menu |
| 155 | $id = $plugin_page; |
| 156 | |
| 157 | if (empty($id)) { |
| 158 | $id = basename(AAM::api()->misc->get($_SERVER, 'SCRIPT_NAME')); |
| 159 | $taxonomy = filter_input(INPUT_GET, 'taxonomy'); |
| 160 | $post_type = filter_input(INPUT_GET, 'post_type'); |
| 161 | $page = filter_input(INPUT_GET, 'page'); |
| 162 | $params = []; |
| 163 | |
| 164 | if (!empty($taxonomy)) { |
| 165 | array_push($params, 'taxonomy=' . $taxonomy); |
| 166 | } |
| 167 | |
| 168 | if (!empty($post_type) && ($post_type !== 'post')) { |
| 169 | array_push($params, 'post_type=' . $post_type); |
| 170 | } elseif (!empty($page)) { |
| 171 | array_push($params, 'page=' . $page); |
| 172 | } |
| 173 | |
| 174 | if (count($params)) { |
| 175 | $id .= '?' . implode('&', $params); |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | if (AAM::api()->backend_menu()->is_denied($id)) { |
| 180 | AAM::api()->redirect->do_access_denied_redirect(); |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | /** |
| 185 | * Filter submenu |
| 186 | * |
| 187 | * @param array $submenus |
| 188 | * @param AAM_Framework_Service_BackendMenu $service |
| 189 | * @param bool $default_effect |
| 190 | * |
| 191 | * @return array |
| 192 | * @access private |
| 193 | * |
| 194 | * @version 7.0.0 |
| 195 | */ |
| 196 | private function _filter_submenu($submenus, $service) |
| 197 | { |
| 198 | foreach ($submenus as $id => $item) { |
| 199 | if ($service->is_denied($item[2])) { |
| 200 | unset($submenus[$id]); |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | return array_values($submenus); |
| 205 | } |
| 206 | |
| 207 | } |