/** * 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)); }
/** * Add the default role to the current user if needed * * This function will bail if the forum is not global in a multisite * installation of WordPress, or if the user is marked as spam or deleted. * * @since bbPress (r3380) * * @uses is_user_logged_in() To bail if user is not logged in * @uses bbp_get_user_role() To bail if user already has a role * @uses bbp_is_user_inactive() To bail if user is inactive * @uses bbp_allow_global_access() To know whether to save role to database * @uses bbp_get_user_role_map() To get the WP to BBP role map array * @uses bbp_get_default_role() To get the site's default forums role * @uses get_option() * * @return If not multisite, not global, or user is deleted/spammed */ function bbp_set_current_user_default_role() { /** Sanity ****************************************************************/ // Bail if deactivating bbPress if (bbp_is_deactivation()) { return; } // Catch all, to prevent premature user initialization if (!did_action('set_current_user')) { return; } // Bail if not logged in or already a member of this site if (!is_user_logged_in()) { return; } // Get the current user ID $user_id = bbp_get_current_user_id(); // Bail if user already has a forums role if (bbp_get_user_role($user_id)) { return; } // Bail if user is marked as spam or is deleted if (bbp_is_user_inactive($user_id)) { return; } /** Ready *****************************************************************/ // Load up bbPress once $bbp = bbpress(); // Get whether or not to add a role to the user account $add_to_site = bbp_allow_global_access(); // Get the current user's WordPress role. Set to empty string if none found. $user_role = bbp_get_user_blog_role($user_id); // Get the role map $role_map = bbp_get_user_role_map(); /** Forum Role ************************************************************/ // Use a mapped role if (isset($role_map[$user_role])) { $new_role = $role_map[$user_role]; // Use the default role } else { $new_role = bbp_get_default_role(); } /** Add or Map ************************************************************/ // Add the user to the site if (true === $add_to_site) { // Make sure bbPress roles are added bbp_add_forums_roles(); $bbp->current_user->add_role($new_role); // Don't add the user, but still give them the correct caps dynamically } else { $bbp->current_user->caps[$new_role] = true; $bbp->current_user->get_role_caps(); } }