/**
  * Runs on the `load-{$page}` hook.  This is the handler for form submissions.
  *
  * @since  1.0.0
  * @access public
  * @return void
  */
 public function load()
 {
     // If the current user can't edit roles, don't proceed.
     if (!current_user_can('edit_roles')) {
         wp_die(esc_html__('Whoah, partner!', 'members'));
     }
     // Get the current role object to edit.
     $this->role = get_role(members_sanitize_role($_GET['role']));
     // If we don't have a real role, die.
     if (is_null($this->role)) {
         wp_die(esc_html__('The requested role to edit does not exist.', 'members'));
     }
     $this->members_role = members_get_role($this->role->name);
     // Get all the capabilities.
     $this->capabilities = members_get_capabilities();
     // Add all caps from the cap groups.
     foreach (members_get_cap_groups() as $group) {
         $this->capabilities = array_merge($this->capabilities, $group->caps);
     }
     // Make sure we have a unique array of caps.
     $this->capabilities = array_unique($this->capabilities);
     // Is the role editable?
     $this->is_editable = members_is_role_editable($this->role->name);
     // Check if the form has been submitted.
     if ($this->is_editable && isset($_POST['members_edit_role_nonce'])) {
         // Verify the nonce.
         check_admin_referer('edit_role', 'members_edit_role_nonce');
         // Get the granted and denied 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();
         // Get the new (custom) granted and denied caps.
         $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();
         // Get the all and custom cap group objects.
         $all_group = members_get_cap_group('all');
         $custom_group = members_get_cap_group('custom');
         // New caps to push to cap groups on update.
         $push_caps = array();
         // Set the $role_updated variable to true.
         $this->role_updated = true;
         // Loop through all available capabilities.
         foreach ($this->capabilities as $cap) {
             // Get the posted capability.
             $grant_this_cap = in_array($cap, $grant_caps);
             $deny_this_cap = in_array($cap, $deny_caps);
             // Does the role have the cap?
             $is_granted_cap = $this->role->has_cap($cap);
             $is_denied_cap = isset($this->role->capabilities[$cap]) && false === $this->role->capabilities[$cap];
             if ($grant_this_cap && !$is_granted_cap) {
                 $this->role->add_cap($cap);
             } else {
                 if ($deny_this_cap && !$is_denied_cap) {
                     $this->role->add_cap($cap, false);
                 } else {
                     if (!$grant_this_cap && $is_granted_cap) {
                         $this->role->remove_cap($cap);
                     } else {
                         if (!$deny_this_cap && $is_denied_cap) {
                             $this->role->remove_cap($cap);
                         }
                     }
                 }
             }
         }
         // End loop through existing capabilities.
         // Loop through the custom granted caps.
         foreach ($grant_new_caps as $grant_new_cap) {
             $_cap = members_sanitize_cap($grant_new_cap);
             // If not an existing cap, add it.
             if (!in_array($_cap, $this->capabilities)) {
                 $this->role->add_cap($_cap);
                 $push_caps[] = $_cap;
             }
         }
         // Loop through the custom denied caps.
         foreach ($deny_new_caps as $deny_new_cap) {
             $_cap = members_sanitize_cap($deny_new_cap);
             // If not a granted cap and not an existing cap, add it.
             if (!in_array($_cap, $this->capabilities) && !in_array($_cap, $grant_new_caps)) {
                 $this->role->add_cap($_cap, false);
                 $push_caps[] = $_cap;
             }
         }
         // If there are new caps, add them to the all and custom groups.
         if ($push_caps) {
             if ($all_group) {
                 $all_group->caps[] = $_cap;
                 sort($all_group->caps);
             }
             if ($custom_group) {
                 $custom_group->caps[] = $_cap;
                 sort($custom_group->caps);
             }
         }
         // Add the updated role to the role factory.
         members_role_factory()->add_role($this->role->name);
         // Reset the Members role object.
         $this->members_role = members_get_role($this->role->name);
         // Action hook for when a role is updated.
         do_action('members_role_updated', $this->role->name);
     }
     // End check for form submission.
     // If successful update.
     if ($this->role_updated) {
         add_settings_error('members_edit_role', 'role_updated', sprintf(esc_html__('%s role updated.', 'members'), members_get_role_name($this->role->name)), 'updated');
     }
     // If the role is not editable.
     if (!$this->is_editable) {
         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_name($this->role->name)));
     }
     // If a new role was added (redirect from new role screen).
     if (isset($_GET['message']) && 'role_added' === $_GET['message']) {
         add_settings_error('members_edit_role', 'role_added', sprintf(esc_html__('The %s role has been created.', 'members'), members_get_role_name($this->role->name)), 'updated');
     }
     // Load page hook.
     do_action('members_load_role_edit');
     // Hook for adding in meta boxes.
     do_action('add_meta_boxes_' . get_current_screen()->id, $this->role->name);
     do_action('add_meta_boxes', get_current_screen()->id, $this->role->name);
     // Add layout screen option.
     add_screen_option('layout_columns', array('max' => 2, 'default' => 2));
 }
 /**
  * Check to see if the given role has a cap, and add if it doesn't exist.
  *
  * @param  object $role User Cap object, part of WP_User.
  * @param  string $cap  Cap to test against.
  * @return void
  */
 public function maybe_add_cap($role, $cap)
 {
     // Update the roles, if needed.
     if (!$role->has_cap($cap)) {
         $role->add_cap($cap);
     }
 }
