/** * Checks posted data on load and performs actions if needed. * * @since 1.0.0 * @access public * @return void */ public function load() { // Are we cloning a role? $this->is_clone = isset($_GET['clone']) && members_role_exists($_GET['clone']); if ($this->is_clone) { // Override the default new role caps. add_filter('members_new_role_default_caps', array($this, 'clone_default_caps'), 15); // Set the clone role. $this->clone_role = members_sanitize_role($_GET['clone']); } // Check if the current user can create roles and the form has been submitted. if (current_user_can('create_roles') && (isset($_POST['role_name']) || isset($_POST['role']) || isset($_POST['grant-caps']) || isset($_POST['deny-caps']) || isset($_POST['grant-new-caps']) || isset($_POST['deny-new-caps']))) { // Verify the nonce. check_admin_referer('new_role', 'members_new_role_nonce'); // Set up some variables. $this->capabilities = array(); $new_caps = array(); $is_duplicate = false; // Check if any capabilities were selected. if (isset($_POST['grant-caps']) || isset($_POST['deny-caps'])) { $grant_caps = !empty($_POST['grant-caps']) ? array_unique($_POST['grant-caps']) : array(); $deny_caps = !empty($_POST['deny-caps']) ? array_unique($_POST['deny-caps']) : array(); foreach (members_get_capabilities() as $cap) { if (in_array($cap, $grant_caps)) { $new_caps[$cap] = true; } else { if (in_array($cap, $deny_caps)) { $new_caps[$cap] = false; } } } } $grant_new_caps = !empty($_POST['grant-new-caps']) ? array_unique($_POST['grant-new-caps']) : array(); $deny_new_caps = !empty($_POST['deny-new-caps']) ? array_unique($_POST['deny-new-caps']) : array(); $_m_caps = members_get_capabilities(); foreach ($grant_new_caps as $grant_new_cap) { $_cap = members_sanitize_cap($grant_new_cap); if (!in_array($_cap, $_m_caps)) { $new_caps[$_cap] = true; } } foreach ($deny_new_caps as $deny_new_cap) { $_cap = members_sanitize_cap($deny_new_cap); if (!in_array($_cap, $_m_caps)) { $new_caps[$_cap] = false; } } // Sanitize the new role name/label. We just want to strip any tags here. if (!empty($_POST['role_name'])) { $this->role_name = strip_tags($_POST['role_name']); } // Sanitize the new role, removing any unwanted characters. if (!empty($_POST['role'])) { $this->role = members_sanitize_role($_POST['role']); } else { if ($this->role_name) { $this->role = members_sanitize_role($this->role_name); } } // Is duplicate? if (members_role_exists($this->role)) { $is_duplicate = true; } // Add a new role with the data input. if ($this->role && $this->role_name && !$is_duplicate) { add_role($this->role, $this->role_name, $new_caps); // If the current user can edit roles, redirect to edit role screen. if (current_user_can('edit_roles')) { wp_redirect(add_query_arg('message', 'role_added', members_get_edit_role_url($this->role))); exit; } // Add role added message. add_settings_error('members_role_new', 'role_added', sprintf(esc_html__('The %s role has been created.', 'members'), $this->role_name), 'updated'); } // If there are new caps, let's assign them. if (!empty($new_caps)) { $this->capabilities = $new_caps; } // Add error if there's no role. if (!$this->role) { add_settings_error('members_role_new', 'no_role', esc_html__('You must enter a valid role.', 'members')); } // Add error if this is a duplicate role. if ($is_duplicate) { add_settings_error('members_role_new', 'duplicate_role', sprintf(esc_html__('The %s role already exists.', 'members'), $this->role)); } // Add error if there's no role name. if (!$this->role_name) { add_settings_error('members_role_new', 'no_role_name', esc_html__('You must enter a valid role name.', 'members')); } } // If we don't have caps yet, get the new role default caps. if (empty($this->capabilities)) { $this->capabilities = members_new_role_default_caps(); } // Load page hook. do_action('members_load_role_new'); // Hook for adding in meta boxes. do_action('add_meta_boxes_' . get_current_screen()->id, ''); do_action('add_meta_boxes', get_current_screen()->id, ''); // Add layout screen option. add_screen_option('layout_columns', array('max' => 2, 'default' => 2)); // Load scripts/styles. add_action('admin_enqueue_scripts', array($this, 'enqueue')); }
/** * Handles the row actions. * * @since 1.0.0 * @access protected * @param string $role * @param string $column_name * @param string $primary * @return array */ protected function handle_row_actions($role, $column_name, $primary) { $actions = array(); // Only add row actions on the primary column (title/role name). if ($primary === $column_name) { // If the role can be edited. if (members_is_role_editable($role)) { // If the current user can edit the role, add an edit link. if (current_user_can('edit_roles')) { $actions['edit'] = sprintf('<a href="%s">%s</a>', esc_url(members_get_edit_role_url($role)), esc_html__('Edit', 'members')); } // If the current user can delete the role, add a delete link. if (is_multisite() && is_super_admin() && $role !== $this->default_role || current_user_can('delete_roles') && $role !== $this->default_role && !current_user_can($role)) { $actions['delete'] = sprintf('<a class="members-delete-role-link" href="%s">%s</a>', esc_url(members_get_delete_role_url($role)), esc_html__('Delete', 'members')); } // If the role cannot be edited. } else { // Add the view role link. $actions['view'] = sprintf('<a href="%s">%s</a>', esc_url(members_get_edit_role_url($role)), esc_html__('View', 'members')); } // If the current user can create roles, add the clone role link. if (current_user_can('create_roles')) { $actions['clone'] = sprintf('<a href="%s">%s</a>', esc_url(members_get_clone_role_url($role)), esc_html__('Clone', 'members')); } // If this is the default role and the current user can manage options, add a default role change link. if (current_user_can('manage_options') && $role === $this->default_role) { $actions['default_role'] = sprintf('<a href="%s">%s</a>', esc_url(admin_url('options-general.php#default_role')), esc_html__('Change Default', 'members')); } // If the currrent user can view users, add a users link. if (current_user_can('list_users')) { $actions['users'] = sprintf('<a href="%s">%s</a>', members_get_role_users_url($role), esc_html__('Users', 'members')); } // Allow devs to filter the row actions. $actions = apply_filters('members_roles_row_actions', $actions, $role); } return $this->row_actions($actions); }
/** * Displays the page content. * * @since 1.0.0 * @access public * @return void */ public function page() { ?> <div class="wrap"> <h1> <?php esc_html_e('Edit Role', 'members'); ?> <?php if (current_user_can('create_roles')) { ?> <?php printf('<a class="page-title-action" href="%s">%s</a>', esc_url(members_get_new_role_url()), esc_html__('Add New', 'members')); ?> <?php } ?> </h1> <?php settings_errors('members_edit_role'); ?> <div id="poststuff"> <form name="form0" method="post" action="<?php echo esc_url(members_get_edit_role_url($this->role->name)); ?> "> <?php wp_nonce_field('edit_role', 'members_edit_role_nonce'); ?> <div id="post-body" class="metabox-holder columns-<?php echo 1 == get_current_screen()->get_columns() ? 1 : 2; ?> "> <div id="post-body-content"> <div id="titlediv" class="members-title-div"> <div id="titlewrap"> <span class="screen-reader-text"><?php esc_html_e('Role Name', 'members'); ?> </span> <input type="text" disabled="disabled" readonly="readonly" value="<?php echo esc_attr(members_get_role_name($this->role->name)); ?> " /> </div><!-- #titlewrap --> <div class="inside"> <div id="edit-slug-box"> <strong><?php esc_html_e('Role:', 'members'); ?> </strong> <?php echo esc_attr($this->role->name); ?> <!-- edit box --> </div> </div><!-- .inside --> </div><!-- .members-title-div --> <?php $cap_tabs = new Members_Cap_Tabs($this->role->name); ?> <?php $cap_tabs->display(); ?> </div><!-- #post-body-content --> <?php wp_nonce_field('closedpostboxes', 'closedpostboxesnonce', false); ?> <?php wp_nonce_field('meta-box-order', 'meta-box-order-nonce', false); ?> <div id="postbox-container-1" class="postbox-container side"> <?php do_meta_boxes(get_current_screen()->id, 'side', $this->role); ?> </div><!-- .post-box-container --> </div><!-- #post-body --> </form> </div><!-- #poststuff --> </div><!-- .wrap --> <?php }