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 | } |