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 25 replies - 1 through 25 (of 30 total)

  • Robin W
    Moderator

    @robin-w

    I presume it is not as simple as changing

    /*Customize the BBPress roles to allow Participants to trash topics
    

    to

    /*Customize the BBPress roles to allow Participants to trash topics */

    on line 1, as currently the lack of a */ at the end of the line means that it just comments out all of your first function, so that function doesn’t run !

    Also please note that that ticket bbpress.trac.wordpress.org/ticket/2685 that is referenced is fixed in version 2.6 of bbPress. So for the next version you will no longer need this.

    Pascal.


    sally
    Participant

    @sallyruchman

    Hello,

    thx for the reply.


    @Robin

    I corrected the code on Line 1, but as Participant i still cannot Trash own Posts / Topics. Role has only Edit / Reply available…


    @Pascal

    Is there already an estimated date, when the Version 2.6 will be released?

    Thx
    Regards
    Sally


    Robin W
    Moderator

    @robin-w

    it seems from this trac ticket that even if you have the correct code, it doesn’t work, and won’t until 2.6 is released.


    sally
    Participant

    @sallyruchman

    Thx, when 2.6 is released ??

    Hi Sally,

    As you might have seen, we have moved away from the alpha version and even Beta 1 is replaced with Beta 2. So if really no major issues are found, I suspect (no, I hope!) to see 2.6 in a couple of weeks !
    Pascal.


    sally
    Participant

    @sallyruchman

    Hi Pascal,

    thx, i already installed 2.6 Beta 2. Still have not the possibility as Participant to Trash own Topics / Replys.. Should this issue not been fixed in Version 2.6, or do i have to activate this?

    Thx
    Regards
    Sally

    @sallyruchman, using your code in your original post with the change suggested by @robin-w allows my test user to delete their own topics and replies.

    I’ve saved the code used as a plugin here: https://gist.github.com/ntwb/56ab5a4eab8bbcdc90fc2bdfc2c57838

    Could you please test this again @sallyruchman and confirm te results please


    sally
    Participant

    @sallyruchman

    Hello Stephen,

    thanks for the plugin. I installed the Plugin.

    When i reply to an existing topic, i can trash the reply, works fine.

    When i create a new topic, and afterwards press Trash, the topic is deleted, thats fine, but i get redirected directly to the Homepage afterwards..

    What do i have to adapt ?

    Thx
    Sally


    sally
    Participant

    @sallyruchman

    Also what i found out when testing, When i login as Admin, and enter the Froums Url http://lemontrend.com/forums i get as well redirected to the Homepage.. When deactivate the Plugin, i can access the Url without any problems..

    Regards
    Sally

    Great, so the root issue of this topic is now resolved, awesome +1

    I suspect the redirects are caused by the juggling in the ST_tweak_trash_meta_caps() function, I’d look there, most of that code shouldn’t be required.

    p.s. Note, I only cut and pasted your code to test, I didn’t look closely or test beyond what I needed to test 🙂


    sally
    Participant

    @sallyruchman

    Hi Stephen,

    thanks for the information. What have to be changed to ST_tweak_trash_meta_caps() function ?

    Regards
    Sally


    sally
    Participant

    @sallyruchman

    Hello Stephen,

    can you maybe help me with this?

    Thx
    Sally


    sally
    Participant

    @sallyruchman

    Hello,

    im not a developer.., can someone maybe help me to get the code work?

    Thx
    Regards
    Sally


    sally
    Participant

    @sallyruchman

    Hello,

    can anyone help me with this?

    Thx
    Regards
    Sally


    sally
    Participant

    @sallyruchman

    Anyone ??

    Thx
    Regards
    Sally


    Robin W
    Moderator

    @robin-w

    ok, I’ve taken pity and kicked this round for an hour – I expect to be on your Christmas list !!!

    This seems to work, although please do some testing for all roles, and check it out.

    /*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() ) {
    			
    			$caps = array(
    
    				// Primary caps
    				'spectate'              => true,
    				'participate'           => true,
    
    				// Forum caps
    				'read_private_forums'   => true,
    
    				// Topic caps
    				'publish_topics'        => true,
    				'edit_topics'           => true,
    				'delete_topics'         => true,
    				
    
    				// Reply caps
    				'publish_replies'       => true,
    				'edit_replies'          => true,
    				'delete_replies'        => true,
    
    				// Topic tag caps
    				'assign_topic_tags'     => true,
    			);
    
    	}
    	
        return $caps;
    }
    /*then only allow participants to trash their own topics*/
    
    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 = 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 ( user_can( $user_id, 'participate' ) && ( (int) $user_id === (int) $_post->post_author ) ) {
                    $caps      = array();
    				//$caps[] = $post_type->cap->delete_posts;
    
    			// Unknown so do not allow
    			} else {
    				$caps[] = 'do_not_allow';
    			}
    		}
    	}	
    	// return the capabilities
    	return $caps;
    	}

    Robin W
    Moderator

    @robin-w

    though re-reading the thread this morning, I think I have not fixed anything, juts done it another way.

    and that doesn’t work either – forget the code above


    sally
    Participant

    @sallyruchman

    Hi Robin,

    Thanks for checking the code. Can you help me to get the open issues fixed with the origin code ?

    Best Regards
    Sally


    Robin W
    Moderator

    @robin-w

    ok, whilst out I think I know what the issue is.

    Firstly can you confirm that apart from redirecting you to home, the code at the top of this thread works? ie redirection to the wrong place is the only outstanding issue.

    If so, the issue is that after you delete a topic, bbpress sends you to back to the topic, which as a moderator you are allowed to see trashed topics, but as a partyicipant you are not. This then will send you to a 404 page, which in your case I think just sends you to home.

    So let me look at ways to get around this.


    sally
    Participant

    @sallyruchman

    Yes, the code on top works, only the redirection seems to be the issue..

    Role Admin

    When code is activated, and I login to the Admin Dashboard, and enter the forums link, I get redirected to the homepage, cannot access / view the forums .

    When I deactivate the code, I can access the forums link as Admin..

    Participant

    Delete Topics

    Topics can be trashed and deleted, but after pressing Trash, Participant Role get redirected to homepage

    Delete Reply’s

    Reply’s can be trashed / deleted without redirection, works fine…

    Thx
    Regards
    Sally


    Robin W
    Moderator

    @robin-w

    on the first one, can you clarify that accessing

    dashboard>forums just sends you to the homepage, or can you tell me exactly what actions are needed.


    sally
    Participant

    @sallyruchman

    When i login with the Admin i come to the WP Admin Dashboard. When i open an other browser and paste the url http://domain.com/forums , i get directly redirected to the Homepage, instead to the Forums Link…

    Regards
    Sally


    Robin W
    Moderator

    @robin-w

    deleting replies will redirect to the topic that still exists, so that will work!

    deleting topics redirects to the topic which no longer exists, so gets a 404 error which your site directs to the homepage – not sure how to fix that. A moderator can still view the topic, so that is ok. might be able to do some code with a readtopics capability so that an author can view his own closed topics.

    on admins – Sorry I still don’t quite understand.

    1. so I have a browser tab logged into admin, and showing the dashboard. I then open another tab in the same browser and paste in a forums link and it goes to the homepage – yes ?
    2. if so – I can’t replicate
    3. Why do you need to do this?


    sally
    Participant

    @sallyruchman

    Hi Robin,

    yes, when participant roles delete replies it works. I was thinking, when a participant delete a topic what he creates, and a lot of users reply to this, after he deletes, all the replies are as well deleted, made no sense.

    Is it possible, to take out the possibility for participants to delete the topics? They should be only able to delete the replies of them

    Regarding the Admin Role:

    When i activate the Code in the Dashboard the following happens

    1.) yes, thats correct. With activated code as Admin i cannot manage / access the Forums page and cannot create topics and replies, when logged in.

    To test then, i opened in the same browser a second Tab and pasted the http://domain.com/forums link and refreshed the browser to see if it works there, but i still get redirected to the Homepage (Landing page)…

    Also what is strange, under Dashboard – Admin Menu the Links Forums, Topics and Replies are complete missing …, when deactivating the code they appear again..

    Regards
    Sally

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