Skip to:
Content
Pages
Categories
Search
Top
Bottom

Allow members to trash their own topics based on edit time

  • @tech55541

    Participant

    Hello,
    I am starting a new thread because all the searches I found come up 1-5 years old and I do not trust some of the code when I do not know how to read it very well.

    I would like to allow participants to be able to trash (not delete) their replies and posts if they are still allowed to edit them based on the edit time provided in BBPress settings. Can you please give me some code to achieve this?

    Thanks.

Viewing 9 replies - 1 through 9 (of 9 total)
  • @tech55541

    Participant

    Hello,
    I would really appreciate it if someone could help me get this working. I understand it is going to take some coding, but that is what it is. A forum should not require this much coding to begin with. Why do the roles have to be so confusing? Remember, if normal people cannot understand how to do something by reading the codex, that creates more threads for the more advanced issues.

    I am pretty much a nonprofit organization with my blog and a small forum, I do not have money to pay a developer, so I am asking the wonderful volunteers here to please provide some assistance.

    Thanks.

    @casiepa

    Moderator

    Hi,
    It’s on my list, but I’m fully busy professionally this week. Will try some evening however !
    Pascal.

    @tech55541

    Participant

    Hello,
    It was hard, but I and another member on a private dev forum were able to figure it out. What tripped us up was this.
    https://bbpress.trac.wordpress.org/ticket/2685
    Can’t believe this has not been fixed in core yet.

    Anyway, here is my full solution. All you have to do is 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', 999, 2 );
    
    function ST_add_role_caps_filter( $caps, $role ){
    if( $role == bbp_get_participant_role() || $role == "default" ) {
    
    	$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;
    }

    I suppose this was the correct way to do it @casiepa?

    Thanks.

    @casiepa

    Moderator

    Great find !

    It’s seems to be correct, but need to check the details for the patch you have at the bottom part.

    Pascal.

    @tech55541

    Participant

    Thanks.

    @tech55541

    Participant

    Hello,
    Actually after further testing, my code above for some reason hid the Forums, Topics, and Replies menu from people who had access to view them. Here is an updated piece of code that does not have this problem.

    /*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.
    bbpress.trac.wordpress.org/changeset/5852
    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;
    }

    NOTE: Had to remove links.

    Thanks.

    @bryanwalters

    Participant

    This really good find.

    Thanks for sharing across.

    @tech55541

    Participant

    Sure, anytime. Glad it worked for you.

    @bryanwalters

    Participant

    Pascal,

    I hope we will address this asap.

Viewing 9 replies - 1 through 9 (of 9 total)
  • You must be logged in to reply to this topic.
Skip to toolbar