Skip to:
Content
Pages
Categories
Search
Top
Bottom

Integrating Buddypress favorite system (aka like system) with BBPress


  • mth75
    Participant

    @mth75

    Hi all,

    I’m trying to integrate the BuddyPress favorite (aka like system) system with BBPress.

    On my site: http://www.starry-night.nl I use the BuddyPress Activity page as homepage, this means that forum topics and replies are
    shown on the activity wall, and can be liked.

    SN Activity (Home) Page

    For easy navigation I made a custom “comment” button which links to the BBPress topic or reply. BBPress activity items (topics or replies)
    can be liked.

    Going to a forum topic of replies of course doesn’t show any likes in the BBPress topic and replies.

    SN Topic/Replies

    Integrating the BuddyPress favorite system with BBPress is at least in my mind the cleanest way and from a functionality point of view, the way
    to go.

    On the BuddyPress forum I found the following code (below) which implements the BuddyPress favorite button in blogpost (and this works), so a rewrite
    should be possible to make this work for BBPress.

    In single.php for blogpost and in the future in the loop-single-reply.php for BBPress.

    <?php echo get_fav_or_unfav_button_for_post( $post ); ?>

    In function.php:

    / Buddypress favorites
    function get_fav_or_unfav_button_for_post( $post ) {
    global $bp, $activities_template;
            // user is not logged ? Show nothing.
    	if ( ! is_user_logged_in() ) {
    	return '';
    	}
    	
    	$activity_id = bp_activity_get_activity_id( array(
    		'user_id' => $post->post_author,
    		'type' => 'new_blog_post',
    		'component' => 'blogs',
    		'item_id' => 1,
    		'secondary_item_id' => $post->ID
    		) );
    	
    	if ( ! $activity_id ) {
    	return '';
    	}
    	
    		bp_has_activities(); // update $activities_template of user's fav
    		$old_value = false;
    		
    		if ( isset( $activities_template->activity->id ) ) {
    		$old_value = $activities_template->activity->id;
    		$activities_template->activity->id = $activity_id;
    		} else {
    		$activities_template->activity = (object) array( 'id' => $activity_id );
    		}
    	
    		// building the template
    	$code = '';
    	$code .= '<div class="activity-meta">'."\n";
    
    		if ( ! bp_get_activity_is_favorite() ) {
    		// if not favorited, add a fav button
    		$code .= ' <a href="'.bp_get_activity_favorite_link( ).'" class="button fav bp-secondary-action" title="'.__( 'Like', 'buddypress' ).'">'.__( 'Like', 'buddyboss' ).'</a>'."\n";
    		
    		} else {
    		
    		// if already favorited, a button to unfav
    		$code .= ' <a href="'.bp_get_activity_unfavorite_link( ).'" class="button unfav bp-secondary-action" title="'.__( 'Unlike', 'buddypress' ).'">'.__( 'Unlike', 'buddyboss' ).'</a>'."\n";
    		
    		// bonus button to show user's all favs
    		$code .= ' <a href="'.bp_loggedin_user_domain() . 'activity/favorites/" class="button unfav bp-secondary-action">'.__( 'My Likes', 'buddyboss' ).'</a>'."\n";
    		}
    		
    		// closing .activity-meta
    	$code .= '</div>'."\n"; 
    
    		if ( false !== $old_value ) {
    		$activities_template->activity->id = $old_value;
    		} else {
    		$activities_template->activity = null;
    		}
    	return $code;
    }

    I’m having problems with rewriting the array for bbpress:

    $activity_id = bp_activity_get_activity_id( array(
    		'user_id' => $post->post_author,
    		'type' => 'new_blog_post',
    		'component' => 'blogs',
    		'item_id' => 1,
    		'secondary_item_id' => $post->ID
    		) );

    Help for figuring this one out is appreciated.

    Best regards,

    Marc

