Feature
7 years ago
View
7 years ago
Widget
7 years ago
phtml
7 years ago
Authorization.php
7 years ago
Feature.php
7 years ago
Filter.php
7 years ago
Manager.php
7 years ago
Subject.php
7 years ago
View.php
7 years ago
Filter.php
296 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 manager |
| 12 | * |
| 13 | * @package AAM |
| 14 | * @author Vasyl Martyniuk <vasyl@vasyltech.com> |
| 15 | */ |
| 16 | class AAM_Backend_Filter { |
| 17 | |
| 18 | /** |
| 19 | * Instance of itself |
| 20 | * |
| 21 | * @var AAM_Backend_Filter |
| 22 | * |
| 23 | * @access private |
| 24 | */ |
| 25 | private static $_instance = null; |
| 26 | |
| 27 | /** |
| 28 | * Initialize backend filters |
| 29 | * |
| 30 | * @return void |
| 31 | * |
| 32 | * @access protected |
| 33 | */ |
| 34 | protected function __construct() { |
| 35 | //menu filter |
| 36 | if (!AAM::isAAM() || !current_user_can('aam_manage_admin_menu')) { |
| 37 | add_filter('parent_file', array($this, 'filterMenu'), 999, 1); |
| 38 | } |
| 39 | |
| 40 | //manager WordPress metaboxes |
| 41 | add_action("in_admin_header", array($this, 'metaboxes'), 999); |
| 42 | add_action("widgets_admin_page", array($this, 'metaboxes'), 999); |
| 43 | |
| 44 | //control admin area |
| 45 | add_action('admin_notices', array($this, 'adminNotices'), -1); |
| 46 | add_action('network_admin_notices', array($this, 'adminNotices'), -1); |
| 47 | add_action('user_admin_notices', array($this, 'adminNotices'), -1); |
| 48 | |
| 49 | //post restrictions |
| 50 | add_filter('page_row_actions', array($this, 'postRowActions'), 10, 2); |
| 51 | add_filter('post_row_actions', array($this, 'postRowActions'), 10, 2); |
| 52 | |
| 53 | add_action('pre_post_update', array($this, 'prePostUpdate'), 10, 2); |
| 54 | |
| 55 | //user/role filters |
| 56 | if (!is_multisite() || !is_super_admin()) { |
| 57 | add_filter('editable_roles', array($this, 'filterRoles')); |
| 58 | add_action('pre_get_users', array($this, 'filterUserQuery'), 999); |
| 59 | add_filter('views_users', array($this, 'filterViews')); |
| 60 | } |
| 61 | |
| 62 | AAM_Backend_Authorization::bootstrap(); //bootstrap backend authorization |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Filter the Admin Menu |
| 67 | * |
| 68 | * @param string $parent_file |
| 69 | * |
| 70 | * @return string |
| 71 | * |
| 72 | * @access public |
| 73 | */ |
| 74 | public function filterMenu($parent_file) { |
| 75 | //filter admin menu |
| 76 | AAM::getUser()->getObject('menu')->filter(); |
| 77 | |
| 78 | return $parent_file; |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Handle metabox initialization process |
| 83 | * |
| 84 | * @return void |
| 85 | * |
| 86 | * @access public |
| 87 | */ |
| 88 | public function metaboxes() { |
| 89 | global $post; |
| 90 | |
| 91 | //make sure that nobody is playing with screen options |
| 92 | if (is_a($post, 'WP_Post')) { |
| 93 | $screen = $post->post_type; |
| 94 | } else { |
| 95 | $screen_object = get_current_screen(); |
| 96 | $screen = ($screen_object ? $screen_object->id : ''); |
| 97 | } |
| 98 | |
| 99 | if (AAM_Core_Request::get('init') !== 'metabox') { |
| 100 | if ($screen !== 'widgets') { |
| 101 | AAM::getUser()->getObject('metabox')->filterBackend($screen); |
| 102 | } else { |
| 103 | AAM::getUser()->getObject('metabox')->filterAppearanceWidgets(); |
| 104 | } |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Manage notifications visibility |
| 110 | * |
| 111 | * @return void |
| 112 | * |
| 113 | * @access public |
| 114 | */ |
| 115 | public function adminNotices() { |
| 116 | if (AAM_Core_API::capabilityExists('show_admin_notices')) { |
| 117 | if (!current_user_can('show_admin_notices')) { |
| 118 | remove_all_actions('admin_notices'); |
| 119 | remove_all_actions('network_admin_notices'); |
| 120 | remove_all_actions('user_admin_notices'); |
| 121 | } |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Post Quick Menu Actions Filtering |
| 127 | * |
| 128 | * @param array $actions |
| 129 | * @param WP_Post $post |
| 130 | * |
| 131 | * @return array |
| 132 | * |
| 133 | * @access public |
| 134 | */ |
| 135 | public function postRowActions($actions, $post) { |
| 136 | $object = AAM::getUser()->getObject('post', $post->ID, $post); |
| 137 | |
| 138 | //filter edit menu |
| 139 | if (!$object->allowed('backend.edit')) { |
| 140 | if (isset($actions['edit'])) { |
| 141 | unset($actions['edit']); |
| 142 | } |
| 143 | if (isset($actions['inline hide-if-no-js'])) { |
| 144 | unset($actions['inline hide-if-no-js']); |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | //filter delete menu |
| 149 | if (!$object->allowed('backend.delete')) { |
| 150 | if (isset($actions['trash'])) { unset($actions['trash']); } |
| 151 | if (isset($actions['delete'])) { unset($actions['delete']); } |
| 152 | } |
| 153 | |
| 154 | //filter edit menu |
| 155 | if (!$object->allowed('backend.publish')) { |
| 156 | if (isset($actions['inline hide-if-no-js'])) { |
| 157 | unset($actions['inline hide-if-no-js']); |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | return $actions; |
| 162 | } |
| 163 | |
| 164 | /** |
| 165 | * Post update hook |
| 166 | * |
| 167 | * Clear cache if post owner changed |
| 168 | * |
| 169 | * @param int $id |
| 170 | * @param array $data |
| 171 | * |
| 172 | * @return void |
| 173 | * |
| 174 | * @access public |
| 175 | */ |
| 176 | public function prePostUpdate($id, $data) { |
| 177 | $post = get_post($id); |
| 178 | |
| 179 | if (intval($post->post_author) !== intval($data['post_author'])) { |
| 180 | AAM_Core_API::clearCache(); |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | /** |
| 185 | * Filter roles |
| 186 | * |
| 187 | * @param array $roles |
| 188 | * |
| 189 | * @return array |
| 190 | */ |
| 191 | public function filterRoles($roles) { |
| 192 | static $levels = array(); // to speed-up the execution |
| 193 | |
| 194 | $userLevel = AAM::getUser()->getMaxLevel(); |
| 195 | |
| 196 | //filter roles |
| 197 | foreach($roles as $id => $role) { |
| 198 | if (!empty($role['capabilities']) && is_array($role['capabilities'])) { |
| 199 | if (!isset($levels[$id])) { |
| 200 | $levels[$id] = AAM_Core_API::maxLevel($role['capabilities']); |
| 201 | } |
| 202 | if ($userLevel < $levels[$id]) { |
| 203 | unset($roles[$id]); |
| 204 | } elseif ($userLevel === $levels[$id] && $this->filterSameLevel()) { |
| 205 | unset($roles[$id]); |
| 206 | } |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | return $roles; |
| 211 | } |
| 212 | |
| 213 | /** |
| 214 | * |
| 215 | * @return type |
| 216 | */ |
| 217 | protected function filterSameLevel() { |
| 218 | $response = false; |
| 219 | |
| 220 | if (AAM_Core_API::capabilityExists('manage_same_user_level')) { |
| 221 | $response = !current_user_can('manage_same_user_level'); |
| 222 | } |
| 223 | |
| 224 | return $response; |
| 225 | } |
| 226 | |
| 227 | /** |
| 228 | * Filter user query |
| 229 | * |
| 230 | * Exclude all users that have higher user level |
| 231 | * |
| 232 | * @param object $query |
| 233 | * |
| 234 | * @access public |
| 235 | * |
| 236 | * @return void |
| 237 | */ |
| 238 | public function filterUserQuery($query) { |
| 239 | //current user max level |
| 240 | $max = AAM::getUser()->getMaxLevel(); |
| 241 | $exclude = array(); |
| 242 | $roles = AAM_Core_API::getRoles(); |
| 243 | |
| 244 | foreach($roles->role_objects as $id => $role) { |
| 245 | $roleMax = AAM_Core_API::maxLevel($role->capabilities); |
| 246 | if ($roleMax > $max ) { |
| 247 | $exclude[] = $id; |
| 248 | } elseif ($roleMax === $max && $this->filterSameLevel()) { |
| 249 | $exclude[] = $id; |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | $query->query_vars['role__not_in'] = $exclude; |
| 254 | } |
| 255 | |
| 256 | /** |
| 257 | * Filter user list view options |
| 258 | * |
| 259 | * @param array $views |
| 260 | * |
| 261 | * @return array |
| 262 | * |
| 263 | * @access public |
| 264 | */ |
| 265 | public function filterViews($views) { |
| 266 | $max = AAM::getUser()->getMaxLevel(); |
| 267 | $roles = AAM_Core_API::getRoles(); |
| 268 | |
| 269 | foreach($roles->role_objects as $id => $role) { |
| 270 | $roleMax = AAM_Core_API::maxLevel($role->capabilities); |
| 271 | if (isset($views[$id])) { |
| 272 | if ($roleMax > $max) { |
| 273 | unset($views[$id]); |
| 274 | } elseif ($roleMax === $max && $this->filterSameLevel()) { |
| 275 | unset($views[$id]); |
| 276 | } |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | return $views; |
| 281 | } |
| 282 | |
| 283 | /** |
| 284 | * Register backend filters and actions |
| 285 | * |
| 286 | * @return void |
| 287 | * |
| 288 | * @access public |
| 289 | */ |
| 290 | public static function register() { |
| 291 | if (is_null(self::$_instance)) { |
| 292 | self::$_instance = new self; |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | } |