PluginProbe ʕ •ᴥ•ʔ
Advanced Access Manager – Access Governance for WordPress / 5.3
Advanced Access Manager – Access Governance for WordPress v5.3
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 / Feature / Main / Capability.php
advanced-access-manager / Application / Backend / Feature / Main Last commit date
404Redirect.php 8 years ago Capability.php 8 years ago LoginRedirect.php 8 years ago LogoutRedirect.php 8 years ago Menu.php 8 years ago Metabox.php 8 years ago Post.php 8 years ago Redirect.php 8 years ago Route.php 8 years ago
Capability.php
310 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 capability manager
12 *
13 * @package AAM
14 * @author Vasyl Martyniuk <vasyl@vasyltech.com>
15 */
16 class AAM_Backend_Feature_Main_Capability extends AAM_Backend_Feature_Abstract {
17
18 /**
19 * Capability groups
20 *
21 * @var array
22 *
23 * @access private
24 */
25 public static $groups = array(
26 'system' => array(
27 'level_0', 'level_1', 'level_2', 'level_3', 'level_4', 'level_5',
28 'level_6', 'level_7', 'level_8', 'level_9', 'level_10'
29 ),
30 'post' => array(
31 'delete_others_pages', 'delete_others_posts', 'edit_others_pages',
32 'delete_posts', 'delete_private_pages', 'delete_private_posts',
33 'delete_published_pages', 'delete_published_posts', 'delete_pages',
34 'edit_others_posts', 'edit_pages', 'edit_private_posts',
35 'edit_private_pages', 'edit_posts', 'edit_published_pages',
36 'edit_published_posts', 'publish_pages', 'publish_posts', 'read',
37 'read_private_pages', 'read_private_posts', 'edit_permalink'
38 ),
39 'backend' => array(
40 'aam_manage', 'activate_plugins', 'add_users', 'update_plugins',
41 'delete_users', 'delete_themes', 'edit_dashboard', 'edit_files',
42 'edit_plugins', 'edit_theme_options', 'edit_themes', 'edit_users',
43 'export', 'import', 'install_plugins', 'install_themes',
44 'manage_options', 'manage_links', 'manage_categories', 'customize',
45 'unfiltered_html', 'unfiltered_upload', 'update_themes',
46 'update_core', 'upload_files', 'delete_plugins', 'remove_users',
47 'switch_themes', 'list_users', 'promote_users', 'create_users', 'delete_site'
48 ),
49 'aam' => array(
50 'aam_manage_admin_menu', 'aam_manage_metaboxes', 'aam_manage_capabilities',
51 'aam_manage_posts', 'aam_manage_access_denied_redirect', 'aam_create_roles',
52 'aam_manage_login_redirect', 'aam_manage_logout_redirect', 'aam_manager',
53 'aam_manage_settings', 'aam_manage_extensions', 'aam_show_notifications',
54 'aam_manage_404_redirect', 'aam_manage_ip_check',
55 'aam_manage_default', 'aam_manage_visitors', 'aam_list_roles',
56 'aam_edit_roles', 'aam_delete_roles', 'aam_toggle_users', 'aam_switch_users',
57 'aam_manage_configpress', 'aam_manage_api_routes'
58 )
59 );
60
61 /**
62 *
63 * @return type
64 */
65 public function getTable() {
66 $response = array('data' => $this->retrieveAllCaps());
67
68 return json_encode($response);
69 }
70
71 /**
72 * Update capability tag
73 *
74 * @return string
75 *
76 * @access public
77 */
78 public function update() {
79 $capability = AAM_Core_Request::post('capability');
80 $updated = AAM_Core_Request::post('updated');
81 $roles = AAM_Core_API::getRoles();
82
83 if (AAM_Core_API::capabilityExists($updated) === false) {
84 foreach($roles->role_objects as $role) {
85 //check if capability is present for current role! Note, we
86 //can not use the native WP_Role::has_cap function because it will
87 //return false if capability exists but not checked
88 if (is_array($role->capabilities)
89 && array_key_exists($capability, $role->capabilities)) {
90 $role->add_cap($updated, $role->capabilities[$capability]);
91 $role->remove_cap($capability);
92 }
93 }
94 $response = array('status' => 'success');
95 } else {
96 $response = array(
97 'status' => 'failure',
98 'message' => __('Capability already exists', AAM_KEY)
99 );
100 }
101
102 return json_encode($response);
103 }
104
105 /**
106 * Delete capability
107 *
108 * This function delete capability in all roles.
109 *
110 * @return string
111 *
112 * @access public
113 */
114 public function delete() {
115 $capability = AAM_Core_Request::post('capability');
116 $roles = AAM_Core_API::getRoles();
117 $subject = AAM_Backend_Subject::getInstance();
118
119 if ($subject->getUID() == AAM_Core_Subject_Role::UID) {
120 foreach($roles->role_objects as $role) {
121 $role->remove_cap($capability);
122 }
123 $response = array('status' => 'success');
124 } else {
125 $response = array(
126 'status' => 'failure',
127 'message' => __('Can not remove the capability', AAM_KEY)
128 );
129 }
130
131 return json_encode($response);
132 }
133
134 /**
135 * @inheritdoc
136 */
137 public static function getTemplate() {
138 return 'main/capability.phtml';
139 }
140
141 /**
142 *
143 * @param type $cap
144 * @return type
145 */
146 protected function prepareActionList($cap) {
147 $subject = AAM_Backend_Subject::getInstance();
148 $actions = array();
149
150 $actions[] = ($subject->hasCapability($cap) ? 'checked' : 'unchecked');
151
152 //allow to delete or update capability only for roles!
153 if (AAM_Core_Config::get('manage-capability', false)
154 && ($subject->getUID() == AAM_Core_Subject_Role::UID)) {
155 $actions[] = 'edit';
156 $actions[] = 'delete';
157 }
158
159 return implode(
160 ',', apply_filters('aam-cap-row-actions-filter', $actions, $subject)
161 );
162 }
163
164 /**
165 * Get list of user roles
166 *
167 * @param array $roles
168 *
169 * @return array
170 *
171 * @access protected
172 */
173 protected function getUserRoles($roles) {
174 $response = array();
175
176 $names = AAM_Core_API::getRoles()->get_names();
177
178 if (is_array($roles)) {
179 foreach($roles as $role) {
180 if (is_array($names) && array_key_exists($role, $names)) {
181 $response[] = translate_user_role($names[$role]);
182 }
183 }
184 }
185
186 return $response;
187 }
188
189 /**
190 *
191 * @return type
192 */
193 protected function retrieveAllCaps() {
194 $response = array();
195 $caps = AAM_Core_API::getAllCapabilities();
196
197 foreach (array_keys($caps) as $cap) {
198 $response[] = array(
199 $cap,
200 $this->getGroup($cap),
201 $cap,
202 $this->prepareActionList($cap)
203 );
204 }
205
206 return $response;
207 }
208
209 /**
210 * Get capability group list
211 *
212 * @return array
213 *
214 * @access public
215 */
216 public function getGroupList() {
217 return apply_filters('aam-capability-groups-filter', array(
218 __('System', AAM_KEY),
219 __('Posts & Pages', AAM_KEY),
220 __('Backend', AAM_KEY),
221 __('AAM Interface', AAM_KEY),
222 __('Miscellaneous', AAM_KEY)
223 ));
224 }
225
226 /**
227 * Add new capability
228 *
229 * @return string
230 *
231 * @access public
232 */
233 public function add() {
234 $capability = sanitize_text_field(AAM_Core_Request::post('capability'));
235
236 if ($capability) {
237 //add the capability to administrator's role as default behavior
238 AAM_Core_API::getRoles()->add_cap('administrator', $capability);
239 AAM_Backend_Subject::getInstance()->addCapability($capability);
240 $response = array('status' => 'success');
241 } else {
242 $response = array('status' => 'failure');
243 }
244
245 return json_encode($response);
246 }
247
248 /**
249 * Get capability group name
250 *
251 * @param string $capability
252 *
253 * @return string
254 *
255 * @access protected
256 */
257 protected function getGroup($capability) {
258 if (in_array($capability, self::$groups['system'])) {
259 $response = __('System', AAM_KEY);
260 } elseif (in_array($capability, self::$groups['post'])) {
261 $response = __('Posts & Pages', AAM_KEY);
262 } elseif (in_array($capability, self::$groups['backend'])) {
263 $response = __('Backend', AAM_KEY);
264 } elseif (in_array($capability, self::$groups['aam'])) {
265 $response = __('AAM Interface', AAM_KEY);
266 } else {
267 $response = __('Miscellaneous', AAM_KEY);
268 }
269
270 return apply_filters(
271 'aam-capability-group-filter', $response, $capability
272 );
273 }
274
275 /**
276 * Check overwritten status
277 *
278 * @return boolean
279 *
280 * @access protected
281 */
282 protected function isOverwritten() {
283 $object = AAM_Backend_Subject::getInstance()->getObject('capability');
284
285 return $object->isOverwritten();
286 }
287
288 /**
289 * Register capability feature
290 *
291 * @return void
292 *
293 * @access public
294 */
295 public static function register() {
296 AAM_Backend_Feature::registerFeature((object) array(
297 'uid' => 'capability',
298 'position' => 15,
299 'title' => __('Capabilities', AAM_KEY),
300 'capability' => 'aam_manage_capabilities',
301 'type' => 'main',
302 'subjects' => array(
303 AAM_Core_Subject_Role::UID,
304 AAM_Core_Subject_User::UID
305 ),
306 'view' => __CLASS__
307 ));
308 }
309
310 }