function roles_capabilities_edit()
 {
     global $wpdb, $current_user;
     if (isset($_POST['save_roles'])) {
         $objrole = new mgm_roles();
         extract($_POST);
         $status = 'error';
         $role_type = '';
         $message = array();
         if (!empty($rolename)) {
             $error = false;
             foreach ($rolename as $role => $value) {
                 //added later to consider only the edited role:
                 if ($role == $selected_role) {
                     $value = trim(mgm_escape($value));
                     if (empty($value)) {
                         $message[] = __('Role cannot be blank', 'mgm');
                         $error = true;
                     } elseif (!preg_match("/^[A-Za-z0-9_,\\s]+\$/", $value)) {
                         $message[] = __('Role cannot contain special characters.', 'mgm');
                         $error = true;
                     } elseif (!$objrole->is_role_unique($value, true, $role)) {
                         $message[] = __('Role/capability already exists.', 'mgm');
                         $error = true;
                     }
                     if (!isset($chk_capability[$role]) || isset($chk_capability[$role]) && empty($chk_capability[$role])) {
                         $message[] = __('Capability must be selected', 'mgm');
                         $error = true;
                     }
                     break;
                 }
             }
             if (!$error) {
                 //save roles:
                 foreach ($rolename as $role => $value) {
                     if ($role == $selected_role) {
                         $key = $role;
                         //save Role name:
                         if (!in_array($role, $objrole->default_roles)) {
                             //please note: this will return the edited role
                             $role = $objrole->edit_role($role, $value);
                         }
                         //remove
                         if (!empty($chk_capability[$key])) {
                             //save capabilities:
                             $arr_previous_caps = $objrole->get_capabilities($role);
                             $arr_new_caps = $chk_capability[$key];
                             $arr_to_add = array_diff($arr_new_caps, $arr_previous_caps);
                             $arr_to_remove = array_diff($arr_previous_caps, $arr_new_caps);
                             //add new capabilities:
                             if (!empty($arr_to_add)) {
                                 foreach ($arr_to_add as $cap) {
                                     $cap = mgm_escape($cap);
                                     //grant access
                                     $objrole->update_capability_role($role, $cap, true);
                                 }
                             }
                             //remove access if any capabilities unchecked
                             if (!empty($arr_to_remove)) {
                                 foreach ($arr_to_remove as $cap) {
                                     $cap = mgm_escape($cap);
                                     //remove access
                                     $objrole->update_capability_role($role, $cap, false);
                                 }
                             }
                         }
                         break;
                     }
                 }
                 $type = $role_type;
                 //from post
                 $message[] = __('Successfully saved the changes.', 'mgm');
                 $status = 'success';
             }
         }
         echo json_encode(array('status' => $status, 'message' => implode("<br/>", $message), 'type' => $type));
         exit;
     }
 }