PluginProbe ʕ •ᴥ•ʔ
Advanced Access Manager – Access Governance for WordPress / 5.9
Advanced Access Manager – Access Governance for WordPress v5.9
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
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 }