/** * Adds a role object. * * @since 1.0.0 * @access public * @param string $role * @return object */ public function add_role($role) { // If the role exists with WP but hasn't been added. if (members_role_exists($role)) { // Get the role object. $this->roles[$role] = new Members_Role($role); // Check if role is editable. if ($this->roles[$role]->is_editable) { $this->editable[$role] = $this->roles[$role]; } else { $this->uneditable[$role] = $this->roles[$role]; } // Is WP role? if (members_is_wordpress_role($role)) { $this->wordpress[$role] = $this->roles[$role]; } } }
/** * Sanitizes/Validates widget options before being saved. * * @since 0.1.0 * @access public * @param array $new_instance * @param array $old_instance * @return array */ function update($new_instance, $old_instance) { // Text fields. $instance['title'] = sanitize_text_field($new_instance['title']); $instance['order'] = sanitize_text_field($new_instance['order']); $instance['orderby'] = sanitize_text_field($new_instance['orderby']); $instance['meta_key'] = sanitize_text_field($new_instance['meta_key']); $instance['meta_value'] = sanitize_text_field($new_instance['meta_value']); $instance['search'] = sanitize_text_field($new_instance['search']); // Roles. $instance['role'] = members_role_exists($new_instance['role']) ? $new_instance['role'] : ''; // ID lists. $instance['include'] = $new_instance['include'] ? join(',', wp_parse_id_list($new_instance['include'])) : ''; $instance['exclude'] = $new_instance['exclude'] ? join(',', wp_parse_id_list($new_instance['exclude'])) : ''; // Integers. $instance['offset'] = absint($new_instance['offset']); $instance['number'] = absint($new_instance['number']) > 0 ? absint($new_instance['number']) : ''; return $instance; }
/** * Runs on the `load-{$page}` hook. This is the handler for form submissions and requests. * * @since 1.0.0 * @access public * @return void */ public function load() { // Get the current action if sent as request. $action = isset($_REQUEST['action']) ? sanitize_key($_REQUEST['action']) : false; // Get the current action if posted. if (isset($_POST['action']) && 'delete' == $_POST['action'] || isset($_POST['action2']) && 'delete' == $_POST['action2']) { $action = 'bulk-delete'; } // Bulk delete role handler. if ('bulk-delete' === $action) { // If roles were selected, let's delete some roles. if (current_user_can('delete_roles') && isset($_POST['roles']) && is_array($_POST['roles'])) { // Verify the nonce. Nonce created via `WP_List_Table::display_tablenav()`. check_admin_referer('bulk-roles'); // Loop through each of the selected roles. foreach ($_POST['roles'] as $role) { $role = members_sanitize_role($role); if (members_role_exists($role)) { members_delete_role($role); } } // Add roles deleted message. add_settings_error('members_roles', 'roles_deleted', esc_html__('Selected roles deleted.', 'members'), 'updated'); } // Delete single role handler. } else { if ('delete' === $action) { // Make sure the current user can delete roles. if (current_user_can('delete_roles')) { // Verify the referer. check_admin_referer('delete_role', 'members_delete_role_nonce'); // Get the role we want to delete. $role = members_sanitize_role($_GET['role']); // Check that we have a role before attempting to delete it. if (members_role_exists($role)) { // Add role deleted message. add_settings_error('members_roles', 'role_deleted', sprintf(esc_html__('%s role deleted.', 'members'), members_get_role_name($role)), 'updated'); // Delete the role. members_delete_role($role); } } } } // Load page hook. do_action('members_load_manage_roles'); }
/** * 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 output of the roles column on the `users.php` screen. * * @since 1.0.0 * @access public * @param string $output * @param string $column * @param int $user_id * @return string */ function members_manage_users_custom_column($output, $column, $user_id) { if ('roles' === $column && members_multiple_user_roles_enabled()) { $user = new WP_User($user_id); $user_roles = array(); $output = esc_html__('None', 'members'); if (is_array($user->roles)) { foreach ($user->roles as $role) { if (members_role_exists($role)) { $user_roles[] = members_translate_role($role); } } $output = join(', ', $user_roles); } } return $output; }