/** * Sets a new role level. This function also updates all users of the given role to update * their user level. * * Note: WP will always set the user level to the highest level when calling the * `WP_User:update_user_level_from_caps()` method, so there's no need to check for the * highest role when dealing with users with multiple roles. * * @since 1.0.0 * @access public * @param string|object $role * @return void */ function mrl_set_role_level($role, $new_level = 'level_0') { // Make sure we have the role object. if (!is_object($role)) { $role = get_role($role); } // Get the allowed levels. $levels = array_keys(mrl_get_role_levels()); // Get the posted level without the `level` prefix. $new_level_numeric = absint(mrl_remove_level_prefix($new_level)); // Get the levels to add and remove. $add = array_slice($levels, 0, $new_level_numeric + 1, true); $remove = array_diff($levels, $add); // Add new levels. foreach ($add as $add_level) { $role->add_cap($add_level); } // Remove levels. foreach ($remove as $remove_level) { $role->remove_cap($remove_level); } // Get the users with the current role. $users = get_users(array('role' => $role->name)); // If there are users with the role, update their user level from caps. if ($users) { foreach ($users as $user) { $user->update_user_level_from_caps(); } } }
/** * Returns the content for the level column. * * @since 1.0.0 * @access public * @return void */ public function column_level($out, $role) { $level = mrl_get_role_level(get_role($role)); return $level ? mrl_remove_level_prefix($level) : '–'; }