| 1 |
<?php |
| 2 |
/** |
| 3 |
* Handles the edit role screen. |
| 4 |
* |
| 5 |
* @package Members |
| 6 |
* @subpackage Admin |
| 7 |
* @author The MemberPress Team |
| 8 |
* @copyright Copyright (c) 2009 - 2018, The MemberPress Team |
| 9 |
* @link https://members-plugin.com/ |
| 10 |
* @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html |
| 11 |
*/ |
| 12 |
namespace Members\Admin; |
| 13 |
|
| 14 |
defined('ABSPATH') || exit; |
| 15 |
/** |
| 16 |
* Class that displays the edit role screen and handles the form submissions for that page. |
| 17 |
* |
| 18 |
* @since 2.0.0 |
| 19 |
* @access public |
| 20 |
*/ |
| 21 |
final class Role_Edit { |
| 22 |
|
| 23 |
/** |
| 24 |
* Current role object to be edited/viewed. |
| 25 |
* |
| 26 |
* @since 2.0.0 |
| 27 |
* @access protected |
| 28 |
* @var object |
| 29 |
*/ |
| 30 |
protected $role; |
| 31 |
|
| 32 |
/** |
| 33 |
* Current Members role object to be edited/viewed. |
| 34 |
* |
| 35 |
* @since 2.0.0 |
| 36 |
* @access protected |
| 37 |
* @var object |
| 38 |
*/ |
| 39 |
protected $members_role; |
| 40 |
|
| 41 |
/** |
| 42 |
* Whether the current role can be edited. |
| 43 |
* |
| 44 |
* @since 2.0.0 |
| 45 |
* @access protected |
| 46 |
* @var bool |
| 47 |
*/ |
| 48 |
protected $is_editable = true; |
| 49 |
|
| 50 |
/** |
| 51 |
* Available capabilities. |
| 52 |
* |
| 53 |
* @since 2.0.0 |
| 54 |
* @access protected |
| 55 |
* @var array |
| 56 |
*/ |
| 57 |
protected $capabilities = array(); |
| 58 |
|
| 59 |
/** |
| 60 |
* Whether the page was updated. |
| 61 |
* |
| 62 |
* @since 2.0.0 |
| 63 |
* @access protected |
| 64 |
* @var bool |
| 65 |
*/ |
| 66 |
protected $role_updated = false; |
| 67 |
|
| 68 |
/** |
| 69 |
* Sets up some necessary actions/filters. |
| 70 |
* |
| 71 |
* @since 2.0.0 |
| 72 |
* @access public |
| 73 |
* @return void |
| 74 |
*/ |
| 75 |
public function __construct() { |
| 76 |
|
| 77 |
// Add help tabs. |
| 78 |
add_action( 'members_load_role_edit', array( $this, 'add_help_tabs' ) ); |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* Runs on the `load-{$page}` hook. This is the handler for form submissions. |
| 83 |
* |
| 84 |
* @since 2.0.0 |
| 85 |
* @access public |
| 86 |
* @return void |
| 87 |
*/ |
| 88 |
public function load() { |
| 89 |
|
| 90 |
// If the current user can't edit roles, don't proceed. |
| 91 |
if ( ! current_user_can( 'edit_roles' ) ) |
| 92 |
wp_die( esc_html__( 'Whoah, partner!', 'members' ) ); |
| 93 |
|
| 94 |
// Get the current role object to edit. |
| 95 |
$this->role = get_role( members_sanitize_role( $_GET['role'] ) ); |
| 96 |
|
| 97 |
// If we don't have a real role, die. |
| 98 |
if ( is_null( $this->role ) ) |
| 99 |
wp_die( esc_html__( 'The requested role to edit does not exist.', 'members' ) ); |
| 100 |
|
| 101 |
$this->members_role = members_get_role( $this->role->name ); |
| 102 |
|
| 103 |
// Get all the capabilities. |
| 104 |
$this->capabilities = members_get_capabilities(); |
| 105 |
|
| 106 |
// Add all caps from the cap groups. |
| 107 |
foreach ( members_get_cap_groups() as $group ) |
| 108 |
$this->capabilities = array_merge( $this->capabilities, $group->caps ); |
| 109 |
|
| 110 |
// Make sure we have a unique array of caps. |
| 111 |
$this->capabilities = array_unique( $this->capabilities ); |
| 112 |
|
| 113 |
// Is the role editable? |
| 114 |
$this->is_editable = members_is_role_editable( $this->role->name ); |
| 115 |
|
| 116 |
// Check if the form has been submitted. |
| 117 |
if ( $this->is_editable && isset( $_POST['members_edit_role_nonce'] ) ) { |
| 118 |
|
| 119 |
// Verify the nonce. |
| 120 |
check_admin_referer( 'edit_role', 'members_edit_role_nonce' ); |
| 121 |
|
| 122 |
// Get the granted and denied caps. |
| 123 |
$grant_caps = ! empty( $_POST['grant-caps'] ) ? members_remove_hidden_caps( array_unique( $_POST['grant-caps'] ) ) : array(); |
| 124 |
$deny_caps = ! empty( $_POST['deny-caps'] ) ? members_remove_hidden_caps( array_unique( $_POST['deny-caps'] ) ) : array(); |
| 125 |
|
| 126 |
// Get the new role label if submitted |
| 127 |
$role_label = ! empty( $_POST['role_label'] ) ? sanitize_text_field( $_POST['role_label'] ) : ''; |
| 128 |
|
| 129 |
if ($role_label) { |
| 130 |
global $wp_roles; |
| 131 |
if (isset($wp_roles->roles[$this->role->name])) { |
| 132 |
$wp_roles->roles[$this->role->name]['name'] = $role_label; |
| 133 |
// Also update the role names array which is used for translations |
| 134 |
$wp_roles->role_names[$this->role->name] = $role_label; |
| 135 |
// Make sure changes are persisted |
| 136 |
update_option($wp_roles->role_key, $wp_roles->roles); |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
// Get the new (custom) granted and denied caps. |
| 141 |
$grant_new_caps = ! empty( $_POST['grant-new-caps'] ) ? members_remove_hidden_caps( array_unique( $_POST['grant-new-caps'] ) ) : array(); |
| 142 |
$deny_new_caps = ! empty( $_POST['deny-new-caps'] ) ? members_remove_hidden_caps( array_unique( $_POST['deny-new-caps'] ) ) : array(); |
| 143 |
|
| 144 |
// Get the all and custom cap group objects. |
| 145 |
$all_group = members_get_cap_group( 'all' ); |
| 146 |
$custom_group = members_get_cap_group( 'custom' ); |
| 147 |
|
| 148 |
// New caps to push to cap groups on update. |
| 149 |
$push_caps = array(); |
| 150 |
|
| 151 |
// Set the $role_updated variable to true. |
| 152 |
$this->role_updated = true; |
| 153 |
|
| 154 |
// Loop through all available capabilities. |
| 155 |
foreach ( $this->capabilities as $cap ) { |
| 156 |
|
| 157 |
// Get the posted capability. |
| 158 |
$grant_this_cap = in_array( $cap, $grant_caps ); |
| 159 |
$deny_this_cap = in_array( $cap, $deny_caps ); |
| 160 |
|
| 161 |
// Does the role have the cap? |
| 162 |
$is_granted_cap = $this->role->has_cap( $cap ); |
| 163 |
$is_denied_cap = isset( $this->role->capabilities[ $cap ] ) && false === $this->role->capabilities[ $cap ]; |
| 164 |
|
| 165 |
if ( $grant_this_cap && ! $is_granted_cap ) |
| 166 |
$this->role->add_cap( $cap ); |
| 167 |
|
| 168 |
else if ( $deny_this_cap && ! $is_denied_cap ) |
| 169 |
$this->role->add_cap( $cap, false ); |
| 170 |
|
| 171 |
else if ( ! $grant_this_cap && $is_granted_cap ) |
| 172 |
$this->role->remove_cap( $cap ); |
| 173 |
|
| 174 |
else if ( ! $deny_this_cap && $is_denied_cap ) |
| 175 |
$this->role->remove_cap( $cap ); |
| 176 |
|
| 177 |
} // End loop through existing capabilities. |
| 178 |
|
| 179 |
// Loop through the custom granted caps. |
| 180 |
foreach ( $grant_new_caps as $grant_new_cap ) { |
| 181 |
|
| 182 |
$_cap = members_sanitize_cap( $grant_new_cap ); |
| 183 |
|
| 184 |
// If not an existing cap, add it. |
| 185 |
if ( 'do_not_allow' !== $_cap && ! in_array( $_cap, $this->capabilities ) ) { |
| 186 |
$this->role->add_cap( $_cap ); |
| 187 |
|
| 188 |
$push_caps[] = $_cap; |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
// Loop through the custom denied caps. |
| 193 |
foreach ( $deny_new_caps as $deny_new_cap ) { |
| 194 |
|
| 195 |
$_cap = members_sanitize_cap( $deny_new_cap ); |
| 196 |
|
| 197 |
// If not a granted cap and not an existing cap, add it. |
| 198 |
if ( 'do_not_allow' !== $_cap && ! in_array( $_cap, $this->capabilities ) && ! in_array( $_cap, $grant_new_caps ) ) { |
| 199 |
$this->role->add_cap( $_cap, false ); |
| 200 |
|
| 201 |
$push_caps[] = $_cap; |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
// If there are new caps, add them to the all and custom groups. |
| 206 |
if ( $push_caps ) { |
| 207 |
|
| 208 |
if ( $all_group ) { |
| 209 |
$all_group->caps[] = $_cap; |
| 210 |
sort( $all_group->caps ); |
| 211 |
} |
| 212 |
|
| 213 |
if ( $custom_group ) { |
| 214 |
$custom_group->caps[] = $_cap; |
| 215 |
sort( $custom_group->caps ); |
| 216 |
} |
| 217 |
} |
| 218 |
|
| 219 |
// Add the updated role to the role registry. |
| 220 |
members_unregister_role( $this->role->name ); |
| 221 |
|
| 222 |
members_register_role( |
| 223 |
$this->role->name, |
| 224 |
array( |
| 225 |
'label' => $role_label ? $role_label : $this->members_role->get( 'label' ), |
| 226 |
'caps' => $this->role->capabilities |
| 227 |
) |
| 228 |
); |
| 229 |
|
| 230 |
// Reset the Members role object. |
| 231 |
$this->members_role = members_get_role( $this->role->name ); |
| 232 |
|
| 233 |
// Action hook for when a role is updated. |
| 234 |
do_action( 'members_role_updated', $this->role->name ); |
| 235 |
|
| 236 |
} // End check for form submission. |
| 237 |
|
| 238 |
// If successful update. |
| 239 |
if ( $this->role_updated ) |
| 240 |
add_settings_error( 'members_edit_role', 'role_updated', sprintf( esc_html__( '%s role updated.', 'members' ), members_get_role( $this->role->name )->get( 'label' ) ), 'updated' ); |
| 241 |
|
| 242 |
// If the role is not editable. |
| 243 |
if ( ! $this->is_editable ) |
| 244 |
add_settings_error( 'members_edit_role', 'role_uneditable', sprintf( esc_html__( 'The %s role is not editable. This means that it is most likely added via another plugin for a special use or that you do not have permission to edit it.', 'members' ), members_get_role( $this->role->name )->get( 'label' ) ) ); |
| 245 |
|
| 246 |
// If editing the core administrator role. |
| 247 |
if ( 'administrator' === $this->role->name ) |
| 248 |
add_settings_error( 'members_edit_role', 'role_is_admin', sprintf( esc_html__( 'The %s role is typically the most important role on the site. Please take extreme caution that you do not inadvertently remove necessary capabilities.', 'members' ), members_get_role( $this->role->name )->get( 'label' ) ) ); |
| 249 |
|
| 250 |
// If a new role was added (redirect from new role screen). |
| 251 |
if ( isset( $_GET['message'] ) && 'role_added' === $_GET['message'] ) |
| 252 |
add_settings_error( 'members_edit_role', 'role_added', sprintf( esc_html__( 'The %s role has been created.', 'members' ), members_get_role( $this->role->name )->get( 'label' ) ), 'updated' ); |
| 253 |
|
| 254 |
// Load page hook. |
| 255 |
do_action( 'members_load_role_edit' ); |
| 256 |
|
| 257 |
// Hook for adding in meta boxes. |
| 258 |
do_action( 'members_add_role_meta_boxes', get_current_screen()->id ); |
| 259 |
|
| 260 |
// Add layout screen option. |
| 261 |
add_screen_option( 'layout_columns', array( 'max' => 2, 'default' => 2 ) ); |
| 262 |
} |
| 263 |
|
| 264 |
/** |
| 265 |
* Adds help tabs. |
| 266 |
* |
| 267 |
* @since 2.0.0 |
| 268 |
* @access public |
| 269 |
* @return void |
| 270 |
*/ |
| 271 |
public function add_help_tabs() { |
| 272 |
|
| 273 |
// Get the current screen. |
| 274 |
$screen = get_current_screen(); |
| 275 |
|
| 276 |
// Add help tabs. |
| 277 |
$screen->add_help_tab( members_get_edit_role_help_overview_args() ); |
| 278 |
$screen->add_help_tab( members_get_edit_role_help_role_name_args() ); |
| 279 |
$screen->add_help_tab( members_get_edit_role_help_edit_caps_args() ); |
| 280 |
$screen->add_help_tab( members_get_edit_role_help_custom_cap_args() ); |
| 281 |
|
| 282 |
// Set the help sidebar. |
| 283 |
$screen->set_help_sidebar( members_get_help_sidebar_text() ); |
| 284 |
} |
| 285 |
|
| 286 |
/** |
| 287 |
* Enqueue scripts/styles. |
| 288 |
* |
| 289 |
* @since 2.0.0 |
| 290 |
* @access public |
| 291 |
* @return void |
| 292 |
*/ |
| 293 |
public function enqueue() { |
| 294 |
|
| 295 |
wp_enqueue_style( 'members-admin' ); |
| 296 |
wp_enqueue_script( 'members-edit-role' ); |
| 297 |
} |
| 298 |
|
| 299 |
/** |
| 300 |
* Displays the page content. |
| 301 |
* |
| 302 |
* @since 2.0.0 |
| 303 |
* @access public |
| 304 |
* @return void |
| 305 |
*/ |
| 306 |
public function page() { ?> |
| 307 |
|
| 308 |
<div class="wrap"> |
| 309 |
|
| 310 |
<h1> |
| 311 |
<?php esc_html_e( 'Edit Role', 'members' ); ?> |
| 312 |
|
| 313 |
<?php if ( current_user_can( 'create_roles' ) ) : ?> |
| 314 |
<?php printf( '<a class="page-title-action" href="%s">%s</a>', esc_url( members_get_new_role_url() ), esc_html_x( 'Add New', 'role', 'members' ) ); ?> |
| 315 |
<?php endif; ?> |
| 316 |
</h1> |
| 317 |
|
| 318 |
<?php settings_errors( 'members_edit_role' ); ?> |
| 319 |
|
| 320 |
<div id="poststuff"> |
| 321 |
|
| 322 |
<form name="form0" method="post" action="<?php echo esc_url( members_get_edit_role_url( $this->role->name ) ); ?>"> |
| 323 |
|
| 324 |
<?php wp_nonce_field( 'edit_role', 'members_edit_role_nonce' ); ?> |
| 325 |
|
| 326 |
<div id="post-body" class="metabox-holder columns-<?php echo 1 == get_current_screen()->get_columns() ? 1 : 2; ?>"> |
| 327 |
|
| 328 |
<div id="post-body-content"> |
| 329 |
|
| 330 |
<div id="titlediv" class="members-title-div"> |
| 331 |
|
| 332 |
<div id="titlewrap"> |
| 333 |
<span class="screen-reader-text"><?php esc_html_e( 'Role Name', 'members' ); ?></span> |
| 334 |
<input type="text" name="role_label" value="<?php echo esc_attr( members_get_role( $this->role->name )->get( 'label' ) ); ?>" /> |
| 335 |
</div><!-- #titlewrap --> |
| 336 |
|
| 337 |
<div class="inside"> |
| 338 |
<div id="edit-slug-box"> |
| 339 |
<strong><?php esc_html_e( 'Role:', 'members' ); ?></strong> <?php echo esc_attr( $this->role->name ); ?> <!-- edit box --> |
| 340 |
</div> |
| 341 |
</div><!-- .inside --> |
| 342 |
|
| 343 |
</div><!-- .members-title-div --> |
| 344 |
|
| 345 |
<?php $cap_tabs = new Cap_Tabs( $this->role->name ); ?> |
| 346 |
<?php $cap_tabs->display(); ?> |
| 347 |
|
| 348 |
</div><!-- #post-body-content --> |
| 349 |
|
| 350 |
<?php wp_nonce_field( 'closedpostboxes', 'closedpostboxesnonce', false ); ?> |
| 351 |
<?php wp_nonce_field( 'meta-box-order', 'meta-box-order-nonce', false ); ?> |
| 352 |
|
| 353 |
<div id="postbox-container-1" class="postbox-container side"> |
| 354 |
|
| 355 |
<?php do_meta_boxes( get_current_screen()->id, 'side', $this->role ); ?> |
| 356 |
|
| 357 |
</div><!-- .post-box-container --> |
| 358 |
|
| 359 |
</div><!-- #post-body --> |
| 360 |
</form> |
| 361 |
|
| 362 |
</div><!-- #poststuff --> |
| 363 |
|
| 364 |
</div><!-- .wrap --> |
| 365 |
<?php } |
| 366 |
} |
| 367 |
|