/** * Return a user's blog role * * @since bbPress (r4446) * * @param int $user_id * @uses bbp_get_user_id() To get the user id * @uses get_userdata() To get the user data * @uses apply_filters() Calls 'bbp_get_user_blog_role' with the role and user id * @return string */ function bbp_get_user_blog_role($user_id = 0) { // Add bbPress roles (returns $wp_roles global) bbp_add_forums_roles(); // Validate user id $user_id = bbp_get_user_id($user_id); $user = get_userdata($user_id); $role = false; // User has roles so lets if (!empty($user->roles)) { // Look for a non bbPress role $roles = array_intersect(array_values($user->roles), array_keys(bbp_get_blog_roles())); // If there's a role in the array, use the first one. This isn't very // smart, but since roles aren't exactly hierarchical, and WordPress // does not yet have a UI for multiple user roles, it's fine for now. if (!empty($roles)) { $role = array_shift($roles); } } return apply_filters('bbp_get_user_blog_role', $role, $user_id, $user); }
/** * This repair tool will map each user of the current site to their respective * forums role. By default, Admins will be Key Masters, and every other role * will be the default role defined in Settings > Forums (Participant). * * @since 2.2.0 bbPress (r4340) * * @uses bbp_get_user_role_map() To get the map of user roles * @uses bbp_get_default_role() To get the default bbPress user role * @uses bbp_get_blog_roles() To get the current WordPress roles * @uses get_users() To get the users of each role (limited to ID field) * @uses bbp_set_user_role() To set each user's forums role */ function bbp_admin_repair_user_roles() { $statement = __('Remapping forum role for each user on this site… %s', 'bbpress'); $changed = 0; $role_map = bbp_get_user_role_map(); $default_role = bbp_get_default_role(); // Bail if no role map exists if (empty($role_map)) { return array(1, sprintf($statement, __('Failed!', 'bbpress'))); } // Iterate through each role... foreach (array_keys(bbp_get_blog_roles()) as $role) { // Reset the offset $offset = 0; // If no role map exists, give the default forum role (bbp-participant) $new_role = isset($role_map[$role]) ? $role_map[$role] : $default_role; // Get users of this site, limited to 1000 while ($users = get_users(array('role' => $role, 'fields' => 'ID', 'number' => 1000, 'offset' => $offset))) { // Iterate through each user of $role and try to set it foreach ((array) $users as $user_id) { if (bbp_set_user_role($user_id, $new_role)) { ++$changed; // Keep a count to display at the end } } // Bump the offset for the next query iteration $offset = $offset + 1000; } } $result = sprintf(__('Complete! %s users updated.', 'bbpress'), bbp_number_format($changed)); return array(0, sprintf($statement, $result)); }
/** * Output blog role selector (for user edit) * * @since 2.0.0 bbPress (r2688) */ function bbp_edit_user_blog_role() { // Return if no user is being edited if (!bbp_is_single_user_edit()) { return; } // Get users current blog role $user_role = bbp_get_user_blog_role(bbp_get_displayed_user_id()); // Get the blog roles $blog_roles = bbp_get_blog_roles(); ?> <select name="role" id="role"> <option value=""><?php esc_html_e('— No role for this site —', 'bbpress'); ?> </option> <?php foreach ($blog_roles as $role => $details) { ?> <option <?php selected($user_role, $role); ?> value="<?php echo esc_attr($role); ?> "><?php echo bbp_translate_user_role($details['name']); ?> </option> <?php } ?> </select> <?php }