Skip to:
Content
Pages
Categories
Search
Top
Bottom

code to improve forums so that the users’ posts in private topics also show

  • Avatar of Dwenaus
    Dwenaus
    Participant

    @dwenaus

    I found it odd that the Forums tab only shows public topics – this makes it much less useful. So I’ve written some code that would shows all topics for logged in users in the forums tab – both public, private and hidden. I’d like to turn this into a plugin, or maybe it could be added to version 1.3. anyway here is the code, and I’d like some feedback before I make it a plugin.

    `
    //
    // Show private topics in Disucssions tab
    //

    // remove the default forums privacy filter if we’re in the forums section and the user is logged in
    function scg_show_private_discussions() {
    global $bp;

    if ( $bp->loggedin_user->id && !$bp->groups->current_group )
    remove_filter( ‘bbpress_init’, ‘groups_add_forum_privacy_sql’ );
    }
    add_action( ‘bbpress_init’, ‘scg_show_private_discussions’, 9 );

    function groups_add_forum_privacy_sql_better() {
    global $bp;

    // filter only in the forums section for logged in users
    if ( $bp->loggedin_user->id && !$bp->groups->current_group ) {
    add_filter( ‘get_topics_fields’, ‘groups_add_forum_fields_sql’ ); //unchanged
    add_filter( ‘get_topics_index_hint’, ‘groups_add_forum_tables_sql_better’ ); //changed
    add_filter( ‘get_topics_where’, ‘groups_add_forum_where_sql_better’ ); //changed
    add_filter( ‘get_topics_distinct’, ‘groups_add_forum_distinct_sql’ ); // new filter
    }
    }
    add_filter( ‘bbpress_init’, ‘groups_add_forum_privacy_sql_better’ );

    // get distinct results otherwise there are duplicates
    function groups_add_forum_distinct_sql( $sql ) {
    return $sql . ‘ DISTINCT ‘;
    }

    /*
    // this function does not need to change
    function groups_add_forum_fields_sql( $sql ) {
    return $sql . ‘, g.id as object_id, g.name as object_name, g.slug as object_slug’;
    }*/

    // added the left join to the group members table
    function groups_add_forum_tables_sql_better( $sql ) {
    global $bp;
    return $sql . ‘, ‘ . $bp->groups->table_name . ‘ AS g LEFT JOIN ‘ . $bp->groups->table_name_groupmeta . ‘ AS gm ON g.id = gm.group_id ‘ . ‘LEFT JOIN ‘. $bp->groups->table_name_members . ‘ AS gmem ON g.id = gmem.group_id ‘;
    }

    // returns all topics from public/private/hidden groups the user is confirmed member of
    function groups_add_forum_where_sql_better( $sql ) {
    global $bp;

    $bp->groups->filter_sql = ‘ AND ‘ . $sql;

    return “(gm.meta_key = ‘forum_id’ AND gm.meta_value = t.forum_id) AND (“. $sql.” AND gmem.user_id = ” . $bp->loggedin_user->id . ” AND is_confirmed = 1 AND is_banned != 1 OR g.status = ‘public’ AND “. $sql.” )”;
    }`

    please use the @dwenaus when replying so I’ll get an email response – hopefully.

Viewing 7 replies - 1 through 7 (of 7 total)
Viewing 7 replies - 1 through 7 (of 7 total)

You must be logged in to reply to this topic.