Allow participant role to trash their own topics and replies on BBPress

Hello,
There are a lot of old code snippets ranging from 1-5 years, and I do not trust my coding abilities to take this on by myself.

What I would like to do is allow participants to be able to trash but not delete their own topics and replies. They should also be able to restore their own topics and replies. This should all be based on the BBPress edit time. If the time runs out, they can no longer trash or restore their topic or reply.

Can you please help me do this?

Thanks,
Alex :smiley:

  • Sajid
    • DEV MAN’s Sidekick

    Hi @alexstine,
    Hope you are doing good today :slight_smile:

    I am sorry but writing a code for such functionality is a bit complex and custom job, that requires a good of deal attention and time, that is beyond the level of support we can provide here. Sorry!

    Take care and have a nice day :slight_smile:
    Cheers, Sajid

  • Alex Stine
    • System Administrator

    Hello,
    For anyone who needs this functionality, add this code to functions.php.

    /*Customize the BBPress roles to allow Participants to trash topics
    add_filter( 'bbp_get_caps_for_role', 'ST_add_role_caps_filter', 10, 2 );
    
    function ST_add_role_caps_filter( $caps, $role ){
        // Only filter for roles we are interested in!
        if( $role == 'bbp_participant' ) {
    
    	$new_caps = array(
                    // Primary caps
                    'spectate'              => true,
                    'participate'           => true,
                    'moderate'              => false,
                    'throttle'              => false,
                    'view_trash'            => false,
    
                    // Forum caps
                    'publish_forums'        => false,
                    'edit_forums'           => false,
                    'edit_others_forums'    => false,
                    'delete_forums'         => false,
                    'delete_others_forums'  => false,
                    'read_private_forums'   => false,
                    'read_hidden_forums'    => false,
    
                    // Topic caps
                    'publish_topics'        => true,
                    'edit_topics'           => true,
                    'edit_others_topics'    => false,
                    'delete_topics'         => true,
                    'delete_others_topics'  => false,
                    'read_private_topics'   => false,
    
                    // Reply caps
                    'publish_replies'       => true,
                    'edit_replies'          => true,
                    'edit_others_replies'   => false,
                    'delete_replies'        => true,
                    'delete_others_replies' => false,
                    'read_private_replies'  => false,
    
                    // Topic tag caps
                    'manage_topic_tags'     => false,
                    'edit_topic_tags'       => false,
                    'delete_topic_tags'     => false,
                    'assign_topic_tags'     => true,
                );	
    
    	}
    
        return $new_caps;
    }
    /*Fixes an issue that only allows mods to trash topics.
    https://bbpress.trac.wordpress.org/changeset/5852
    https://bbpress.trac.wordpress.org/ticket/2685*/
    
    add_filter( 'bbp_map_reply_meta_caps', 'ST_tweak_trash_meta_caps', 11, 4 );
    add_filter( 'bbp_map_topic_meta_caps', 'ST_tweak_trash_meta_caps', 11, 4 );
    
    // tweak for replies
    function ST_tweak_trash_meta_caps( $caps, $cap, $user_id, $args ){
    
    	// apply only to delete_reply and delete_topic
    	if ( $cap == "delete_reply" || $cap == "delete_topic" ){
    		// Get the post
    		$_post = get_post( $args[0] );
    		if ( !empty( $_post ) ) {
    
    			// Get caps for post type object
    			$post_type = get_post_type_object( $_post->post_type );
    			$caps      = array();
    
    			// Add 'do_not_allow' cap if user is spam or deleted
    			if ( bbp_is_user_inactive( $user_id ) ) {
    				$caps[] = 'do_not_allow';
    
    			// Moderators can always edit forum content
    			} elseif ( user_can( $user_id, 'moderate' ) ) {
    				$caps[] = 'moderate';
    
    			// User is author so allow edit if not in admin
                } elseif ( ! is_admin() && ( (int) $user_id === (int) $_post->post_author ) ) {
                    $caps[] = $post_type->cap->delete_posts;
    
    			// Unknown so map to delete_others_posts
    			} else {
    				$caps[] = $post_type->cap->delete_others_posts;
    			}
    		}
    
    	}
    	// return the capabilities
    	return $caps;
    }

    Thanks,
    Alex :smiley:

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.