Exemplo n.º 1
0
 /**
  * Callback function for handling forum role changes.  Note that we needed to execute this function 
  * on a different hook, `profile_update`.  Using the normal hooks on the edit user screen won't work 
  * because WP will wipe out the role.
  *
  * @since  1.0.0
  * @access public
  * @param  int    $user_id
  * @return void
  */
 public function role_update($user_id)
 {
     /* If the current user can't promote users or edit this particular user, bail. */
     if (!current_user_can('promote_users') || !current_user_can('edit_user', $user_id)) {
         return;
     }
     /* Is this a forum role change? */
     if (!isset($_POST['mb_forum_role'])) {
         return;
     }
     /* Get keymaster variables. */
     $is_keymaster = mb_is_user_keymaster(get_current_user_id());
     $keymaster_role = mb_get_keymaster_role();
     /* Get role variables. */
     $forum_role = mb_get_user_role($user_id);
     $new_role = sanitize_key($_POST['mb_forum_role']);
     /* Only keymasters can promote/demote other keymasters. */
     if ($is_keymaster && ($keymaster_role === $forum_role || $keymaster_role === $new_role)) {
         return;
     }
     /* If there's a new role and it doesn't match the old one, set it. */
     if ($forum_role !== $new_role && !empty($new_role)) {
         mb_set_user_role($user_id, $new_role);
     } elseif ($forum_role !== $new_role && empty($new_role)) {
         mb_remove_user_role($user_id, $new_role);
     }
 }
Exemplo n.º 2
0
/**
 * Maps default WordPress roles to the plugin's roles.  This is the default used when a user doesn't yet
 * have a forum role.  Developers can add custom-created roles to the map using a filter on the
 * `mb_get_role_map` hook.  Roles are mapped in key/value pairs.  The key is the WP or custom role.  The
 * value is the forum role to map it to.
 *
 * @since  1.0.0
 * @access public
 * @return array
 */
function mb_get_role_map()
{
    $default_role = mb_get_default_role();
    $mapped = array('administrator' => mb_get_keymaster_role(), 'editor' => $default_role, 'author' => $default_role, 'contributor' => $default_role, 'subscriber' => $default_role);
    return apply_filters('mb_get_role_map', $mapped, $default_role);
}
Exemplo n.º 3
0
 /**
  * Callback function for handling post status changes.
  *
  * @since  1.0.0
  * @access public
  * @return void
  */
 public function handler()
 {
     /* If the current user can't promote users or there are no users, bail. */
     if (!current_user_can('promote_users') || empty($_REQUEST['users'])) {
         return;
     }
     /* Is this a forum role change request? */
     if (empty($_REQUEST['mb_forum_role']) || empty($_REQUEST['mb_change_role'])) {
         return;
     }
     /* Get the new role. */
     $new_role = sanitize_key($_REQUEST['mb_forum_role']);
     /* Get an array of the allowed forum roles. */
     $dynamic_roles = mb_get_dynamic_roles();
     /* If the new role isn't one of our forum roles, bail. */
     if (!isset($dynamic_roles[$new_role])) {
         return;
     }
     /* Get some variables we need. */
     $current_user_id = get_current_user_id();
     $keymaster_role = mb_get_keymaster_role();
     $is_keymaster = mb_is_user_keymaster($current_user_id);
     /* Only keymasters can make new keymasters. */
     if ($keymaster_role === $new_role && false === $is_keymaster) {
         return;
     }
     /* Loop through each user and change their forum role. */
     foreach ((array) $_REQUEST['users'] as $user_id) {
         /* You can't go changing your own role, silly! */
         if ($current_user_id === $user_id) {
             continue;
         }
         /* Get the user's current forum role. */
         $forum_role = mb_get_user_role($user_id);
         /* Only keymasters can demote other keymasters. */
         if (false === $is_keymaster && $keymaster_role === $forum_role) {
             continue;
         }
         /* If the new role doesn't match the user's current role, let's set it. */
         if ($new_role !== $forum_role) {
             mb_set_user_role($user_id, $new_role);
         }
     }
     /* Remove some query args and build the redirect URL. */
     $url = remove_query_arg(array('s', 'action', 'new_role', 'mb_forum_role', 'mb_change_role', 'action2', 'users'));
     /* Redirect back to the users screen. */
     wp_safe_redirect($url);
 }