phpwordpressuser-rolesbbpress

bbpress user-roles or capabilities per forum


I'm trying to setup a bbpress with extended user capabilities.

The problem

My goal is that users need to have different capabilities in each forum, i.e:

Plugins

These are the plugins I tried so far, but without success:

First attempt

Since itthinx Groups plugin allows me to assign multiple groups to UserA, which is great, it's still not solving my issue.

So, I tried something like this:

But the problem is, since UserA belongs to groups that have publish_replies and moderate capabilities, he has full access to ForumX.

So what I need is an intersection of the forum-groups and the user-groups - which in this example is ForumX_readers.

The promising part, but...

I digged into the code of the plugin and found the line that handles the capabilities of the user based on his assigned groups and quickly tried to get the current forum groups, to implement the intersection.

Unfortunatelly I was not able to access the global $post, the $_GLOBALS['post'] nor the $_REQUEST[] variables in this part of code. Neither directly nor with an apply_filters() function, that I implemented into the part of the code myself.

UPDATE: I was able to get the ID with get_posts() and the slug of the current forum/topic.

So, my question

Is there any solution to my first attempt, which I may have overseen?

If not, is there maybe any other plugin that can solve my problem that I'm not aware of?

Or is something like that even impossible in bbpress?


Solution

  • After some further research and trial & error, I finally figured it out.

    First step to do is to set up the capabilities, which in my case look something like this.

    enter image description here

    In the plugins directory, there is the file core/class-groups-user.php. The init_cache() function retrieves the assigned groups to the user, and sets the according capabilities.

    To not mess around to much with the core-plugin, I applied a filter to the $group_ids variable which can be found in line: 415.

    foreach( $user_groups as $user_group ) {
        $group_ids[] = Groups_Utility::id( $user_group->group_id );
    }
    
    // added this line
    $group_ids = apply_filters('filter_user_group_ids', $group_ids);`
    

    I then created a new plugin, which hooks into this filter.

    add_filter('filter_user_group_ids', 'dnmc_filter_groups', 10, 1);
    
    function dnmc_filter_groups($user_group_ids) {
        $forum_id = dnmc_get_forum_id();
        if(!$forum_id) return $user_group_ids;
    
        $forum_group_ids   = Groups_Post_Access::get_read_group_ids( $forum_id);
        $user_restricted_forum_group_ids = array_intersect($user_group_ids, $forum_group_ids);
    
        return $user_restricted_forum_group_ids;
    
    }
    
    function dnmc_get_forum_id() {
        $args_topic = array(
          'name'        => basename( untrailingslashit( rtrim($_SERVER['REQUEST_URI'], '/') ) ),
          'post_type'   => 'topic',
          'post_status' => 'publish',
          'numberposts' => 1
        );
    
        if($topic = get_posts($args_topic)) {
            return $topic[0]->post_parent;
        }
    
        $args_forum = array(
          'name'        => basename( untrailingslashit( rtrim($_SERVER['REQUEST_URI'], '/') ) ),
          'post_type'   => 'forum',
          'post_status' => 'publish',
          'numberposts' => 1
        );
    
        if($forum = get_posts($args_forum)) {
            return $forum[0]->ID;
        }
    
        return false;
    }