| 1 |
<?php |
| 2 |
/** |
| 3 |
* Handles the new 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 new role screen and handles the form submissions for that page. |
| 17 |
* |
| 18 |
* @since 2.0.0 |
| 19 |
* @access public |
| 20 |
*/ |
| 21 |
final class Role_New { |
| 22 |
|
| 23 |
/** |
| 24 |
* Holds the instances of this class. |
| 25 |
* |
| 26 |
* @since 2.0.0 |
| 27 |
* @access private |
| 28 |
* @var object |
| 29 |
*/ |
| 30 |
private static $instance; |
| 31 |
|
| 32 |
/** |
| 33 |
* Name of the page we've created. |
| 34 |
* |
| 35 |
* @since 2.0.0 |
| 36 |
* @access public |
| 37 |
* @var string |
| 38 |
*/ |
| 39 |
public $page = ''; |
| 40 |
|
| 41 |
/** |
| 42 |
* Role that's being created. |
| 43 |
* |
| 44 |
* @since 2.0.0 |
| 45 |
* @access public |
| 46 |
* @var string |
| 47 |
*/ |
| 48 |
public $role = ''; |
| 49 |
|
| 50 |
/** |
| 51 |
* Name of the role that's being created. |
| 52 |
* |
| 53 |
* @since 2.0.0 |
| 54 |
* @access public |
| 55 |
* @var string |
| 56 |
*/ |
| 57 |
public $role_name = ''; |
| 58 |
|
| 59 |
/** |
| 60 |
* Array of the role's capabilities. |
| 61 |
* |
| 62 |
* @since 2.0.0 |
| 63 |
* @access public |
| 64 |
* @var array |
| 65 |
*/ |
| 66 |
public $capabilities = array(); |
| 67 |
|
| 68 |
/** |
| 69 |
* Conditional to see if we're cloning a role. |
| 70 |
* |
| 71 |
* @since 2.0.0 |
| 72 |
* @access public |
| 73 |
* @var bool |
| 74 |
*/ |
| 75 |
public $is_clone = false; |
| 76 |
|
| 77 |
/** |
| 78 |
* Role that is being cloned. |
| 79 |
* |
| 80 |
* @since 2.0.0 |
| 81 |
* @access public |
| 82 |
* @var string |
| 83 |
*/ |
| 84 |
public $clone_role = ''; |
| 85 |
|
| 86 |
/** |
| 87 |
* Sets up our initial actions. |
| 88 |
* |
| 89 |
* @since 2.0.0 |
| 90 |
* @access public |
| 91 |
* @return void |
| 92 |
*/ |
| 93 |
public function __construct() { |
| 94 |
|
| 95 |
// If the role manager is active. |
| 96 |
if ( function_exists('members_role_manager_enabled') && members_role_manager_enabled() ) { |
| 97 |
add_action( 'admin_menu', array( $this, 'add_submenu_admin_page' ), 20 ); |
| 98 |
} |
| 99 |
add_action( 'admin_menu', array( $this, 'add_admin_page' ) ); |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* Adds the roles page to the admin. |
| 104 |
* |
| 105 |
* @since 2.0.0 |
| 106 |
* @access public |
| 107 |
* @return void |
| 108 |
*/ |
| 109 |
public function add_admin_page() { |
| 110 |
|
| 111 |
$this->page = add_menu_page( 'Members', 'Members', 'create_roles', 'members', array( $this, 'page' ), 'data:image/svg+xml;base64,' . base64_encode( '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 200 140" aria-hidden="true" focusable="false"><g opacity="0.55" fill="currentColor"><circle cx="48" cy="46" r="18"/><path d="M16 118 C16 88 30 76 48 76 C66 76 80 88 80 118 Z"/></g><g opacity="0.55" fill="currentColor"><circle cx="152" cy="46" r="18"/><path d="M120 118 C120 88 134 76 152 76 C170 76 184 88 184 118 Z"/></g><circle cx="100" cy="36" r="24" fill="currentColor"/><path d="M58 124 C58 88 76 76 100 76 C124 76 142 88 142 124 Z" fill="currentColor"/></svg>' ) ); |
| 112 |
|
| 113 |
// We don't need to have a "Members" link in the submenu, so this removes it |
| 114 |
add_submenu_page( 'members', '', '', 'create_roles', 'members', array( $this, 'page' ) ); |
| 115 |
remove_submenu_page( 'members', 'members' ); |
| 116 |
|
| 117 |
// Let's roll if we have a page. |
| 118 |
if ( $this->page ) { |
| 119 |
|
| 120 |
add_action( "load-{$this->page}", array( $this, 'load' ) ); |
| 121 |
add_action( "load-{$this->page}", array( $this, 'add_help_tabs' ) ); |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
/** |
| 126 |
* Adds the "Add New Role" submenu page to the admin. |
| 127 |
* |
| 128 |
* @since 3.0.0 |
| 129 |
* @access public |
| 130 |
* @return void |
| 131 |
*/ |
| 132 |
public function add_submenu_admin_page() { |
| 133 |
add_submenu_page( 'members', esc_html_x( 'Add New Role', 'admin screen', 'members' ), esc_html_x( 'Add New Role', 'admin screen', 'members' ), 'create_roles', 'members', array( $this, 'page' ) ); |
| 134 |
} |
| 135 |
|
| 136 |
/** |
| 137 |
* Checks posted data on load and performs actions if needed. |
| 138 |
* |
| 139 |
* @since 2.0.0 |
| 140 |
* @access public |
| 141 |
* @return void |
| 142 |
*/ |
| 143 |
public function load() { |
| 144 |
|
| 145 |
// Are we cloning a role? |
| 146 |
$this->is_clone = isset( $_GET['clone'] ) && members_role_exists( $_GET['clone'] ); |
| 147 |
|
| 148 |
if ( $this->is_clone ) { |
| 149 |
|
| 150 |
// Override the default new role caps. |
| 151 |
add_filter( 'members_new_role_default_caps', array( $this, 'clone_default_caps' ), 15 ); |
| 152 |
|
| 153 |
// Set the clone role. |
| 154 |
$this->clone_role = members_sanitize_role( $_GET['clone'] ); |
| 155 |
} |
| 156 |
|
| 157 |
// Check if the current user can create roles and the form has been submitted. |
| 158 |
if ( current_user_can( 'create_roles' ) && isset( $_POST['members_new_role_nonce'] ) ) { |
| 159 |
|
| 160 |
// Verify the nonce. |
| 161 |
check_admin_referer( 'new_role', 'members_new_role_nonce' ); |
| 162 |
|
| 163 |
// Set up some variables. |
| 164 |
$this->capabilities = array(); |
| 165 |
$new_caps = array(); |
| 166 |
$is_duplicate = false; |
| 167 |
|
| 168 |
// Get all the capabilities. |
| 169 |
$_m_caps = members_get_capabilities(); |
| 170 |
|
| 171 |
// Add all caps from the cap groups. |
| 172 |
foreach ( members_get_cap_groups() as $group ) |
| 173 |
$_m_caps = array_merge( $_m_caps, $group->caps ); |
| 174 |
|
| 175 |
// Make sure we have a unique array of caps. |
| 176 |
$_m_caps = array_unique( $_m_caps ); |
| 177 |
|
| 178 |
// Check if any capabilities were selected. |
| 179 |
if ( isset( $_POST['grant-caps'] ) || isset( $_POST['deny-caps'] ) ) { |
| 180 |
|
| 181 |
$grant_caps = ! empty( $_POST['grant-caps'] ) ? members_remove_hidden_caps( array_unique( $_POST['grant-caps'] ) ) : array(); |
| 182 |
$deny_caps = ! empty( $_POST['deny-caps'] ) ? members_remove_hidden_caps( array_unique( $_POST['deny-caps'] ) ) : array(); |
| 183 |
|
| 184 |
foreach ( $_m_caps as $cap ) { |
| 185 |
|
| 186 |
if ( in_array( $cap, $grant_caps ) ) |
| 187 |
$new_caps[ $cap ] = true; |
| 188 |
|
| 189 |
else if ( in_array( $cap, $deny_caps ) ) |
| 190 |
$new_caps[ $cap ] = false; |
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
$grant_new_caps = ! empty( $_POST['grant-new-caps'] ) ? members_remove_hidden_caps( array_unique( $_POST['grant-new-caps'] ) ) : array(); |
| 195 |
$deny_new_caps = ! empty( $_POST['deny-new-caps'] ) ? members_remove_hidden_caps( array_unique( $_POST['deny-new-caps'] ) ) : array(); |
| 196 |
|
| 197 |
foreach ( $grant_new_caps as $grant_new_cap ) { |
| 198 |
|
| 199 |
$_cap = members_sanitize_cap( $grant_new_cap ); |
| 200 |
|
| 201 |
if ( ! in_array( $_cap, $_m_caps ) ) |
| 202 |
$new_caps[ $_cap ] = true; |
| 203 |
} |
| 204 |
|
| 205 |
foreach ( $deny_new_caps as $deny_new_cap ) { |
| 206 |
|
| 207 |
$_cap = members_sanitize_cap( $deny_new_cap ); |
| 208 |
|
| 209 |
if ( ! in_array( $_cap, $_m_caps ) ) |
| 210 |
$new_caps[ $_cap ] = false; |
| 211 |
} |
| 212 |
|
| 213 |
// Sanitize the new role name/label. We just want to strip any tags here. |
| 214 |
if ( ! empty( $_POST['role_name'] ) ) |
| 215 |
$this->role_name = wp_strip_all_tags( wp_unslash( $_POST['role_name'] ) ); |
| 216 |
|
| 217 |
// Sanitize the new role, removing any unwanted characters. |
| 218 |
if ( ! empty( $_POST['role'] ) ) |
| 219 |
$this->role = members_sanitize_role( $_POST['role'] ); |
| 220 |
|
| 221 |
else if ( $this->role_name ) |
| 222 |
$this->role = members_sanitize_role( $this->role_name ); |
| 223 |
|
| 224 |
// Is duplicate? |
| 225 |
if ( members_role_exists( $this->role ) ) |
| 226 |
$is_duplicate = true; |
| 227 |
|
| 228 |
// Add a new role with the data input. |
| 229 |
if ( $this->role && $this->role_name && ! $is_duplicate ) { |
| 230 |
|
| 231 |
add_role( $this->role, $this->role_name, $new_caps ); |
| 232 |
|
| 233 |
// Track as created by Members UI (for reset-roles to only remove these). |
| 234 |
members_track_created_role( $this->role ); |
| 235 |
|
| 236 |
// Action hook for when a role is added. |
| 237 |
do_action( 'members_role_added', $this->role ); |
| 238 |
|
| 239 |
// If the current user can edit roles, redirect to edit role screen. |
| 240 |
if ( current_user_can( 'edit_roles' ) ) { |
| 241 |
wp_redirect( esc_url_raw( add_query_arg( 'message', 'role_added', members_get_edit_role_url( $this->role ) ) ) ); |
| 242 |
exit; |
| 243 |
} |
| 244 |
|
| 245 |
// Add role added message. |
| 246 |
add_settings_error( 'members_role_new', 'role_added', sprintf( esc_html__( 'The %s role has been created.', 'members' ), $this->role_name ), 'updated' ); |
| 247 |
} |
| 248 |
|
| 249 |
// If there are new caps, let's assign them. |
| 250 |
if ( ! empty( $new_caps ) ) |
| 251 |
$this->capabilities = $new_caps; |
| 252 |
|
| 253 |
// Add error if there's no role. |
| 254 |
if ( ! $this->role ) |
| 255 |
add_settings_error( 'members_role_new', 'no_role', esc_html__( 'You must enter a valid role.', 'members' ) ); |
| 256 |
|
| 257 |
// Add error if this is a duplicate role. |
| 258 |
if ( $is_duplicate ) |
| 259 |
add_settings_error( 'members_role_new', 'duplicate_role', sprintf( esc_html__( 'The %s role already exists.', 'members' ), $this->role ) ); |
| 260 |
|
| 261 |
// Add error if there's no role name. |
| 262 |
if ( ! $this->role_name ) |
| 263 |
add_settings_error( 'members_role_new', 'no_role_name', esc_html__( 'You must enter a valid role name.', 'members' ) ); |
| 264 |
} |
| 265 |
|
| 266 |
// If we don't have caps yet, get the new role default caps. |
| 267 |
if ( empty( $this->capabilities ) ) |
| 268 |
$this->capabilities = members_new_role_default_caps(); |
| 269 |
|
| 270 |
// Load page hook. |
| 271 |
do_action( 'members_load_role_new' ); |
| 272 |
|
| 273 |
// Hook for adding in meta boxes. |
| 274 |
do_action( 'members_add_role_meta_boxes', get_current_screen()->id ); |
| 275 |
|
| 276 |
// Add layout screen option. |
| 277 |
add_screen_option( 'layout_columns', array( 'max' => 2, 'default' => 2 ) ); |
| 278 |
|
| 279 |
// Load scripts/styles. |
| 280 |
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue' ) ); |
| 281 |
} |
| 282 |
|
| 283 |
/** |
| 284 |
* Adds help tabs. |
| 285 |
* |
| 286 |
* @since 2.0.0 |
| 287 |
* @access public |
| 288 |
* @return void |
| 289 |
*/ |
| 290 |
public function add_help_tabs() { |
| 291 |
|
| 292 |
// Get the current screen. |
| 293 |
$screen = get_current_screen(); |
| 294 |
|
| 295 |
// Add help tabs. |
| 296 |
$screen->add_help_tab( members_get_edit_role_help_overview_args() ); |
| 297 |
$screen->add_help_tab( members_get_edit_role_help_role_name_args() ); |
| 298 |
$screen->add_help_tab( members_get_edit_role_help_edit_caps_args() ); |
| 299 |
$screen->add_help_tab( members_get_edit_role_help_custom_cap_args() ); |
| 300 |
|
| 301 |
// Set the help sidebar. |
| 302 |
$screen->set_help_sidebar( members_get_help_sidebar_text() ); |
| 303 |
} |
| 304 |
|
| 305 |
/** |
| 306 |
* Enqueue scripts/styles. |
| 307 |
* |
| 308 |
* @since 2.0.0 |
| 309 |
* @access public |
| 310 |
* @return void |
| 311 |
*/ |
| 312 |
public function enqueue() { |
| 313 |
|
| 314 |
wp_enqueue_style( 'members-admin' ); |
| 315 |
wp_enqueue_script( 'members-edit-role' ); |
| 316 |
} |
| 317 |
|
| 318 |
/** |
| 319 |
* Outputs the page. |
| 320 |
* |
| 321 |
* @since 2.0.0 |
| 322 |
* @access public |
| 323 |
* @return void |
| 324 |
*/ |
| 325 |
public function page() { ?> |
| 326 |
|
| 327 |
<div class="wrap"> |
| 328 |
|
| 329 |
<h1><?php ! $this->is_clone ? esc_html_e( 'Add New Role', 'members' ) : esc_html_e( 'Clone Role', 'members' ); ?></h1> |
| 330 |
|
| 331 |
<?php settings_errors( 'members_role_new' ); ?> |
| 332 |
|
| 333 |
<div id="poststuff"> |
| 334 |
|
| 335 |
<form name="form0" method="post" action="<?php echo esc_url( members_get_new_role_url() ); ?>"> |
| 336 |
|
| 337 |
<?php wp_nonce_field( 'new_role', 'members_new_role_nonce' ); ?> |
| 338 |
|
| 339 |
<div id="post-body" class="metabox-holder columns-<?php echo 1 == get_current_screen()->get_columns() ? 1 : 2; ?>"> |
| 340 |
|
| 341 |
<div id="post-body-content"> |
| 342 |
|
| 343 |
<div id="titlediv" class="members-title-div"> |
| 344 |
|
| 345 |
<div id="titlewrap"> |
| 346 |
<span class="screen-reader-text"><?php esc_html_e( 'Role Name', 'members' ); ?></span> |
| 347 |
<input type="text" name="role_name" value="<?php echo ! $this->role && $this->clone_role ? esc_attr( sprintf( __( '%s Clone', 'members' ), members_get_role( $this->clone_role )->get( 'label' ) ) ) : esc_attr( $this->role_name ); ?>" placeholder="<?php esc_attr_e( 'Enter role name', 'members' ); ?>" /> |
| 348 |
</div><!-- #titlewrap --> |
| 349 |
|
| 350 |
<div class="inside"> |
| 351 |
<div id="edit-slug-box"> |
| 352 |
<strong><?php esc_html_e( 'Role:', 'members' ); ?></strong> <span class="role-slug"><?php echo ! $this->role && $this->clone_role ? esc_attr( "{$this->clone_role}_clone" ) : esc_attr( $this->role ); ?></span> <!-- edit box --> |
| 353 |
<input type="text" name="role" value="<?php echo members_sanitize_role( $this->role ); ?>" /> |
| 354 |
<button type="button" class="role-edit-button button button-small closed"><?php esc_html_e( 'Edit', 'members' ); ?></button> |
| 355 |
</div> |
| 356 |
</div><!-- .inside --> |
| 357 |
|
| 358 |
</div><!-- .members-title-div --> |
| 359 |
|
| 360 |
<?php $cap_tabs = new Cap_Tabs( '', $this->capabilities ); ?> |
| 361 |
<?php $cap_tabs->display(); ?> |
| 362 |
|
| 363 |
</div><!-- #post-body-content --> |
| 364 |
|
| 365 |
<?php wp_nonce_field( 'closedpostboxes', 'closedpostboxesnonce', false ); ?> |
| 366 |
<?php wp_nonce_field( 'meta-box-order', 'meta-box-order-nonce', false ); ?> |
| 367 |
|
| 368 |
<div id="postbox-container-1" class="postbox-container side"> |
| 369 |
|
| 370 |
<?php do_meta_boxes( get_current_screen()->id, 'side', '' ); ?> |
| 371 |
|
| 372 |
</div><!-- .post-box-container --> |
| 373 |
|
| 374 |
</div><!-- #post-body --> |
| 375 |
</form> |
| 376 |
|
| 377 |
</div><!-- #poststuff --> |
| 378 |
|
| 379 |
</div><!-- .wrap --> |
| 380 |
|
| 381 |
<?php } |
| 382 |
|
| 383 |
/** |
| 384 |
* Filters the new role default caps in the case that we're cloning a role. |
| 385 |
* |
| 386 |
* @since 2.0.0 |
| 387 |
* @access public |
| 388 |
* @param array $capabilities |
| 389 |
* @param array |
| 390 |
*/ |
| 391 |
public function clone_default_caps( $capabilities ) { |
| 392 |
|
| 393 |
if ( $this->is_clone ) { |
| 394 |
|
| 395 |
$role = get_role( $this->clone_role ); |
| 396 |
|
| 397 |
if ( $role && isset( $role->capabilities ) && is_array( $role->capabilities ) ) |
| 398 |
$capabilities = $role->capabilities; |
| 399 |
} |
| 400 |
|
| 401 |
return $capabilities; |
| 402 |
} |
| 403 |
|
| 404 |
/** |
| 405 |
* Returns the instance. |
| 406 |
* |
| 407 |
* @since 2.0.0 |
| 408 |
* @access public |
| 409 |
* @return object |
| 410 |
*/ |
| 411 |
public static function get_instance() { |
| 412 |
|
| 413 |
if ( ! self::$instance ) |
| 414 |
self::$instance = new self; |
| 415 |
|
| 416 |
return self::$instance; |
| 417 |
} |
| 418 |
} |
| 419 |
|
| 420 |
Role_New::get_instance(); |
| 421 |
|