Viewing 8 replies - 1 through 8 (of 8 total)

  • mth75
    Participant

    @mth75

    Solved:

    function get_fav_or_unfav_button_for_bpp( $reply ) {
    global $bp, $activities_template;
            // user is not logged ? Show nothing.
    	if ( ! is_user_logged_in() ) {
    	return '';
    	}
    	
    	$activity_id = (int) get_post_meta( bbp_get_reply_id( $reply_id ), '_bbp_activity_id', true ); 
    	
    	if ( ! $activity_id ) {
    	return '';
    	}
    	
    		bp_has_activities(); // update $activities_template of user's fav
    		$old_value = false;
    		
    		if ( isset( $activities_template->activity->id ) ) {
    		$old_value = $activities_template->activity->id;
    		$activities_template->activity->id = $activity_id;
    		} else {
    		$activities_template->activity = (object) array( 'id' => $activity_id );
    		}
    	
    		// building the template
    	$code = '';
    	$code .= '<div class="activity-like-count">'."\n";
    
    		if ( ! bp_get_activity_is_favorite() ) {
    		// if not favorited, add a fav button
    		$code .= ' <a href="'.bp_get_activity_favorite_link( ).'" class="acomment-like fav-comment bp-secondary-action" title="'.__( 'Like', 'buddypress' ).'">'.__( 'Like', 'buddyboss' ).'</a>'."\n";
    		
    		} else {
    		
    		// if already favorited, a button to unfav
    		$code .= ' <a href="'.bp_get_activity_unfavorite_link( ).'" class="acomment-like unfav-comment bp-secondary-action" title="'.__( 'Unlike', 'buddypress' ).'">'.__( 'Unlike', 'buddyboss' ).'</a>'."\n";
    		
    		}
    		
    		// closing .activity-meta
    	$code .= '</div>'."\n"; 
    
    		if ( false !== $old_value ) {
    		$activities_template->activity->id = $old_value;
    		} else {
    		$activities_template->activity = null;
    		}
    	return $code;
    	
    }
    
    ?>

    Add to loop-single-reply.php for BBPress:

    <?php echo get_fav_or_unfav_button_for_post( reply ); ?> or in the bbp_reply_menu, which i’m still trying to figure out:

    add my own menu option to the bbp-reply-header


    mth75
    Participant

    @mth75

    I order to add a Like option to the BBPress header menu in topics and replies I resorted to this quick solution (not perfect yet):

     <span class="bbp-admin-links">
    		<?php echo get_fav_or_unfav_button_for_bpp(); ?>
     </span>

    Add after <?php bbp_reply_admin_links(); ?> in loop-single-reply.php

    But mission accomplished, the integration of the Buddypress Favorite system with BBPress is complete.

    Buddpress/BBpress Favorites integrated


    mth75
    Participant

    @mth75

    I am using the original Buddyboss Theme, this gives me the chance to show who likes topics and replies (with link to userprofiles) below the content, and some nice extra logic:

    Show who liked


    mth75
    Participant

    @mth75

    Hi Stephen @netweb ,

    Is it possible to integrate the above (functionality) (except the theme dependent functionality) in future BBPress releases. As a user, for me it’s a integration issue between Buddypress & BBpress.

    Best regards,

    Marc

    @mth75 Sure, kind of like how BP’s profiles, if active, replace bbPress’ profiles we could possibly do something similar for BuddyPress favourites.

    Create a ticket on Trac and if you can, take a stab at writing a patch maybe?

    https://bbpress.trac.wordpress.org/newticket


    mth75
    Participant

    @mth75

    @netweb Thx for your reply.

    “Create a ticket on Trac and if you can, take a stab at writing a patch maybe?”

    I will create a ticket, and why not try to do it myself 🙂 .

    Awesome 🙂


    bitlupus
    Participant

    @bitlupus

    Hi all,

    trying to rebuild this case. Also want to make each post “likeable”, but if I use the source code above and paste it at same part in

    Add after <?php bbp_reply_admin_links(); ?> in loop-single-reply.php

    it won’t work… You also said, paste it in functions.php – functions.php for replies?
    Or is there a better (maybe built in) solution created in the past two years?

    thanks a lot

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