Skip to:
Content
Pages
Categories
Search
Top
Bottom

Allow participants to trash their own topic and replies?


  • Leonyipa
    Participant

    @leonyipa

    How to Allow participants to “trash” their own topic and replies?
    like keymaster did?

    I only want them to be able to trash, but not delete it.

    Thanks!

Viewing 25 replies - 1 through 25 (of 28 total)

  • Robin W
    Moderator

    @robin-w

    long answer I’m afraid and in two parts

    part 1 – setting the user permission to allow delete topic and delete reply

    delete is easy, making it only trash is in part 2 !

    Basically you’ll need to change a couple of capabilities of the participant role

    see

    https://codex.bbpress.org/bbpress-user-roles-and-capabilities/

    you see the abilities delete topics and delete replies (as distinct from delete others topics and delete others replies)

    Whilst you could amend the participant role, I haven’t documented that, so easiest is just for you to create a new role and give it the participant capabilities plus the ‘delete topics’ and ‘delete replies’. This article explains how

    https://codex.bbpress.org/custom-capabilities/

    add this code to your functions file – see

    https://codex.bbpress.org/functions-files-and-child-themes-explained/

    part 2 – changing the function, so that delete is only shown for keymasters

    This code will only allow a keymaster to permanently delete a topic

    add the following to your functions file

    add_filter ('bbp_get_topic_trash_link', 'topic_dont_delete');
    add_filter ('bbp_get_reply_trash_link', 'reply_dont_delete');
    
    function topic_dont_delete( $args = '') {
    
    		// Parse arguments against default values
    		$r = bbp_parse_args( $args, array(
    			'id'           => 0,
    			'link_before'  => '',
    			'link_after'   => '',
    			'sep'          => ' | ',
    			'trash_text'   => esc_html__( 'Trash',   'bbpress' ),
    			'restore_text' => esc_html__( 'Restore', 'bbpress' ),
    			'delete_text'  => esc_html__( 'Delete',  'bbpress' )
    		), 'get_topic_trash_link' );
    
    		$actions = array();
    		$topic   = bbp_get_topic( bbp_get_topic_id( (int) $r['id'] ) );
    
    		if ( empty( $topic ) || !current_user_can( 'delete_topic', $topic->ID ) ) {
    			return;
    		}
    
    		if ( bbp_is_topic_trash( $topic->ID ) ) {
    			$actions['untrash'] = '<a title="' . esc_attr__( 'Restore this item from the Trash', 'bbpress' ) . '" href="' . esc_url( wp_nonce_url( add_query_arg( array( 'action' => 'bbp_toggle_topic_trash', 'sub_action' => 'untrash', 'topic_id' => $topic->ID ) ), 'untrash-' . $topic->post_type . '_' . $topic->ID ) ) . '" class="bbp-topic-restore-link">' . $r['restore_text'] . '</a>';
    		} elseif ( EMPTY_TRASH_DAYS ) {
    			$actions['trash']   = '<a title="' . esc_attr__( 'Move this item to the Trash',      'bbpress' ) . '" href="' . esc_url( wp_nonce_url( add_query_arg( array( 'action' => 'bbp_toggle_topic_trash', 'sub_action' => 'trash',   'topic_id' => $topic->ID ) ), 'trash-'   . $topic->post_type . '_' . $topic->ID ) ) . '" class="bbp-topic-trash-link">'   . $r['trash_text']   . '</a>';
    		}
    		
    		if ( bbp_is_topic_trash( $topic->ID ) || !EMPTY_TRASH_DAYS ) {
    		if ( bbp_is_user_keymaster()) {
    		$actions['delete']  = '<a title="' . esc_attr__( 'Delete this item permanently',     'bbpress' ) . '" href="' . esc_url( wp_nonce_url( add_query_arg( array( 'action' => 'bbp_toggle_topic_trash', 'sub_action' => 'delete',  'topic_id' => $topic->ID ) ), 'delete-'  . $topic->post_type . '_' . $topic->ID ) ) . '" onclick="return confirm(\'' . esc_js( __( 'Are you sure you want to delete that permanently?', 'bbpress' ) ) . '\' );" class="bbp-topic-delete-link">' . $r['delete_text'] . '</a>';
    		}
    		}
    
    				// Process the admin links
    		$retval = $r['link_before'] . implode( $r['sep'], $actions ) . $r['link_after'];
    
    		return $retval ;
    	}
    	
    	function reply_dont_delete( $args = '' ) {
    
    		// Parse arguments against default values
    		$r = bbp_parse_args( $args, array(
    			'id'           => 0,
    			'link_before'  => '',
    			'link_after'   => '',
    			'sep'          => ' | ',
    			'trash_text'   => esc_html__( 'Trash',   'bbpress' ),
    			'restore_text' => esc_html__( 'Restore', 'bbpress' ),
    			'delete_text'  => esc_html__( 'Delete',  'bbpress' )
    		), 'get_reply_trash_link' );
    
    		$actions = array();
    		$reply   = bbp_get_reply( bbp_get_reply_id( (int) $r['id'] ) );
    
    		if ( empty( $reply ) || !current_user_can( 'delete_reply', $reply->ID ) ) {
    			return;
    		}
    
    		if ( bbp_is_reply_trash( $reply->ID ) ) {
    			$actions['untrash'] = '<a title="' . esc_attr__( 'Restore this item from the Trash', 'bbpress' ) . '" href="' . esc_url( wp_nonce_url( add_query_arg( array( 'action' => 'bbp_toggle_reply_trash', 'sub_action' => 'untrash', 'reply_id' => $reply->ID ) ), 'untrash-' . $reply->post_type . '_' . $reply->ID ) ) . '" class="bbp-reply-restore-link">' . $r['restore_text'] . '</a>';
    		} elseif ( EMPTY_TRASH_DAYS ) {
    			$actions['trash']   = '<a title="' . esc_attr__( 'Move this item to the Trash',      'bbpress' ) . '" href="' . esc_url( wp_nonce_url( add_query_arg( array( 'action' => 'bbp_toggle_reply_trash', 'sub_action' => 'trash',   'reply_id' => $reply->ID ) ), 'trash-'   . $reply->post_type . '_' . $reply->ID ) ) . '" class="bbp-reply-trash-link">'   . $r['trash_text']   . '</a>';
    		}
    
    		if ( bbp_is_reply_trash( $reply->ID ) || !EMPTY_TRASH_DAYS ) {
    		if ( bbp_is_user_keymaster()) {
    		$actions['delete']  = '<a title="' . esc_attr__( 'Delete this item permanently',     'bbpress' ) . '" href="' . esc_url( wp_nonce_url( add_query_arg( array( 'action' => 'bbp_toggle_reply_trash', 'sub_action' => 'delete',  'reply_id' => $reply->ID ) ), 'delete-'  . $reply->post_type . '_' . $reply->ID ) ) . '" onclick="return confirm(\'' . esc_js( __( 'Are you sure you want to delete that permanently?', 'bbpress' ) ) . '\' );" class="bbp-reply-delete-link">' . $r['delete_text'] . '</a>';
    		}
    		}
    
    		// Process the admin links
    		$retval = $r['link_before'] . implode( $r['sep'], $actions ) . $r['link_after'];
    
    		return $retval ;
    	}
    

    I haven’t fully tested this code as my test site is in some disarray whilst I test something else, so you’ll need to check that it works


    Leonyipa
    Participant

    @leonyipa

    hi, sorry for the late reply.

    I tried both ways:

    1) changing the Participant role directly

    // Participant/Default
    case bbp_get_participant_role() :
    default :
    $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,
    );

    break;
    }

    after I changed it, nothing happened :/

    and
    2) making custom role, but it says:

    Parse error: syntax error, unexpected ‘roles’ (T_STRING), expecting ‘(‘ in /home/gleam/public_html/wp-content/plugins/bbpress/includes/core/capabilities.php on line 216

    
    216: function add_new roles( $bbp_roles )
    217: {

    Robin W
    Moderator

    @robin-w

    you shouldn’t be changing directly !

    can you come back and post your code for 2, you’ve just missed a “‘” somewhere I expect.


    Leonyipa
    Participant

    @leonyipa

    yes, sorry for the late reply,
    where did I miss?


    Robin W
    Moderator

    @robin-w

    can you come back and post your code for 2

    that’s why I need your to post your code, so that I can work out where it’s wrong :-)


    Leonyipa
    Participant

    @leonyipa

    Sorry for the late reply,

    why changing directly doesn’t work? I am a bit confused sorry

    yes, I will paste the code


    Leonyipa
    Participant

    @leonyipa

    My Full Code in /public_html/wp-content/plugins/bbpress/includes/core/capabilitiese.php

    http://pastebin.com/bdisb0Bb


    Robin W
    Moderator

    @robin-w

    why changing directly doesn’t work?

    because on every software update you will lose your changes!

    My Full Code

    sorry but pastebin adds millions of blank lines, so without re-editing the file I can’t see what you are doing.

    If you go back to my original instructions and follow these and get an error, then come back and I’ll try and help you further

    or if you go back to your

    2) making custom role, but it says:

    “ Parse error: syntax error, unexpected ‘roles’ (T_STRING), expecting ‘(‘ in /home/gleam/public_html/wp-content/plugins/bbpress/includes/core/capabilities.php on line 216

    216: function add_new roles( $bbp_roles )
    217: {

    and let me have lines 210 to 220 I’ll try and sort the error


    Leonyipa
    Participant

    @leonyipa

    yes, I followed your instructions, and when I saw:
    https://codex.bbpress.org/custom-capabilities/

    I followed it and copy-and-pasted the code into my capabilities.php

    however, it still show me the error message.

    sorry but pastebin adds millions of blank lines, so without re-editing the file I can’t see what you are doing.

    Does it work if using RAW Paste Data?


    Robin W
    Moderator

    @robin-w

    Apologies was trying to answer many threads with guests due for dinner, so prevouis answer was a bit short !

    I followed it and copy-and-pasted the code into my capabilities.php

    ahhh, now I understand, as per original thread you need to add that code to your functions.php, not to capabilities.php, and that would explain the error.

    Whilst you could amend the participant role, I haven’t documented that, so easiest is just for you to create a new role and give it the participant capabilities plus the ‘delete topics’ and ‘delete replies’. This article explains how

    https://codex.bbpress.org/custom-capabilities/

    add this code to your functions file – see

    https://codex.bbpress.org/functions-files-and-child-themes-explained/


    Leonyipa
    Participant

    @leonyipa

    Thanks very much for the quick response, I will try it right away!


    Leonyipa
    Participant

    @leonyipa

    now I added the code in functions.php
    but the error message still appear:

    Parse error: syntax error, unexpected T_STRING, expecting ‘(‘ in /home/u639474582/public_html/wp-content/plugins/bbpress/includes/core/functions.php on line 168

    My Full Code:
    http://pastebin.com/75iu9ijH


    Robin W
    Moderator

    @robin-w

    [ignore this post]


    Robin W
    Moderator

    @robin-w

    ok, I’ve worked out that error

    You have two issues

    1. the code is slightly wrong, I’ve corrected it in the documentation, but you need to change your line 168 from

    function add_new roles( $bbp_roles )
    

    to

    function add_new_roles( $bbp_roles )

    ie an underline between ‘new’ and ‘roles’

    BUT MORE IMPORTANTLY

    2. you have now put the code in a core functions file of bbpress, and it cannot run there as it calls code that has not yet been loaded.

    The code needs to go into your THEME’s functions.php file, not any functions file with the PLUGIN bbpress.

    Please read again

    https://codex.bbpress.org/functions-files-and-child-themes-explained/


    swammy05
    Participant

    @swammy05

    Robin, I’ve followed these steps (and several other of your support responses on this issue), and I’ve successfully created new roles using both methods, but I have a strange symptom: No matter what capabilities I allow, only the ‘Edit’ link shows on topics and replies. I simplified it to a new role, modeled after a keymaster. The role shows up, the database capabilities shows correctly, and now keymaster-only data shows under user profile images (like IP address), but still no admin links such as move, trash, etc. If I switch the user back to a regular moderator role, the links show just fine:

    function add_custom_role( $bbp_roles ) {
      
    $bbp_roles['villager'] = array(
    'name' => 'Villager',
    'capabilities' => bbp_get_caps_for_role( bbp_get_keymaster_role() ) // the same capabilities as participants
    );
    return $bbp_roles;
    }
    add_filter( 'bbp_get_dynamic_roles', 'add_custom_role', 1 );

    swammy05
    Participant

    @swammy05

    To add a bit more context:

    If I add ‘delete_topics’ capability, I do not see the ‘trash’ link in the admin links, even on my topics.
    If I add ‘delete_others_topics’, the trash link does show on all topics. Same for replies


    Robin W
    Moderator

    @robin-w

    can you humour me and try

    Plugins

    Deactivate all but bbpress and see if this fixes. if it does, re-enable one at a time to see which is causing the error.

    Themes

    If plugins don’t pinpoint the problem, as a test switch to a default theme such as twentytwelve, and see if this fixes.


    swammy05
    Participant

    @swammy05

    Thanks for jumping back into this old thread, Robin.

    I’ll have to wait until the middle of the night to try that, as it’s an active community with a lot of users. So you think this is an error caused by a plugin despite no warnings?

    The only plugin that I can imagine would touch this behavior is ‘paid memberships pro’, which is scary to deactivate, at the risk of losing data.

    Anything else I can try to debug/test for you?


    Robin W
    Moderator

    @robin-w

    Ok, no don’t bother – I was not really expecting it to change anything, just wanted to eliminate before jumping into lots of code, but since it will be a hassle, we’ll leave that for the moment and I’ve now looked at the raw code !!

    ok, the template it is using is loop-single-reply which then callsbbp_reply_admin_links . this then checks whether it is a topic or reply and for a reply continues and then calls each of the elements in turn

    so for trash it calls

    bbp_get_reply_trash_link and checks that the user can ‘delete_reply’ before posting that link.

    For a topic it jumps to

    bbp_get_topic_admin_links and checks that the user can ‘delete_topic’ before posting that link.

    NOW I guessing (and probably wrongly, but am short of time tonight so will get you to do more work!) that the codex or this function might be wrong and you should be giving a delete_topic singular rather than delete_topics so give that go.

    If that works (and I’ll be very surprise if it does!), then you’ll need the filter much earlier to prevent permanent deletion.

    Otherwise I’ll take another look tomorrow !


    swammy05
    Participant

    @swammy05

    Thank you again, Robin. I’ll followup with my findings.

    I did switch the theme quickly (twenty twelve), and I noticed two things:

    My role no longer displayed as ‘villager’ but instead switched back to ‘participant’, and the ‘trash’ links were never present. Not sure why the role would have been changed with a theme switch, but there ya go.


    Robin W
    Moderator

    @robin-w

    My role no longer displayed as ‘villager’ but instead switched back to ‘participant’, and the ‘trash’ links were never present

    That’ll be because the code you’ve changed is in the functions file of your theme?!, so goes when you switch to a another theme.


    swammy05
    Participant

    @swammy05

    Yes, that makes sense.

    I did find this: https://bbpress.trac.wordpress.org/ticket/2685

    Is that a core fix that has not been made? My bbpress is up to date


    Robin W
    Moderator

    @robin-w

    That looks suspiciously like the problem and the solution! and as it’s recent it’s not in the core.

    I’d suggest you change the core and see if it works.

    If it does, then make a note, and check on the next update that it’s been incorporated.

    Changing the core of a plugin is only a problem because it will probably be overwritten on next upgrade. You could write a couple of filters (the capability is there) if you’re worried about changing core.


    swammy05
    Participant

    @swammy05

    worked like a charm. Thank you!


    Robin W
    Moderator

    @robin-w

    great – hopefully it will be a permanent fix soon !

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