How to create Private (and moderated) Forums BuddyPress and BBPress

If you develop on BuddyPress, you’re likely to get stuck while trying to get per-forum permissions.

Indeed, it seems there’s not yet a way to make BBPress read the BuddyPress groups and hide the froums belonging to private  groups I had to develop a way myself.

Furthermore, I needed to give forum moderation rights to the moderators of each group in BuddyPress on a per-forum basis.

This is a quick and dirty tutorial for future reference. Keep in mind I am using the bleeding edge (svn) versions of WordPress Mu, BuddyPress and BBPress.

At the end of this tutorial your BBpress will hide any forum associated with a non-public user group, and group moderators will have the needed  privileges in BBPress as well (without manual intervention).

Note: I assume you already work with those three softwares and you know how to install them, install plugins, etc.

First: install WordPress Mu 2.7 (it’s not stable yet, so you’ll have to get it somewhere from svn)

Second: Install BuddyPress bleeding edge version on top of WordPress.

Third: Download the svn version of BBPress and decompress it in a sub folder.

Be sure to properly integrate BBPress with WordPress. (further reading here)

When you get everything working, download the Hidden Forums plugin and install it in BBPress.

Now, open the hidden-forums/hidden-forums.php file and replace everything from the top to the “/*    stop  editing  here    */” mark with:

require_once( 'bp_integration.php' );

When you’re done, create a new file named “bp_integration.php” in the same folder and copy paste the following code. Save everything, activate, and you’re done :) .

