Example #1
0
/**
 * Add the default role and mapped bbPress caps 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 bbp_allow_global_access()
 * @uses bbp_is_user_inactive()
 * @uses is_user_logged_in()
 * @uses current_user_can()
 * @uses get_option()
 * @uses bbp_get_caps_for_role()
 *
 * @return If not multisite, not global, or user is deleted/spammed
 */
function bbp_global_access_role_mask()
{
    // Bail if not multisite or forum is not global
    if (!bbp_allow_global_access()) {
        return;
    }
    // Bail if user is marked as spam or is deleted
    if (bbp_is_user_inactive()) {
        return;
    }
    // Normal user is logged in but has no caps
    if (is_user_logged_in() && !current_user_can('read')) {
        // Assign user the minimal participant role to map caps to
        $default_role = bbp_get_participant_role();
        // Get bbPress caps for the default role
        $caps_for_role = bbp_get_caps_for_role($default_role);
        // Set all caps to true
        foreach ($caps_for_role as $cap) {
            $mapped_meta_caps[$cap] = true;
        }
        // Add 'read' cap just in case
        $mapped_meta_caps['read'] = true;
        $mapped_meta_caps['bbp_masked'] = true;
        // Allow global access caps to be manipulated
        $mapped_meta_caps = apply_filters('bbp_global_access_mapped_meta_caps', $mapped_meta_caps);
        // Assign the role and mapped caps to the current user
        $bbp = bbpress();
        $bbp->current_user->roles[0] = $default_role;
        $bbp->current_user->caps = $mapped_meta_caps;
        $bbp->current_user->allcaps = $mapped_meta_caps;
    }
}
/**
 * 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();
    }
}
Example #3
0
/**
 * 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 bbp_allow_global_access()
 * @uses bbp_is_user_inactive()
 * @uses is_user_logged_in()
 * @uses is_user_member_of_blog()
 * @uses get_option()
 *
 * @return If not multisite, not global, or user is deleted/spammed
 */
function bbp_set_current_user_default_role()
{
    // Bail if forum is not global
    if (!bbp_allow_global_access()) {
        return;
    }
    // Bail if not logged in or already a member of this site
    if (!is_user_logged_in() || is_user_member_of_blog()) {
        return;
    }
    // Bail if user is marked as spam or is deleted
    if (bbp_is_user_inactive()) {
        return;
    }
    // Assign the default role to the current user
    bbpress()->current_user->set_role(get_option('default_role', 'subscriber'));
}
Example #4
0
/**
 * 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 
}
Example #5
0
/**
 * Allow global access setting field
 *
 * @since bbPress (r3378)
 *
 * @uses checked() To display the checked attribute
 */
function bbp_admin_setting_callback_global_access()
{
    ?>

	<input id="_bbp_allow_global_access" name="_bbp_allow_global_access" type="checkbox" id="_bbp_allow_global_access" value="1" <?php 
    checked(bbp_allow_global_access(false));
    ?>
 />
	<label for="_bbp_allow_global_access"><?php 
    _e('Allow all registered users to create topics and replies', 'bbpress');
    ?>
</label>

<?php 
}
Example #6
0
/**
 * Allow global access setting field
 *
 * @since bbPress (r3378)
 *
 * @uses checked() To display the checked attribute
 */
function bbp_admin_setting_callback_global_access()
{
    ?>

	<input id="_bbp_allow_global_access" name="_bbp_allow_global_access" type="checkbox" id="_bbp_allow_global_access" value="1" <?php 
    checked(bbp_allow_global_access(false));
    bbp_maybe_admin_setting_disabled('_bbp_allow_global_access');
    ?>
 />
	<label for="_bbp_allow_global_access"><?php 
    _e('Automatically assign default role to new, registered users upon visiting the site.', 'bbpress');
    ?>
</label>

<?php 
}