Exemplo n.º 3
0
/**
 * Add caps shortcut for a role
 *
 * @param object $role 
 * @return void
 * @author Amaury Balmer
 */
function Shortcuts_Translation_Caps(&$role)
{
    $role->add_cap('edit_' . SHORT_CPT);
    $role->add_cap('read_' . SHORT_CPT);
    $role->add_cap('delete_' . SHORT_CPT);
    $role->add_cap('edit_' . SHORT_CPT . 's');
    $role->add_cap('edit_others_' . SHORT_CPT . 's');
    $role->add_cap('publish_' . SHORT_CPT . 's');
    $role->add_cap('read_private_' . SHORT_CPT . 's');
    $role->add_cap('delete_' . SHORT_CPT . 's');
    $role->add_cap('delete_private_' . SHORT_CPT . 's');
    $role->add_cap('delete_published_' . SHORT_CPT . 's');
    $role->add_cap('delete_others_' . SHORT_CPT . 's');
    $role->add_cap('edit_private_' . SHORT_CPT . 's');
    $role->add_cap('edit_published_' . SHORT_CPT . 's');
}
Exemplo n.º 4
0
 /**
  * Add an existing capability to a specific WordPress role
  *
  * @brief Add a cap to a role
  *
  * @since 1.0.0
  *
  * @param string $sRoleName - role name/key that receive the cap
  * @param string $sCapName  - capability name/key
  * @param bool   $bCapValue (optional) boolean value of capability. Default to TRUE.
  *
  * @return mixed TRUE|WPDKError
  *
  */
 static function add_cap_to_role($sRoleName, $sCapName, $bCapValue = TRUE)
 {
     // Does the role exist?
     if (FALSE == array_key_exists($sRoleName, self::get_all_roles())) {
         return new WPDKError('wpdk_roles_caps', sprintf(__('Role %s does not exist in system.', WPDK_TEXTDOMAIN), $sRoleName));
     }
     // Does the cap exist?
     if (FALSE == self::cap_exists($sCapName)) {
         return new WPDKError('wpdk_roles_caps', sprintf(__('Unable to add unexistent capability "%s" to role "%s".', WPDK_TEXTDOMAIN), $sCapName, $sRoleName));
     }
     // Does the role have already this cap?
     if (TRUE == array_key_exists($sCapName, self::get_caps_of_role($sRoleName))) {
         return new WPDKError('wpdk_roles_caps', sprintf(__('Role "%s" has already the capability "%s".', WPDK_TEXTDOMAIN), $sRoleName, $sCapName));
     }
     // add cap to role
     self::$cWpRoles->add_cap($sRoleName, $sCapName, $bCapValue);
     // add_cap method of WP_Roles returns void. So return TRUE
     return TRUE;
 }