/** * 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)); }
/** * Return a map of WordPress roles to bbPress roles. Used to automatically grant * appropriate bbPress roles to WordPress users that wouldn't already have a * role in the forums. Also guarantees WordPress admins get the Keymaster role. * * @since bbPress (r4334) * * @return array Filtered array of WordPress roles to bbPress roles */ function bbp_get_user_role_map() { // Get the default role once here $default_role = bbp_get_default_role(); // Return filtered results, forcing admins to keymasters. return (array) apply_filters('bbp_get_user_role_map', array('administrator' => bbp_get_keymaster_role(), 'editor' => $default_role, 'author' => $default_role, 'contributor' => $default_role, 'subscriber' => $default_role)); }
/** * Allow global access setting field * * @since 2.0.0 bbPress (r3378) * * @uses checked() To display the checked attribute */ function bbp_admin_setting_callback_global_access() { // Get the default role once rather than loop repeatedly below $default_role = bbp_get_default_role(); // Start the output buffer for the select dropdown ob_start(); ?> </label> <label for="_bbp_default_role"> <select name="_bbp_default_role" id="_bbp_default_role" <?php bbp_maybe_admin_setting_disabled('_bbp_default_role'); ?> > <?php foreach (bbp_get_dynamic_roles() as $role => $details) { ?> <option <?php selected($default_role, $role); ?> value="<?php echo esc_attr($role); ?> "><?php echo translate_user_role($details['name']); ?> </option> <?php } ?> </select> <?php $select = ob_get_clean(); ?> <label for="_bbp_allow_global_access"> <input name="_bbp_allow_global_access" id="_bbp_allow_global_access" type="checkbox" value="1" <?php checked(bbp_allow_global_access(true)); bbp_maybe_admin_setting_disabled('_bbp_allow_global_access'); ?> /> <?php printf(esc_html__('Automatically give registered visitors the %s forum role', 'bbpress'), $select); ?> </label> <?php }
function etheme_bb_user_role() { if (!function_exists('bbp_is_deactivation')) { return; } // 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 = 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(); } $new_role = bbp_get_default_role(); bbp_set_user_role($user_id, $new_role); }
/** * Output forum role selector (for user edit) * * @since bbPress (r4284) */ function bbp_admin_setting_callback_default_role() { $default_role = bbp_get_default_role(); ?> <select name="_bbp_default_role" id="_bbp_default_role" <?php bbp_maybe_admin_setting_disabled('_bbp_default_role'); ?> > <?php foreach (bbp_get_dynamic_roles() as $role => $details) { ?> <option <?php selected($default_role, $role); ?> value="<?php echo esc_attr($role); ?> "><?php echo translate_user_role($details['name']); ?> </option> <?php } ?> </select> <?php }