PluginProbe ʕ •ᴥ•ʔ
Advanced Access Manager – Access Governance for WordPress / 5.3.5
Advanced Access Manager – Access Governance for WordPress v5.3.5
6.8.4 6.8.5 6.9.0 6.9.1 6.9.10 6.9.11 6.9.12 6.9.13 6.9.14 6.9.15 6.9.16 6.9.17 6.9.18 6.9.19 6.9.2 6.9.20 6.9.21 6.9.22 6.9.23 6.9.24 6.9.25 6.9.26 6.9.27 6.9.28 6.9.29 6.9.3 6.9.30 6.9.31 6.9.32 6.9.33 6.9.34 6.9.35 6.9.36 6.9.37 6.9.38 6.9.39 6.9.4 6.9.41 6.9.42 6.9.43 6.9.44 6.9.45 6.9.46 6.9.47 6.9.48 6.9.49 6.9.5 6.9.51 6.9.6 6.9.7 6.9.8 6.9.9 7.0.0 7.0.0-alpha.6 7.0.0-alpha.7 7.0.0-beta.1 7.0.0-rc1 7.0.0-rc2 7.0.0-rc3 7.0.1 7.0.10 7.0.11 7.0.2 7.0.3 7.0.4 7.0.5 7.0.6 7.0.7 7.0.8 7.0.9 7.1.0 7.1.1 trunk 3.0 4.0 4.0.1 4.1 4.2 4.3 4.4 4.4.1 4.5 4.6 4.6.1 4.6.2 4.7 4.7.1 4.7.2 4.7.5 4.7.6 4.8 4.8.1 4.9 4.9.1 4.9.2 4.9.3 4.9.4 4.9.5 4.9.5.1 4.9.5.2 5.0 5.0.1 5.0.2 5.0.3 5.0.4 5.0.5 5.0.6 5.0.7 5.0.8 5.1 5.1.1 5.10 5.11 5.2 5.2.1 5.2.5 5.2.6 5.2.7 5.3 5.3.1 5.3.2 5.3.3 5.3.4 5.3.5 5.4 5.4.1 5.4.2 5.4.3 5.4.3.1 5.4.3.2 5.5 5.5.1 5.5.2 5.6 5.6.1 5.6.1.1 5.7 5.7.1 5.7.2 5.7.3 5.8 5.8.1 5.8.2 5.8.3 5.9 5.9.1 5.9.1.1 5.9.2 5.9.2.1 5.9.3 5.9.4 5.9.5 5.9.6 5.9.6.1 5.9.6.2 5.9.6.3 5.9.7 5.9.7.1 5.9.7.2 5.9.7.3 5.9.8 5.9.8.1 5.9.9 5.9.9.1 6.0.0 6.0.1 6.0.2 6.0.3 6.0.4 6.0.5 6.1.0 6.1.1 6.2.0 6.2.1 6.2.2 6.3.0 6.3.1 6.3.2 6.3.3 6.4.0 6.4.1 6.4.2 6.4.3 6.5.0 6.5.1 6.5.2 6.5.3 6.5.4 6.6.0 6.6.1 6.6.2 6.6.3 6.6.4 6.7.0 6.7.1 6.7.2 6.7.3 6.7.4 6.7.5 6.7.6 6.7.7 6.7.8 6.7.9 6.8.0 6.8.1 6.8.2 6.8.3
advanced-access-manager / Application / Backend / Filter.php
advanced-access-manager / Application / Backend Last commit date
Feature 8 years ago View 8 years ago Widget 8 years ago phtml 8 years ago Authorization.php 8 years ago Feature.php 8 years ago Filter.php 8 years ago Manager.php 8 years ago Subject.php 8 years ago View.php 8 years ago
Filter.php
410 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 add_filter('parent_file', array($this, 'filterMenu'), 999, 1);
37
38 //manager WordPress metaboxes
39 add_action("in_admin_header", array($this, 'metaboxes'), 999);
40 add_action("widgets_admin_page", array($this, 'metaboxes'), 999);
41
42 //control admin area
43 add_action('admin_notices', array($this, 'adminNotices'), -1);
44 add_action('network_admin_notices', array($this, 'adminNotices'), -1);
45 add_action('user_admin_notices', array($this, 'adminNotices'), -1);
46
47 //admin bar
48 add_action('wp_before_admin_bar_render', array($this, 'filterAdminBar'), 999);
49
50 //post restrictions
51 add_filter('page_row_actions', array($this, 'postRowActions'), 10, 2);
52 add_filter('post_row_actions', array($this, 'postRowActions'), 10, 2);
53
54 //default category filder
55 // TODO - THIS HAS TO GO TO THE PLUS PACKAGE EXTENSION
56 add_filter('pre_option_default_category', array($this, 'filterDefaultCategory'));
57
58 add_action('pre_post_update', array($this, 'prePostUpdate'), 10, 2);
59
60 //user/role filters
61 if (!is_multisite() || !is_super_admin()) {
62 add_filter('editable_roles', array($this, 'filterRoles'));
63 add_action('pre_get_users', array($this, 'filterUserQuery'), 999);
64 add_filter('views_users', array($this, 'filterViews'));
65 }
66
67 // Check if user has ability to perform certain task based on provided
68 // capability and meta data
69 add_filter(
70 'user_has_cap',
71 array(AAM_Shared_Manager::getInstance(), 'userHasCap'),
72 999,
73 3
74 );
75
76 AAM_Backend_Authorization::bootstrap(); //bootstrap backend authorization
77 }
78
79 /**
80 * Filter the Admin Menu
81 *
82 * @param string $parent_file
83 *
84 * @return string
85 *
86 * @access public
87 */
88 public function filterMenu($parent_file) {
89 //filter admin menu
90 AAM::getUser()->getObject('menu')->filter();
91
92 return $parent_file;
93 }
94
95 /**
96 * Handle metabox initialization process
97 *
98 * @return void
99 *
100 * @access public
101 */
102 public function metaboxes() {
103 global $post;
104
105 //make sure that nobody is playing with screen options
106 if (is_a($post, 'WP_Post')) {
107 $screen = $post->post_type;
108 } elseif ($screen_object = get_current_screen()) {
109 $screen = $screen_object->id;
110 } else {
111 $screen = '';
112 }
113
114 if (AAM_Core_Request::get('init') != 'metabox') {
115 if ($screen != 'widgets') {
116 AAM::getUser()->getObject('metabox')->filterBackend($screen);
117 } else {
118 AAM::getUser()->getObject('metabox')->filterAppearanceWidgets();
119 }
120 }
121 }
122
123 /**
124 * Manage notifications visibility
125 *
126 * @return void
127 *
128 * @access public
129 */
130 public function adminNotices() {
131 if (AAM_Core_API::capabilityExists('show_admin_notices')) {
132 if (!AAM::getUser()->hasCapability('show_admin_notices')) {
133 remove_all_actions('admin_notices');
134 remove_all_actions('network_admin_notices');
135 remove_all_actions('user_admin_notices');
136 }
137 }
138 }
139
140 /**
141 * Filter top admin bar
142 *
143 * The filter will be performed based on the Backend Menu access settings
144 *
145 * @return void
146 *
147 * @access public
148 * @global WP_Admin_Bar $wp_admin_bar
149 */
150 public function filterAdminBar() {
151 global $wp_admin_bar;
152
153 $menu = AAM::getUser()->getObject('menu');
154 foreach($wp_admin_bar->get_nodes() as $id => $node) {
155 if (!empty($node->href)) {
156 $suffix = str_replace(admin_url(), '', $node->href);
157 if ($menu->has($suffix, true)) {
158 if (empty($node->parent) && $this->hasChildren($id)) { //root level
159 $node->href = '#';
160 $wp_admin_bar->add_node($node);
161 } else {
162 $wp_admin_bar->remove_menu($id);
163 }
164 }
165 }
166 }
167 }
168
169 /**
170 * Check if specified top bar item has children
171 *
172 * @param string $id
173 *
174 * @return boolean
175 *
176 * @access protected
177 * @global WP_Admin_Bar $wp_admin_bar
178 */
179 protected function hasChildren($id) {
180 global $wp_admin_bar;
181
182 $has = false;
183
184 foreach($wp_admin_bar->get_nodes() as $node) {
185 if ($node->parent == $id) {
186 $has = true;
187 break;
188 }
189 }
190
191 return $has;
192 }
193
194 /**
195 * Post Quick Menu Actions Filtering
196 *
197 * @param array $actions
198 * @param WP_Post $post
199 *
200 * @return array
201 *
202 * @access public
203 */
204 public function postRowActions($actions, $post) {
205 $object = AAM::getUser()->getObject('post', $post->ID, $post);
206
207 //filter edit menu
208 if (!$this->isAllowed('backend.edit', $object)) {
209 if (isset($actions['edit'])) {
210 unset($actions['edit']);
211 }
212 if (isset($actions['inline hide-if-no-js'])) {
213 unset($actions['inline hide-if-no-js']);
214 }
215 }
216
217 //filter delete menu
218 if (!$this->isAllowed('backend.delete', $object)) {
219 if (isset($actions['trash'])) { unset($actions['trash']); }
220 if (isset($actions['delete'])) { unset($actions['delete']); }
221 }
222
223 //filter edit menu
224 if (!$this->isAllowed('backend.publish', $object)) {
225 if (isset($actions['inline hide-if-no-js'])) {
226 unset($actions['inline hide-if-no-js']);
227 }
228 }
229
230 return $actions;
231 }
232
233 /**
234 * Check if action is allowed
235 *
236 * This method will take in consideration also *_others action
237 *
238 * @param string $action
239 * @param AAM_Core_Object_Post $object
240 *
241 * @return boolean
242 *
243 * @access protected
244 */
245 protected function isAllowed($action, $object) {
246 $edit = $object->has($action);
247 $others = $object->has("{$action}_others");
248 $author = ($object->post_author == get_current_user_id());
249
250 return ($edit || ($others && !$author)) ? false : true;
251 }
252
253 /**
254 * Override default category if defined
255 *
256 * @param type $category
257 *
258 * @return int
259 *
260 * @access public
261 * @staticvar type $default
262 */
263 public function filterDefaultCategory($category) {
264 static $default = null;
265
266 if (is_null($default)) {
267 //check if user category is defined
268 $id = get_current_user_id();
269 $default = AAM_Core_Config::get('feature.post.defaultTerm.user.' . $id , null);
270 $roles = AAM::getUser()->roles;
271
272 if (is_null($default) && count($roles)) {
273 $default = AAM_Core_Config::get(
274 'feature.post.defaultTerm.role.' . array_shift($roles), false
275 );
276 }
277 }
278
279 return ($default ? $default : $category);
280 }
281
282 /**
283 * Post update hook
284 *
285 * Clear cache if post owner changed
286 *
287 * @param int $id
288 * @param array $data
289 *
290 * @return void
291 *
292 * @access public
293 */
294 public function prePostUpdate($id, $data) {
295 $post = get_post($id);
296
297 if ($post->post_author != $data['post_author']) {
298 AAM_Core_API::clearCache();
299 }
300 }
301
302 /**
303 * Filter roles
304 *
305 * @param array $roles
306 *
307 * @return array
308 */
309 public function filterRoles($roles) {
310 $userLevel = AAM_Core_API::maxLevel(AAM::getUser()->allcaps);
311
312 //filter roles
313 foreach($roles as $id => $role) {
314 if (!empty($role['capabilities']) && is_array($role['capabilities'])) {
315 $roleLevel = AAM_Core_API::maxLevel($role['capabilities']);
316 if ($userLevel < $roleLevel) {
317 unset($roles[$id]);
318 } elseif ($userLevel == $roleLevel && $this->filterSameLevel()) {
319 unset($roles[$id]);
320 }
321 }
322 }
323
324 return $roles;
325 }
326
327 /**
328 *
329 * @return type
330 */
331 protected function filterSameLevel() {
332 $response = false;
333
334 if (AAM_Core_API::capabilityExists('manage_same_user_level')) {
335 $response = !AAM::getUser()->hasCapability('manage_same_user_level');
336 }
337
338 return $response;
339 }
340
341 /**
342 * Filter user query
343 *
344 * Exclude all users that have higher user level
345 *
346 * @param object $query
347 *
348 * @access public
349 *
350 * @return void
351 */
352 public function filterUserQuery($query) {
353 //current user max level
354 $max = AAM_Core_API::maxLevel(AAM::getUser()->allcaps);
355 $exclude = array();
356 $roles = AAM_Core_API::getRoles();
357
358 foreach($roles->role_objects as $id => $role) {
359 $roleMax = AAM_Core_API::maxLevel($role->capabilities);
360 if ($roleMax > $max ) {
361 $exclude[] = $id;
362 } elseif ($roleMax == $max && $this->filterSameLevel()) {
363 $exclude[] = $id;
364 }
365 }
366
367 $query->query_vars['role__not_in'] = $exclude;
368 }
369
370 /**
371 * Filter user list view options
372 *
373 * @param array $views
374 *
375 * @return array
376 *
377 * @access public
378 */
379 public function filterViews($views) {
380 $max = AAM_Core_API::maxLevel(AAM::getUser()->allcaps);
381 $roles = AAM_Core_API::getRoles();
382
383 foreach($roles->role_objects as $id => $role) {
384 $roleMax = AAM_Core_API::maxLevel($role->capabilities);
385 if (isset($views[$id])) {
386 if ($roleMax > $max) {
387 unset($views[$id]);
388 } elseif ($roleMax == $max && $this->filterSameLevel()) {
389 unset($views[$id]);
390 }
391 }
392 }
393
394 return $views;
395 }
396
397 /**
398 * Register backend filters and actions
399 *
400 * @return void
401 *
402 * @access public
403 */
404 public static function register() {
405 if (is_null(self::$_instance)) {
406 self::$_instance = new self;
407 }
408 }
409
410 }