CoreAdmin.php
6 years ago
admin.php
6 years ago
backup-handler.php
6 years ago
backup.php
6 years ago
cap-helper.php
6 years ago
filters-admin.php
6 years ago
filters-woocommerce.php
7 years ago
filters-wp_rest_workarounds.php
6 years ago
filters.php
6 years ago
functions-admin.php
6 years ago
functions.php
6 years ago
handler.php
6 years ago
inflect-cme.php
7 years ago
manager.php
6 years ago
network.php
6 years ago
pp-handler.php
6 years ago
pp-ui.php
6 years ago
publishpress-roles.php
6 years ago
manager.php
681 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Capability Manager. |
| 4 | * Plugin to create and manage roles and capabilities. |
| 5 | * |
| 6 | * @author Jordi Canals, Kevin Behrens |
| 7 | * @copyright Copyright (C) 2009, 2010 Jordi Canals, (C) 2020 PublishPress |
| 8 | * @license GNU General Public License version 2 |
| 9 | * @link https://publishpress.com/ |
| 10 | * |
| 11 | * |
| 12 | * Copyright 2009, 2010 Jordi Canals <devel@jcanals.cat> |
| 13 | * |
| 14 | * Modifications Copyright 2020, PublishPress <help@publishpress.com> |
| 15 | * |
| 16 | * This program is free software; you can redistribute it and/or |
| 17 | * modify it under the terms of the GNU General Public License |
| 18 | * version 2 as published by the Free Software Foundation. |
| 19 | * |
| 20 | * This program is distributed in the hope that it will be useful, |
| 21 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 22 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 23 | * GNU General Public License for more details. |
| 24 | * |
| 25 | * You should have received a copy of the GNU General Public License |
| 26 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 27 | */ |
| 28 | |
| 29 | add_action( 'init', 'cme_update_pp_usage' ); // update early so resulting post type cap changes are applied for this request's UI construction |
| 30 | |
| 31 | function cme_update_pp_usage() { |
| 32 | if ( ! empty($_REQUEST['update_filtered_types']) || ! empty($_REQUEST['update_filtered_taxonomies']) || ! empty($_REQUEST['update_detailed_taxonomies']) || ! empty($_REQUEST['SaveRole']) ) { |
| 33 | require_once( dirname(__FILE__).'/pp-handler.php' ); |
| 34 | return _cme_update_pp_usage(); |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | // Core WP roles to apply safeguard preventing accidental lockout from dashboard |
| 39 | function _cme_core_roles() { |
| 40 | return apply_filters( 'pp_caps_core_roles', array( 'administrator', 'editor', 'revisor', 'author', 'contributor', 'subscriber' ) ); |
| 41 | } |
| 42 | |
| 43 | function _cme_core_caps() { |
| 44 | $core_caps = array_fill_keys( array( 'switch_themes', 'edit_themes', 'activate_plugins', 'edit_plugins', 'edit_users', 'edit_files', 'manage_options', 'moderate_comments', |
| 45 | 'manage_links', 'upload_files', 'import', 'unfiltered_html', 'read', 'delete_users', 'create_users', 'unfiltered_upload', 'edit_dashboard', |
| 46 | 'update_plugins', 'delete_plugins', 'install_plugins', 'update_themes', 'install_themes', |
| 47 | 'update_core', 'list_users', 'remove_users', 'promote_users', 'edit_theme_options', 'delete_themes', 'export' ), true ); |
| 48 | |
| 49 | ksort( $core_caps ); |
| 50 | return $core_caps; |
| 51 | } |
| 52 | |
| 53 | function _cme_is_read_removal_blocked( $role_name ) { |
| 54 | $role = get_role($role_name); |
| 55 | $rcaps = $role->capabilities; |
| 56 | |
| 57 | $core_caps = array_diff_key( _cme_core_caps(), array_fill_keys( array( 'unfiltered_html', 'unfiltered_upload', 'upload_files', 'edit_files', 'read' ), true ) ); |
| 58 | |
| 59 | if ( empty( $rcaps['dashboard_lockout_ok'] ) ) { |
| 60 | $edit_caps = array(); |
| 61 | foreach ( get_post_types( array( 'public' => true ), 'object' ) as $type_obj ) { |
| 62 | $edit_caps = array_merge( $edit_caps, array_values( array_diff_key( (array) $type_obj->cap, array( 'read_private_posts' => true ) ) ) ); |
| 63 | } |
| 64 | |
| 65 | $edit_caps = array_fill_keys( $edit_caps, true ); |
| 66 | unset( $edit_caps['read'] ); |
| 67 | unset( $edit_caps['upload_files'] ); |
| 68 | unset( $edit_caps['edit_files'] ); |
| 69 | |
| 70 | if ( $role_has_admin_caps = in_array( $role_name, _cme_core_roles() ) && ( array_intersect_key( $rcaps, array_diff_key( $core_caps, array( 'read' => true ) ) ) || array_intersect_key( $rcaps, $edit_caps ) ) ) { |
| 71 | return true; |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | return false; |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Class CapabilityManager. |
| 80 | * Sets the main environment for all Capability Manager components. |
| 81 | * |
| 82 | * @author Jordi Canals, Kevin Behrens |
| 83 | * @link https://publishpress.com/ |
| 84 | */ |
| 85 | class CapabilityManager |
| 86 | { |
| 87 | /** |
| 88 | * Array with all capabilities to be managed. (Depends on user caps). |
| 89 | * The array keys are the capability, the value is its screen name. |
| 90 | * @var array |
| 91 | */ |
| 92 | var $capabilities = array(); |
| 93 | |
| 94 | /** |
| 95 | * Array with roles that can be managed. (Depends on user roles). |
| 96 | * The array keys are the role name, the value is its translated name. |
| 97 | * @var array |
| 98 | */ |
| 99 | var $roles = array(); |
| 100 | |
| 101 | /** |
| 102 | * Current role we are managing |
| 103 | * @var string |
| 104 | */ |
| 105 | var $current; |
| 106 | |
| 107 | /** |
| 108 | * Maximum level current manager can assign to a user. |
| 109 | * @var int |
| 110 | */ |
| 111 | private $max_level; |
| 112 | |
| 113 | private $log_db_role_objects = array(); |
| 114 | |
| 115 | var $message; |
| 116 | |
| 117 | /** |
| 118 | * Module ID. Is the module internal short name. |
| 119 | * |
| 120 | * @var string |
| 121 | */ |
| 122 | public $ID; |
| 123 | |
| 124 | public function __construct() |
| 125 | { |
| 126 | $this->ID = 'capsman'; |
| 127 | $this->mod_url = plugins_url( '', CME_FILE ); |
| 128 | |
| 129 | $this->moduleLoad(); |
| 130 | |
| 131 | add_action('admin_menu', array($this, 'adminMenus'), 5); // execute prior to PP, to use menu hook |
| 132 | |
| 133 | // Load styles |
| 134 | add_action('admin_print_styles', array($this, 'adminStyles')); |
| 135 | |
| 136 | if ( isset($_REQUEST['page']) && ( 'capsman' == $_REQUEST['page'] ) ) { |
| 137 | add_action('admin_enqueue_scripts', array($this, 'adminScriptsPP')); |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * Enqueues administration styles. |
| 143 | * |
| 144 | * @hook action 'admin_print_styles' |
| 145 | * |
| 146 | * @return void |
| 147 | */ |
| 148 | function adminStyles() |
| 149 | { |
| 150 | if ( empty( $_REQUEST['page'] ) || ! in_array( $_REQUEST['page'], array( 'capsman', 'capsman-tool' ) ) ) |
| 151 | return; |
| 152 | |
| 153 | wp_enqueue_style('cme-admin-common', $this->mod_url . '/common/css/pressshack-admin.css', [], PUBLISHPRESS_CAPS_VERSION); |
| 154 | |
| 155 | wp_register_style( $this->ID . 'framework_admin', $this->mod_url . '/framework/styles/admin.css', false, PUBLISHPRESS_CAPS_VERSION); |
| 156 | wp_enqueue_style( $this->ID . 'framework_admin'); |
| 157 | |
| 158 | wp_register_style( $this->ID . '_admin', $this->mod_url . '/admin.css', false, PUBLISHPRESS_CAPS_VERSION); |
| 159 | wp_enqueue_style( $this->ID . '_admin'); |
| 160 | |
| 161 | $suffix = defined('SCRIPT_DEBUG') && SCRIPT_DEBUG ? '.dev' : ''; |
| 162 | $url = $this->mod_url . "/admin{$suffix}.js"; |
| 163 | wp_enqueue_script( 'cme_admin', $url, array('jquery'), PUBLISHPRESS_CAPS_VERSION, true ); |
| 164 | wp_localize_script( 'cme_admin', 'cmeAdmin', array( |
| 165 | 'negationCaption' => __( 'Explicity negate this capability by storing as disabled', 'capsman-enhanced' ), |
| 166 | 'typeCapsNegationCaption' => __( 'Explicitly negate these capabilities by storing as disabled', 'capsman-enhanced' ), |
| 167 | 'typeCapUnregistered' => __( 'Post type registration does not define this capability distinctly', 'capsman-enhanced' ), |
| 168 | 'capNegated' => __( 'This capability is explicitly negated. Click to add/remove normally.', 'capsman-enhanced' ), |
| 169 | 'chkCaption' => __( 'Add or remove this capability from the WordPress role', 'capsman-enhanced' ), |
| 170 | 'switchableCaption' => __( 'Add or remove capability from the role normally', 'capsman-enhanced' ) ) |
| 171 | ); |
| 172 | } |
| 173 | |
| 174 | function adminScriptsPP() { |
| 175 | wp_enqueue_style( 'plugin-install' ); |
| 176 | wp_enqueue_script( 'plugin-install' ); |
| 177 | add_thickbox(); |
| 178 | } |
| 179 | |
| 180 | /** |
| 181 | * Creates some filters at module load time. |
| 182 | * |
| 183 | * @return void |
| 184 | */ |
| 185 | protected function moduleLoad () |
| 186 | { |
| 187 | $old_version = get_option($this->ID . '_version'); |
| 188 | if ( version_compare( $old_version, PUBLISHPRESS_CAPS_VERSION, 'ne') ) { |
| 189 | update_option($this->ID . '_version', PUBLISHPRESS_CAPS_VERSION); |
| 190 | $this->pluginUpdate(); |
| 191 | } |
| 192 | |
| 193 | // Only roles that a user can administer can be assigned to others. |
| 194 | add_filter('editable_roles', array($this, 'filterEditRoles')); |
| 195 | |
| 196 | // Users with roles that cannot be managed, are not allowed to be edited. |
| 197 | add_filter('map_meta_cap', array(&$this, 'filterUserEdit'), 10, 4); |
| 198 | |
| 199 | // ensure storage, retrieval of db-stored customizations to dynamic roles |
| 200 | if ( isset($_REQUEST['page']) && in_array( $_REQUEST['page'], array( 'capsman', 'capsman-tool' ) ) ) { |
| 201 | global $wpdb; |
| 202 | $role_key = $wpdb->prefix . 'user_roles'; |
| 203 | $this->log_db_roles(); |
| 204 | add_filter( 'option_' . $role_key, array( &$this, 'reinstate_db_roles' ), PHP_INT_MAX ); |
| 205 | } |
| 206 | |
| 207 | add_filter( 'plugins_loaded', array( &$this, 'processRoleUpdate' ) ); |
| 208 | } |
| 209 | |
| 210 | // Direct query of stored role definitions |
| 211 | function log_db_roles( $legacy_arg = '' ) { |
| 212 | global $wpdb; |
| 213 | |
| 214 | $results = (array) maybe_unserialize( $wpdb->get_var("SELECT option_value FROM $wpdb->options WHERE option_name = '{$wpdb->prefix}user_roles' LIMIT 1") ); |
| 215 | foreach( $results as $_role_name => $_role ) { |
| 216 | $this->log_db_role_objects[$_role_name] = (object) $_role; |
| 217 | } |
| 218 | |
| 219 | return $legacy_arg; |
| 220 | } |
| 221 | |
| 222 | // note: this is only applied when accessing the cme role edit form |
| 223 | function reinstate_db_roles( $passthru_roles = array() ) { |
| 224 | global $wp_roles; |
| 225 | |
| 226 | if ( isset($wp_roles) && $this->log_db_role_objects ) { |
| 227 | $intersect = array_intersect_key( $wp_roles->role_objects, $this->log_db_role_objects ); |
| 228 | foreach( array_keys( $intersect ) as $key ) { |
| 229 | if ( ! empty( $this->log_db_role_objects[$key]->capabilities ) ) |
| 230 | $wp_roles->role_objects[$key]->capabilities = $this->log_db_role_objects[$key]->capabilities; |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | return $passthru_roles; |
| 235 | } |
| 236 | |
| 237 | /** |
| 238 | * Updates Capability Manager to a new version |
| 239 | * |
| 240 | * @return void |
| 241 | */ |
| 242 | protected function pluginUpdate () |
| 243 | { |
| 244 | global $wpdb; |
| 245 | |
| 246 | $backup = get_option($this->ID . '_backup'); |
| 247 | if ( false === $backup ) { // No previous backup found. Save it! |
| 248 | global $wpdb; |
| 249 | $roles = get_option($wpdb->prefix . 'user_roles'); |
| 250 | update_option( $this->ID . '_backup', $roles, false ); |
| 251 | update_option( $this->ID . '_backup_datestamp', current_time( 'timestamp' ), false ); |
| 252 | } |
| 253 | |
| 254 | if (!$wpdb->get_var("SELECT COUNT(option_id) FROM $wpdb->options WHERE option_name LIKE 'cme_backup_auto_%'")) { |
| 255 | pp_capabilities_autobackup(); |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | /** |
| 260 | * Adds admin panel menus. (At plugins loading time. This is before plugins_loaded). |
| 261 | * User needs to have 'manage_capabilities' to access this menus. |
| 262 | * This is set as an action in the parent class constructor. |
| 263 | * |
| 264 | * @hook action admin_menu |
| 265 | * @return void |
| 266 | */ |
| 267 | public function adminMenus () |
| 268 | { |
| 269 | // First we check if user is administrator and can 'manage_capabilities'. |
| 270 | if ( current_user_can('administrator') && ! current_user_can('manage_capabilities') ) { |
| 271 | $this->setAdminCapability(); |
| 272 | } |
| 273 | |
| 274 | add_action( 'admin_menu', array( &$this, 'cme_menu' ), 18 ); |
| 275 | } |
| 276 | |
| 277 | public function cme_menu() { |
| 278 | $cap_name = (is_multisite() && is_super_admin()) ? 'read' : 'manage_capabilities'; |
| 279 | |
| 280 | $permissions_title = __('Capabilities', 'capsman-enhanced'); |
| 281 | |
| 282 | $menu_order = 72; |
| 283 | |
| 284 | if (defined('PUBLISHPRESS_PERMISSIONS_MENU_GROUPING')) { |
| 285 | foreach (get_option('active_plugins') as $plugin_file) { |
| 286 | if ( false !== strpos($plugin_file, 'publishpress.php') ) { |
| 287 | $menu_order = 27; |
| 288 | } |
| 289 | } |
| 290 | } |
| 291 | |
| 292 | add_menu_page( |
| 293 | $permissions_title, |
| 294 | $permissions_title, |
| 295 | $cap_name, |
| 296 | 'capsman', |
| 297 | array($this, 'generalManager'), |
| 298 | 'dashicons-admin-network', |
| 299 | $menu_order |
| 300 | ); |
| 301 | |
| 302 | add_submenu_page('capsman', __('Backup', 'capsman-enhanced'), __('Backup', 'capsman-enhanced'), $cap_name, $this->ID . '-tool', array($this, 'backupTool')); |
| 303 | |
| 304 | if (!defined('PUBLISHPRESS_CAPS_PRO_VERSION')) { |
| 305 | add_submenu_page( |
| 306 | 'capsman', |
| 307 | __('Upgrade to Pro', 'capsman-enhanced'), |
| 308 | __('Upgrade to Pro', 'capsman-enhanced'), |
| 309 | 'manage_capabilities', |
| 310 | 'capabilities-pro', |
| 311 | array($this, 'generalManager') |
| 312 | ); |
| 313 | } |
| 314 | } |
| 315 | |
| 316 | /** |
| 317 | * Sets the 'manage_capabilities' cap to the administrator role. |
| 318 | * |
| 319 | * @return void |
| 320 | */ |
| 321 | public function setAdminCapability () |
| 322 | { |
| 323 | $admin = get_role('administrator'); |
| 324 | $admin->add_cap('manage_capabilities'); |
| 325 | } |
| 326 | |
| 327 | /** |
| 328 | * Filters roles that can be shown in roles list. |
| 329 | * This is mainly used to prevent an user admin to create other users with |
| 330 | * higher capabilities. |
| 331 | * |
| 332 | * @hook 'editable_roles' filter. |
| 333 | * |
| 334 | * @param $roles List of roles to check. |
| 335 | * @return array Restircted roles list |
| 336 | */ |
| 337 | function filterEditRoles ( $roles ) |
| 338 | { |
| 339 | $this->generateNames(); |
| 340 | $valid = array_keys($this->roles); |
| 341 | |
| 342 | foreach ( $roles as $role => $caps ) { |
| 343 | if ( ! in_array($role, $valid) ) { |
| 344 | unset($roles[$role]); |
| 345 | } |
| 346 | } |
| 347 | |
| 348 | return $roles; |
| 349 | } |
| 350 | |
| 351 | /** |
| 352 | * Checks if a user can be edited or not by current administrator. |
| 353 | * Returns array('do_not_allow') if user cannot be edited. |
| 354 | * |
| 355 | * @hook 'map_meta_cap' filter |
| 356 | * |
| 357 | * @param array $caps Current user capabilities |
| 358 | * @param string $cap Capability to check |
| 359 | * @param int $user_id Current user ID |
| 360 | * @param array $args For our purpose, we receive edited user id at $args[0] |
| 361 | * @return array Allowed capabilities. |
| 362 | */ |
| 363 | function filterUserEdit ( $caps, $cap, $user_id, $args ) |
| 364 | { |
| 365 | if ( ! in_array( $cap, array( 'edit_user', 'delete_user', 'promote_user', 'remove_user' ) ) || ( ! isset($args[0]) ) || $user_id == (int) $args[0] ) { |
| 366 | return $caps; |
| 367 | } |
| 368 | |
| 369 | $user = new WP_User( (int) $args[0] ); |
| 370 | |
| 371 | $this->generateNames(); |
| 372 | |
| 373 | if ( defined( 'CME_LEGACY_USER_EDIT_FILTER' ) && CME_LEGACY_USER_EDIT_FILTER ) { |
| 374 | $valid = array_keys($this->roles); |
| 375 | |
| 376 | foreach ( $user->roles as $role ) { |
| 377 | if ( ! in_array($role, $valid) ) { |
| 378 | $caps = array('do_not_allow'); |
| 379 | break; |
| 380 | } |
| 381 | } |
| 382 | } else { |
| 383 | global $wp_roles; |
| 384 | |
| 385 | foreach ( $user->roles as $role ) { |
| 386 | $r = get_role( $role ); |
| 387 | $level = ak_caps2level($r->capabilities); |
| 388 | |
| 389 | if ( ( ! $level ) && ( 'administrator' == $role ) ) |
| 390 | $level = 10; |
| 391 | |
| 392 | if ( $level > $this->max_level ) { |
| 393 | $caps = array('do_not_allow'); |
| 394 | break; |
| 395 | } |
| 396 | } |
| 397 | |
| 398 | } |
| 399 | |
| 400 | return $caps; |
| 401 | } |
| 402 | |
| 403 | function processRoleUpdate() { |
| 404 | if ( 'POST' == $_SERVER['REQUEST_METHOD'] && ( ! empty($_REQUEST['SaveRole']) || ! empty($_REQUEST['AddCap']) ) ) { |
| 405 | if ((!is_multisite() || !is_super_admin()) && !current_user_can('administrator') && !current_user_can('manage_capabilities')) { |
| 406 | // TODO: Implement exceptions. |
| 407 | wp_die('<strong>' .__('You do not have permission to manage capabilities.', 'capsman-enhanced') . '</strong>'); |
| 408 | } |
| 409 | |
| 410 | if ( ! empty($_REQUEST['current']) ) { // don't process role update unless form variable is received |
| 411 | check_admin_referer('capsman-general-manager'); |
| 412 | |
| 413 | $role = get_role($_REQUEST['current']); |
| 414 | $current_level = ($role) ? ak_caps2level($role->capabilities) : 0; |
| 415 | |
| 416 | $this->processAdminGeneral(); |
| 417 | |
| 418 | $set_level = (isset($_POST['level'])) ? $_POST['level'] : 0; |
| 419 | |
| 420 | if ($set_level != $current_level) { |
| 421 | global $wp_roles, $wp_version; |
| 422 | |
| 423 | if ( version_compare($wp_version, '4.9', '>=') ) { |
| 424 | $wp_roles->for_site(); |
| 425 | } else { |
| 426 | $wp_roles->reinit(); |
| 427 | } |
| 428 | |
| 429 | foreach( get_users(array('role' => $_REQUEST['current'], 'fields' => 'ID')) as $ID ) { |
| 430 | $user = new WP_User($ID); |
| 431 | $user->get_role_caps(); |
| 432 | $user->update_user_level_from_caps(); |
| 433 | } |
| 434 | } |
| 435 | } |
| 436 | } |
| 437 | } |
| 438 | |
| 439 | /** |
| 440 | * Manages global settings admin. |
| 441 | * |
| 442 | * @hook add_submenu_page |
| 443 | * @return void |
| 444 | */ |
| 445 | function generalManager () { |
| 446 | if ((!is_multisite() || !is_super_admin()) && !current_user_can('administrator') && !current_user_can('manage_capabilities')) { |
| 447 | // TODO: Implement exceptions. |
| 448 | wp_die('<strong>' .__('You do not have permission to manage capabilities.', 'capsman-enhanced') . '</strong>'); |
| 449 | } |
| 450 | |
| 451 | if ( 'POST' == $_SERVER['REQUEST_METHOD'] ) { |
| 452 | if ( empty($_REQUEST['SaveRole']) && empty($_REQUEST['AddCap']) ) { |
| 453 | check_admin_referer('capsman-general-manager'); |
| 454 | $this->processAdminGeneral(); |
| 455 | } elseif ( ! empty($_REQUEST['SaveRole']) ) { |
| 456 | ak_admin_notify( $this->message ); // moved update operation to earlier action to avoid UI refresh issues. But outputting notification there breaks styling. |
| 457 | } elseif ( ! empty($_REQUEST['AddCap']) ) { |
| 458 | ak_admin_notify( $this->message ); |
| 459 | } |
| 460 | } else { |
| 461 | if (!empty($_REQUEST['added'])) { |
| 462 | ak_admin_notify(__('New capability added to role.')); |
| 463 | } |
| 464 | } |
| 465 | |
| 466 | $this->generateNames(); |
| 467 | $roles = array_keys($this->roles); |
| 468 | |
| 469 | if ( isset($_GET['action']) && 'delete' == $_GET['action']) { |
| 470 | require_once( dirname(__FILE__).'/handler.php' ); |
| 471 | $capsman_modify = new CapsmanHandler( $this ); |
| 472 | $capsman_modify->adminDeleteRole(); |
| 473 | } |
| 474 | |
| 475 | if ( ! isset($this->current) ) { // By default, we manage the default role |
| 476 | if (empty($_POST) && !empty($_REQUEST['role'])) { |
| 477 | $this->current = $_REQUEST['role']; |
| 478 | } |
| 479 | } |
| 480 | |
| 481 | if (!isset($this->current) || !get_role($this->current)) { |
| 482 | $this->current = get_option('default_role'); |
| 483 | } |
| 484 | |
| 485 | if ( ! in_array($this->current, $roles) ) { // Current role has been deleted. |
| 486 | $this->current = array_shift($roles); |
| 487 | } |
| 488 | |
| 489 | include ( dirname(CME_FILE) . '/includes/admin.php' ); |
| 490 | } |
| 491 | |
| 492 | /** |
| 493 | * Processes and saves the changes in the general capabilities form. |
| 494 | * |
| 495 | * @return void |
| 496 | */ |
| 497 | private function processAdminGeneral () |
| 498 | { |
| 499 | if (! isset($_POST['action']) || 'update' != $_POST['action'] ) { |
| 500 | // TODO: Implement exceptions. This must be a fatal error. |
| 501 | ak_admin_error(__('Bad form Received', 'capsman-enhanced')); |
| 502 | return; |
| 503 | } |
| 504 | |
| 505 | $post = stripslashes_deep($_POST); |
| 506 | if ( empty ($post['caps']) ) { |
| 507 | $post['caps'] = array(); |
| 508 | } |
| 509 | |
| 510 | $this->current = $post['current']; |
| 511 | |
| 512 | // Select a new role. |
| 513 | if ( ! empty($post['LoadRole']) ) { |
| 514 | $this->current = $post['role']; |
| 515 | } else { |
| 516 | require_once( dirname(__FILE__).'/handler.php' ); |
| 517 | $capsman_modify = new CapsmanHandler( $this ); |
| 518 | $capsman_modify->processAdminGeneral( $post ); |
| 519 | } |
| 520 | } |
| 521 | |
| 522 | /** |
| 523 | * Callback function to create names. |
| 524 | * Replaces underscores by spaces and uppercases the first letter. |
| 525 | * |
| 526 | * @access private |
| 527 | * @param string $cap Capability name. |
| 528 | * @return string The generated name. |
| 529 | */ |
| 530 | function _capNamesCB ( $cap ) |
| 531 | { |
| 532 | $cap = str_replace('_', ' ', $cap); |
| 533 | //$cap = ucfirst($cap); |
| 534 | |
| 535 | return $cap; |
| 536 | } |
| 537 | |
| 538 | /** |
| 539 | * Generates an array with the system capability names. |
| 540 | * The key is the capability and the value the created screen name. |
| 541 | * |
| 542 | * @uses self::_capNamesCB() |
| 543 | * @return void |
| 544 | */ |
| 545 | function generateSysNames () |
| 546 | { |
| 547 | $this->max_level = 10; |
| 548 | $this->roles = ak_get_roles(true); |
| 549 | $caps = array(); |
| 550 | |
| 551 | foreach ( array_keys($this->roles) as $role ) { |
| 552 | $role_caps = get_role($role); |
| 553 | $caps = array_merge( $caps, (array) $role_caps->capabilities ); // user reported PHP 5.3.3 error without array cast |
| 554 | } |
| 555 | |
| 556 | $keys = array_keys($caps); |
| 557 | $names = array_map(array($this, '_capNamesCB'), $keys); |
| 558 | $this->capabilities = array_combine($keys, $names); |
| 559 | |
| 560 | asort($this->capabilities); |
| 561 | } |
| 562 | |
| 563 | /** |
| 564 | * Generates an array with the user capability names. |
| 565 | * If user has 'administrator' role, system roles are generated. |
| 566 | * The key is the capability and the value the created screen name. |
| 567 | * A user cannot manage more capabilities that has himself (Except for administrators). |
| 568 | * |
| 569 | * @uses self::_capNamesCB() |
| 570 | * @return void |
| 571 | */ |
| 572 | function generateNames () |
| 573 | { |
| 574 | if ( current_user_can('administrator') || ( is_multisite() && is_super_admin() ) ) { |
| 575 | $this->generateSysNames(); |
| 576 | } else { |
| 577 | global $user_ID; |
| 578 | $user = new WP_User($user_ID); |
| 579 | $this->max_level = ak_caps2level($user->allcaps); |
| 580 | |
| 581 | $keys = array_keys($user->allcaps); |
| 582 | $names = array_map(array($this, '_capNamesCB'), $keys); |
| 583 | |
| 584 | $this->capabilities = ( $keys ) ? array_combine($keys, $names) : array(); |
| 585 | |
| 586 | $roles = ak_get_roles(true); |
| 587 | unset($roles['administrator']); |
| 588 | |
| 589 | if ( ( defined( 'CME_LEGACY_USER_EDIT_FILTER' ) && CME_LEGACY_USER_EDIT_FILTER ) || ( ! empty( $_REQUEST['page'] ) && 'capsman' == $_REQUEST['page'] ) ) { |
| 590 | foreach ( $user->roles as $role ) { // Unset the roles from capability list. |
| 591 | unset ( $this->capabilities[$role] ); |
| 592 | unset ( $roles[$role]); // User cannot manage his roles. |
| 593 | } |
| 594 | } |
| 595 | |
| 596 | asort($this->capabilities); |
| 597 | |
| 598 | foreach ( array_keys($roles) as $role ) { |
| 599 | $r = get_role($role); |
| 600 | $level = ak_caps2level($r->capabilities); |
| 601 | |
| 602 | if ( $level > $this->max_level ) { |
| 603 | unset($roles[$role]); |
| 604 | } |
| 605 | } |
| 606 | |
| 607 | $this->roles = $roles; |
| 608 | } |
| 609 | } |
| 610 | |
| 611 | /** |
| 612 | * Manages backup, restore and resset roles and capabilities |
| 613 | * |
| 614 | * @hook add_management_page |
| 615 | * @return void |
| 616 | */ |
| 617 | function backupTool () |
| 618 | { |
| 619 | if ((!is_multisite() || !is_super_admin()) && !current_user_can('administrator') && !current_user_can('restore_roles')) { |
| 620 | // TODO: Implement exceptions. |
| 621 | wp_die('<strong>' .__('You do not have permission to restore roles.', 'capsman-enhanced') . '</strong>'); |
| 622 | } |
| 623 | |
| 624 | if ( 'POST' == $_SERVER['REQUEST_METHOD'] ) { |
| 625 | require_once( dirname(__FILE__).'/backup-handler.php' ); |
| 626 | $cme_backup_handler = new Capsman_BackupHandler( $this ); |
| 627 | $cme_backup_handler->processBackupTool(); |
| 628 | } |
| 629 | |
| 630 | if ( isset($_GET['action']) && 'reset-defaults' == $_GET['action']) { |
| 631 | require_once( dirname(__FILE__).'/backup-handler.php' ); |
| 632 | $cme_backup_handler = new Capsman_BackupHandler( $this ); |
| 633 | $cme_backup_handler->backupToolReset(); |
| 634 | } |
| 635 | |
| 636 | include ( dirname(CME_FILE) . '/includes/backup.php' ); |
| 637 | } |
| 638 | } |
| 639 | |
| 640 | function cme_publishpressFooter() { |
| 641 | ?> |
| 642 | <footer> |
| 643 | |
| 644 | <div class="pp-rating"> |
| 645 | <a href="https://wordpress.org/support/plugin/capability-manager-enhanced/reviews/#new-post" target="_blank" rel="noopener noreferrer"> |
| 646 | <?php printf( |
| 647 | __('If you like %s, please leave us a %s rating. Thank you!', 'capsman-enhanced'), |
| 648 | '<strong>PublishPress Capabilities</strong>', |
| 649 | '<span class="dashicons dashicons-star-filled"></span><span class="dashicons dashicons-star-filled"></span><span class="dashicons dashicons-star-filled"></span><span class="dashicons dashicons-star-filled"></span><span class="dashicons dashicons-star-filled"></span>' |
| 650 | ); |
| 651 | ?> |
| 652 | </a> |
| 653 | </div> |
| 654 | |
| 655 | <hr> |
| 656 | <nav> |
| 657 | <ul> |
| 658 | <li><a href="https://publishpress.com/capability-manager/" target="_blank" rel="noopener noreferrer" title="<?php _e('About PublishPress Capabilities', 'capsman-enhanced');?>"><?php _e('About', 'capsman-enhanced');?> |
| 659 | </a></li> |
| 660 | <li><a href="https://publishpress.com/knowledge-base/how-to-use-capability-manager/" target="_blank" rel="noopener noreferrer" title="<?php _e('Capabilites Documentation', 'capsman-enhanced');?>"><?php _e('Documentation', 'capsman-enhanced');?> |
| 661 | </a></li> |
| 662 | <li><a href="https://publishpress.com/contact" target="_blank" rel="noopener noreferrer" title="<?php _e('Contact the PublishPress team', 'capsman-enhanced');?>"><?php _e('Contact', 'capsman-enhanced');?> |
| 663 | </a></li> |
| 664 | <li><a href="https://twitter.com/publishpresscom" target="_blank" rel="noopener noreferrer"><span class="dashicons dashicons-twitter"></span> |
| 665 | </a></li> |
| 666 | <li><a href="https://facebook.com/publishpress" target="_blank" rel="noopener noreferrer"><span class="dashicons dashicons-facebook"></span> |
| 667 | </a></li> |
| 668 | </ul> |
| 669 | </nav> |
| 670 | |
| 671 | <div class="pp-pressshack-logo"> |
| 672 | <a href="https://publishpress.com" target="_blank" rel="noopener noreferrer"> |
| 673 | |
| 674 | <img src="<?php echo plugins_url('', CME_FILE) . '/common/img/publishpress-logo.png';?>" /> |
| 675 | </a> |
| 676 | </div> |
| 677 | |
| 678 | </footer> |
| 679 | <?php |
| 680 | } |
| 681 |