Skip to:
Content
Pages
Categories
Search
Top
Bottom

Allow Participants to Trash / own Topics and Posts


  • sally
    Participant

    @sallyruchman

    Hi,

    I’m working on WP 4.7.1 and BBPress Version 2.5.12. I would like to allow Participants to Trash there own Topics and Posts. I found the following code here in the Forum and added it to my functions.php. When i register as Participant and create a new Topic I have only EDIT / REPLY available, but not TRASH.

    
    /*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;
    }
    
    

    Can someone help me with this, or has a other code snippet what is working?

    Thx
    Sally

Viewing 5 replies - 26 through 30 (of 30 total)

  • Robin W
    Moderator

    @robin-w

    ok, this code allows participabts to

    trash their own replies and
    trash their own topics where there have been no replies, once a reply has been posted then they can no longer delete.

    it does display slightly weirdly when you delete a topic, but it is the best you are going to get from me.

    give it a try and see if you still have the admin problems

     /*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_get_participant_role() ) {
    			
    			$newcaps = array(
    
    				// Primary caps
    				'spectate'              => true,
    				'participate'           => true,
    
    				// Forum caps
    				'read_private_forums'   => true,
    
    				// Topic caps
    				'publish_topics'        => true,
    				'edit_topics'           => true,
    				'delete_topics'         => true,
    				'view_trash'			=> true,
    				
    
    				// Reply caps
    				'publish_replies'       => true,
    				'edit_replies'          => true,
    				'delete_replies'        => true,
    				
    				
    				// Topic tag caps
    				'assign_topic_tags'     => true,
    			);
    	
    	return $newcaps;
    	}
    	
        return $caps;
    }
    
    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 );
    
    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 =  $_post->post_type ;
    			
    			
    			// 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 particiapte so allow delete 
                } elseif ( user_can( $user_id, 'participate' ) && ( (int) $user_id === (int) $_post->post_author ) ) {
                 
    			//allow any reply to be deleted
    			if ($post_type == bbp_get_reply_post_type()) $caps  = array('delete_replies') ;
    			
    			//only allow topic delete if there are no replies
    			if ($post_type == bbp_get_topic_post_type()) {
    				$reply_count = bbp_get_topic_reply_count();
    
    				if ( $reply_count == 0)  $caps  = array('delete_topics') ;
    			}
    			
    			// Unknown so do not allow
    			} else {
    				$caps[] = 'do_not_allow';
    			}
    		}
    		
    	}
    	
    	// return the capabilities
    	return $caps;
    	
    }

    sally
    Participant

    @sallyruchman

    Hi Robin,

    thanks so much for the adapted code, all works yet perfect!!!

    Thanks for the Support!

    Best Regards
    Sally


    Robin W
    Moderator

    @robin-w

    no problem – was a fun challenge once I had started !!


    realnsleo
    Participant

    @realnsleo

    Hi Robin,
    I have just tried your code with BBPress 2.5.12 and I can not see the “trash” links. I hope it’s not a lot to ask, but could you know why it might be broken?

    From what I could test, it has something to do with the lines where the capabilities are given, for example this line:

    //allow any reply to be deleted
    if ($post_type == bbp_get_reply_post_type()) $caps = array('delete_replies');

    I appreciate any response.

    Regards,


    Robin W
    Moderator

    @robin-w

    for anyone finding this topic

    I kicked this code around to improve it and also stop the 404 error when a participant trashes a topic, so latest version is

    /*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_get_participant_role() ) {
    			//only change delete topics 
    			$caps ['delete_topics']= true ;
    	}
    	return $caps;
    }
    /*then only allow participants to trash their own topics*/
    add_filter( 'bbp_map_topic_meta_caps', 'ST_tweak_trash_meta_caps', 11, 4 );
    
    function ST_tweak_trash_meta_caps( $caps, $cap, $user_id, $args ){
    	// apply only to delete_topic
    	if ( $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 );
    			
    			// 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 ( user_can( $user_id, 'participate' ) && ( (int) $user_id === (int) $_post->post_author ) ) {
                    $caps      = array();
    				
    			// Unknown so do not allow
    			} else {
    				$caps[] = 'do_not_allow';
    			}
    		}
    	}	
    	// return the capabilities
    	return $caps;
    	}
    
    //then redirect to the forum after trashing topic
    add_action('bbp_template_redirect', 'ST_trash_topic_check', 8);
    
    //check if topic has been trashed by author and show forum if it has
    function ST_trash_topic_check() {
    	$topic_slug = get_option( '_bbp_topic_slug') ;
    	//quick check if we need to do this function, so bail if not a topic
    	if (strpos($_SERVER['REQUEST_URI'], $topic_slug) == FALSE) return ;
    	$forum_slug = bbp_get_root_slug() ;
    	//if check is set (ie we prefix forums with the forum slug) then part 1 will be forum slug and part 2 will be topic slug, if not part 1 will be topic slug
    	$check = bbp_include_root_slug() ;
    	$link = explode('/',$_SERVER['REQUEST_URI']);
    	//next we need the topic id (post id) of the topic so we need to check if it is a topic and if so, find the topic id
    	if (is_user_logged_in() && $check && $link[1] == $forum_slug && $link[2] == $topic_slug ) {
    		$post = bsp_get_page_by_slug( $link[3], OBJECT, 'topic' );
    		$login_check=1 ;
    		} 
    	elseif (is_user_logged_in() && empty($check) && $link[1] === $topic_slug) {
    		$post = bsp_get_page_by_slug( $link[2], OBJECT, 'topic' );
    		$login_check=1 ;
    	}
    	//now we need to check if the topic has been trashed by author
    	if (!empty ($login_check) && $post->post_status == 'trash' && $post->post_author == get_current_user_id() ) {
    		$topic_id =  $post->ID;
    		//then redirect to the forum we came from
    		$forum = bbp_get_forum_permalink (bbp_get_topic_forum_id (  $topic_id )) ;
    		wp_redirect ($forum) ;
    		exit ;
    	}
    	else return ;
    					
    }

    I’ll add this function into my style pack plugin shortly

    bbp style pack

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