<?php
//settings
global $bp;
$bp['groups']['table_name_groupmeta'] = 'wp_bp_groups_groupmeta';
$bp['groups']['table_name'] = 'wp_bp_groups';
$bp['groups']['table_name_members'] = 'wp_bp_groups_members';
$bp_integration_privileges = array(); //ids of forums the logged user can moderate
function bp_integration_apply_user_permissions()
{
    global $hidden_forums, $bb_current_user;
    global $bbdb, $bp;
    global $bp_integration_privileges;
    $query = "
    SELECT group.id as id, group.enable_forum as enable_forum, meta.meta_value as forum_id FROM `{$bp['groups']['table_name']}` as `group`
    INNER JOIN `{$bp['groups']['table_name_groupmeta']}` as meta
        ON group.id = meta.group_id AND meta.meta_key = 'forum_id' AND group.status != 'public'
    ";
    $private_groups = $bbdb->get_results( $query );
    $user_id=(!empty($bb_current_user)) ? intval($bb_current_user->ID) : 0;
    // hide these forums, list by comma seperated number
    $hidden_forums['hidden_forums']=array();
    foreach ( $private_groups as $group )
    {
        $hidden_forums['hidden_forums'][] = $group->forum_id;
    }
    $allowed_group = bp_integration_allowed_forums($user_id);
    $moderator_in = bp_integration_user_privileges($user_id);
    foreach ( $allowed_group as $allowed )
    {
        foreach ( $private_groups as $group )
        {
            if ( $group->id == $allowed->group_id && $group->enable_forum )
            {
                $hidden_forums['allow_users'][$group->forum_id][] = $user_id;
            }
        }
    }
    foreach ( $moderator_in as $moderated )
    {
        foreach ( $private_groups as $group )
        {
            if ( $group->id == $moderated->group_id && $group->enable_forum )
            {
                $bp_integration_privileges[] = $group->forum_id;
            }
        }
    }
    $hidden_forums['allow_roles']['all_forums']=array('keymaster');
    $hidden_forums['allow_users']['all_forums'] = array();
}
add_action('bb_init','bp_integration_apply_user_permissions',199);
function bp_integration_user_privileges ( $user_id, $limit = false, $page = false, $get_total = true, $as_col = false )
{
    global $bbdb, $bp;
    if ( $limit && $page )
        $pag_sql = $bbdb->prepare( " LIMIT %d, %d", intval( ( $page - 1 ) * $limit), intval( $limit ) );
			$group_sql = $bbdb->prepare( "
SELECT DISTINCT group_id
FROM " . $bp['groups']['table_name_members'] . "
WHERE user_id = %d AND inviter_id = 0 AND is_banned = 0
AND ( is_admin = 1 OR is_mod = 1 )
{$pag_sql}
", $user_id );
    return $paged_groups = $bbdb->get_results( $group_sql );
}
function bp_integration_allowed_forums( $user_id, $limit = false, $page = false, $get_total = true, $as_col = false ) {
		global $bbdb, $bp;
		if ( $limit && $page )
			$pag_sql = $bbdb->prepare( " LIMIT %d, %d", intval( ( $page - 1 ) * $limit), intval( $limit ) );
			$group_sql = $bbdb->prepare( "SELECT DISTINCT group_id FROM " . $bp['groups']['table_name_members'] . " WHERE user_id = %d AND inviter_id = 0 AND is_banned = 0{$pag_sql}", $user_id );
		return $paged_groups = $bbdb->get_results( $group_sql );
	}
function bp_integration_moderators_privileges ()
{
    global $bb_current_user, $forum,$topic_id;
    global $bp_integration_privileges;
    $args = func_get_args();
     $moderator_skills = array(
		'moderate' => true,
		'participate' => true,
		'manage_tags' => true,			//+
        /* ??!?! seems not work
		'delete_topics' => true,		//+
		'close_topics' => true,			//+
		'stick_topics' => true,			//+
		'move_topics' => false,			//+
         */
        /* ??!?! seems to work */
        'delete_topic' => true,		//+
		'close_topic' => true,			//+
		'stick_topic' => true,			//+
		'move_topic' => false,
		'view_by_ip' => false,			//+
		'edit_closed' => true,			//+
		'edit_deleted' => true,			//+
		'browse_deleted' => true,		//+
		'edit_others_tags' => true,		//+
		'edit_others_topics' => true,	//+
		'delete_posts' => true,			//+
		'throttle' => true,				//+
		'ignore_edit_lock' => true,		//+
		'edit_others_posts' => true,	//+
		'edit_favorites' => true,
		'edit_tags' => true,
		'edit_topics' => true,
		'edit_posts' => true,
		'edit_profile' => true,
		'write_topics' => true,
		'write_posts' => true,
		'change_password' => true,
		'read' => true
	);
    if ( ! in_array($args[1], array_keys($moderator_skills)) )
    {
        //echo '<pre>';var_dump( $args[1] );echo '</pre>';
        return $args[0];
    }
    if (!$bb_current_user || in_array('keymaster',$bb_current_user->roles) || in_array('administrator',$bb_current_user->roles))
    {
		return $args[0];
    }
    //--------> OK, LET'S GO !
    // prevents forum moderators to stick a topic to the forum's front page
    if ( $args[1] == 'stick_topic' )
    {
        global $super;
        $super = 0;
    }
	if (empty($forum)) {
		$topic = get_topic($topic_id);
		$forum_id = $topic->forum_id;
	} else {
        $forum_id = $forum->forum_id;
    }
    if ( !$forum_id ) return $args[0];
    if ( in_array( $forum_id, $bp_integration_privileges ))
    {
        $args[0] = $moderator_skills[ $args[1] ];
    }
    return $args[0];
}
add_filter('bb_current_user_can','bp_integration_moderators_privileges',10,25);
?>

This seems like a great time to subscribe my RSS !

One response to “How to create Private (and moderated) Forums BuddyPress and BBPress”

  1. Ray

    There’s a plugin called bpGroups that does something similar to what you have written up in this article.
    http://code.ourcommoninterest.org/2009/02/14/buddypress-group-forums-for-bbpress/

Leave a Reply

Bills

Don’t forget to Subscribe

Latest Activity

Posts

  • Google this is ridiculous
    Google you’re doing it wrong. Very wrong. This is utterly ridiculous. It’s a screenshot with a standard Firefox browser, in the standard screen resolution (1280×800), on Read More
  • A step back from the open source
    One month ago, I created my first Android app. While the app was a paid one, the reception has been outstanding. I’ve gotten a fair amount Read More
  • Facebook shuts down EventPress development.
    When you’re a big company, especially one that makes its money on free web services and advertising, it’s very easy to say you love open Read More
  • Why Android is laggy
    Great post from Andrew Munn explaining some key differences between Android and iPhone/iOs, especially when it comes to rendering and smoothness of animations and why Read More
  • Google Plus keeps your data as much as Facebook does
    When you delete an account from Google+, Google promises you to delete all the data associated with your G+ profile. Well, I deleted mine some time Read More