PluginProbe ʕ •ᴥ•ʔ
Advanced Access Manager – Access Governance for WordPress / 5.8.2
Advanced Access Manager – Access Governance for WordPress v5.8.2
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 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
325 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 //default category filder
54 // TODO - THIS HAS TO GO TO THE PLUS PACKAGE EXTENSION
55 add_filter('pre_option_default_category', array($this, 'filterDefaultCategory'));
56
57 add_action('pre_post_update', array($this, 'prePostUpdate'), 10, 2);
58
59 //user/role filters
60 if (!is_multisite() || !is_super_admin()) {
61 add_filter('editable_roles', array($this, 'filterRoles'));
62 add_action('pre_get_users', array($this, 'filterUserQuery'), 999);
63 add_filter('views_users', array($this, 'filterViews'));
64 }
65
66 AAM_Backend_Authorization::bootstrap(); //bootstrap backend authorization
67 }
68
69 /**
70 * Filter the Admin Menu
71 *
72 * @param string $parent_file
73 *
74 * @return string
75 *
76 * @access public
77 */
78 public function filterMenu($parent_file) {
79 //filter admin menu
80 AAM::getUser()->getObject('menu')->filter();
81
82 return $parent_file;
83 }
84
85 /**
86 * Handle metabox initialization process
87 *
88 * @return void
89 *
90 * @access public
91 */
92 public function metaboxes() {
93 global $post;
94
95 //make sure that nobody is playing with screen options
96 if (is_a($post, 'WP_Post')) {
97 $screen = $post->post_type;
98 } else {
99 $screen_object = get_current_screen();
100 $screen = ($screen_object ? $screen_object->id : '');
101 }
102
103 if (AAM_Core_Request::get('init') !== 'metabox') {
104 if ($screen !== 'widgets') {
105 AAM::getUser()->getObject('metabox')->filterBackend($screen);
106 } else {
107 AAM::getUser()->getObject('metabox')->filterAppearanceWidgets();
108 }
109 }
110 }
111
112 /**
113 * Manage notifications visibility
114 *
115 * @return void
116 *
117 * @access public
118 */
119 public function adminNotices() {
120 if (AAM_Core_API::capabilityExists('show_admin_notices')) {
121 if (!AAM::getUser()->hasCapability('show_admin_notices')) {
122 remove_all_actions('admin_notices');
123 remove_all_actions('network_admin_notices');
124 remove_all_actions('user_admin_notices');
125 }
126 }
127 }
128
129 /**
130 * Post Quick Menu Actions Filtering
131 *
132 * @param array $actions
133 * @param WP_Post $post
134 *
135 * @return array
136 *
137 * @access public
138 */
139 public function postRowActions($actions, $post) {
140 $object = AAM::getUser()->getObject('post', $post->ID, $post);
141
142 //filter edit menu
143 if (!$object->allowed('backend.edit')) {
144 if (isset($actions['edit'])) {
145 unset($actions['edit']);
146 }
147 if (isset($actions['inline hide-if-no-js'])) {
148 unset($actions['inline hide-if-no-js']);
149 }
150 }
151
152 //filter delete menu
153 if (!$object->allowed('backend.delete')) {
154 if (isset($actions['trash'])) { unset($actions['trash']); }
155 if (isset($actions['delete'])) { unset($actions['delete']); }
156 }
157
158 //filter edit menu
159 if (!$object->allowed('backend.publish')) {
160 if (isset($actions['inline hide-if-no-js'])) {
161 unset($actions['inline hide-if-no-js']);
162 }
163 }
164
165 return $actions;
166 }
167
168 /**
169 * Override default category if defined
170 *
171 * @param type $category
172 *
173 * @return int
174 *
175 * @access public
176 * @staticvar type $default
177 */
178 public function filterDefaultCategory($category) {
179 //check if user category is defined
180 $id = get_current_user_id();
181 $default = AAM_Core_Config::get('feature.post.defaultTerm.user.' . $id , null);
182 $roles = AAM::getUser()->roles;
183
184 if (is_null($default) && count($roles)) {
185 $default = AAM_Core_Config::get(
186 'feature.post.defaultTerm.role.' . array_shift($roles), false
187 );
188 }
189
190 return ($default ? $default : $category);
191 }
192
193 /**
194 * Post update hook
195 *
196 * Clear cache if post owner changed
197 *
198 * @param int $id
199 * @param array $data
200 *
201 * @return void
202 *
203 * @access public
204 */
205 public function prePostUpdate($id, $data) {
206 $post = get_post($id);
207
208 if (intval($post->post_author) !== intval($data['post_author'])) {
209 AAM_Core_API::clearCache();
210 }
211 }
212
213 /**
214 * Filter roles
215 *
216 * @param array $roles
217 *
218 * @return array
219 */
220 public function filterRoles($roles) {
221 static $levels = array(); // to speed-up the execution
222
223 $userLevel = AAM::getUser()->getMaxLevel();
224
225 //filter roles
226 foreach($roles as $id => $role) {
227 if (!empty($role['capabilities']) && is_array($role['capabilities'])) {
228 if (!isset($levels[$id])) {
229 $levels[$id] = AAM_Core_API::maxLevel($role['capabilities']);
230 }
231 if ($userLevel < $levels[$id]) {
232 unset($roles[$id]);
233 } elseif ($userLevel === $levels[$id] && $this->filterSameLevel()) {
234 unset($roles[$id]);
235 }
236 }
237 }
238
239 return $roles;
240 }
241
242 /**
243 *
244 * @return type
245 */
246 protected function filterSameLevel() {
247 $response = false;
248
249 if (AAM_Core_API::capabilityExists('manage_same_user_level')) {
250 $response = !AAM::getUser()->hasCapability('manage_same_user_level');
251 }
252
253 return $response;
254 }
255
256 /**
257 * Filter user query
258 *
259 * Exclude all users that have higher user level
260 *
261 * @param object $query
262 *
263 * @access public
264 *
265 * @return void
266 */
267 public function filterUserQuery($query) {
268 //current user max level
269 $max = AAM::getUser()->getMaxLevel();
270 $exclude = array();
271 $roles = AAM_Core_API::getRoles();
272
273 foreach($roles->role_objects as $id => $role) {
274 $roleMax = AAM_Core_API::maxLevel($role->capabilities);
275 if ($roleMax > $max ) {
276 $exclude[] = $id;
277 } elseif ($roleMax === $max && $this->filterSameLevel()) {
278 $exclude[] = $id;
279 }
280 }
281
282 $query->query_vars['role__not_in'] = $exclude;
283 }
284
285 /**
286 * Filter user list view options
287 *
288 * @param array $views
289 *
290 * @return array
291 *
292 * @access public
293 */
294 public function filterViews($views) {
295 $max = AAM::getUser()->getMaxLevel();
296 $roles = AAM_Core_API::getRoles();
297
298 foreach($roles->role_objects as $id => $role) {
299 $roleMax = AAM_Core_API::maxLevel($role->capabilities);
300 if (isset($views[$id])) {
301 if ($roleMax > $max) {
302 unset($views[$id]);
303 } elseif ($roleMax === $max && $this->filterSameLevel()) {
304 unset($views[$id]);
305 }
306 }
307 }
308
309 return $views;
310 }
311
312 /**
313 * Register backend filters and actions
314 *
315 * @return void
316 *
317 * @access public
318 */
319 public static function register() {
320 if (is_null(self::$_instance)) {
321 self::$_instance = new self;
322 }
323 }
324
325 }