BuddyDev

Search

[Resolved] Testimonials one time only

  • Participant
    Level: Initiated
    Posts: 8
    jan hansen on #2097

    Hi again.

    1.
    Is it possible to extend a hook that makes it possible only to leave 1 testimonial on a profile in buddypress. So that if you have already done it once, you can’t do it again unless the testimonial is deleted.

    2.
    Also could you please advise on the exact code in order to make the settings so that:
    only admin and editor role can delete a testimonial.
    User & admin can approve a testimonial

    Thanx so much in advance
    Jan

  • Keymaster
    (BuddyDev Team)
    Posts: 24212
    Brajesh Singh on #2100

    Hi Jan,
    Can you please put this code in your bp-custom.php and see if it works as expected

    
    
    ////
    //For Testimonials
    
    add_filter( 'bp_testimonial_user_can_delete', 'bp_testimonial_custom_delete_permission' );
    
    function bp_testimonial_custom_delete_permission( $can_delete ) {
    	
    	if ( ! current_user_can( 'publish_pages' ) ) {
    		$can_delete = false;
    	}
    	
    	return $can_delete;
    	
    }
    
    add_filter( 'bp_testimonial_user_can_write', 'bp_testimonial_custom_write_permission' );
    
    function bp_testimonial_custom_write_permission( $can_write ) {
    	
    	if ( ! is_user_logged_in() || bp_is_my_profile() ) {
    		return $can_write;
    	}
    	
    	$displayed_user_id = bp_displayed_user_id();
    	
    	$user_id = get_current_user_id();
    	
    	if ( ! $displayed_user_id || ! $user_id ) {
    		return $can_write;
    	}
    	
    	if( bpt_has_user_already_written( $user_id, $displayed_user_id  ) ) {
    		$can_write = false;
    	}
    	
    	return $can_write;
    	//all good, get post id
    	
    }
    
    function bpt_has_user_already_written( $user_id , $other_id ) {
    	
    	$post_id = get_user_meta( $other_id, 'associated_testimonial_page', true );
    	
    	if ( ! $post_id ) {
    		return false;//should never happen
    	}
    	
    	//Now check if the user has a comment on this post
    	
    	global $wpdb;
    	
    	$has_comment = $wpdb->get_var( $wpdb->prepare( "SELECT comment_ID FROM {$wpdb->comments} WHERE comment_post_ID = %d AND user_id = %d", $post_id, $user_id ) );
    	
    	if( $has_comment ) {
    		return true;
    	}
    	
    	return false;
    }
    

    Thank you
    Brajesh

  • Participant
    Level: Initiated
    Posts: 8
    jan hansen on #2108

    Dear Brajesh once again thank you for quick reply.

    I put the bp-custom.php file in the root of the testimonial plugin folder, is that correct or should it be in the root folder for all plugins?

    It can still write several testimonials on a profile even though there is already one from the same user. Only difference is that the receiving user has to approve every testimonial, before shown public. That could work since the user then just deny to approve more testimonials from the same user, but would be luxury if they didn’t have to admin that and needed to remember who already did write earlier, so it automatic denied the option to post if you already had an approved testimonial on that profile.

    The writer of a testimonial can’t delete after approved – OK
    The receiver can still delete after approved – Could that be so only ADMIN can delete testimonials. (to prevent honest criticism to be deleted by receiver)

    once again thank you for great support and feedback.

    /Jan

  • Keymaster
    (BuddyDev Team)
    Posts: 24212
    Brajesh Singh on #2136

    Hi Jan,
    I am sorry for the delayed reply.

    Please create a file in wp-content/plugins directory(If the file does not exist) and name it bp-custom.php. This file is loaded by BuddyPress before anything.

    Please give it a try then and let me know how it goes.

    Thank you
    Brajesh

  • Participant
    Level: Initiated
    Posts: 8
    jan hansen on #2148

    Thank you, worked like a charm!

    Very nice Brajesh!

    Merry Christmas and thank you for great work!

    Cheers
    Jan

  • Keymaster
    (BuddyDev Team)
    Posts: 24212
    Brajesh Singh on #2154

    Thank you Jan.
    Glad it worked.
    Merry Christmas and happy holiday 🙂

    Cheers
    Brajesh

The topic ‘ [Resolved] Testimonials one time only’ is closed to new replies.

This topic is: resolved