Skip to:
Content
Pages
Categories
Search
Top
Bottom

Activity Favorite Notification

  • @fanoop

    Participant

    I was hoping there is a way to send a user a notification email (and show in the notifications bar) when a user favorites a an activity or activity comment.

    I searched the web, but was unable to find any documentation on this. I am looking to use the favorite just as Facebook would use a “like”.

Viewing 8 replies - 1 through 8 (of 8 total)
  • @fanoop

    Participant

    Any feedback?

    @transmission

    Participant

    I am also interested in this if anyone has any ideas? As I understand it at the moment marking something as favorite just adds it to a list on your profile?

    @artifex223

    Participant

    Yeah, Favorites is more of a personal bookmarking sort of feature. I believe there are plugins available that will replicate the Facebook “Like” functionality, although I haven’t used any of them myself.

    @fanoop

    Participant

    I did check out some of the “like” plugins, but most of them hadn’t been maintained or didn’t work properly. I was hoping to tailor a core feature to work in a similar, simple way. So far I think what I have done works great, but just need to incorporate notifications.

    I still think that it wold be useful if a user was notified when someone favorited their activity.

    @godavid33

    Participant

    I know this is a very old thread, but here’s some useful code for solving this problem (put it in functions.php) in case someone else stumbles upon it

    
    add_action("bp_activity_add_user_favorite","favorite_notification", 10, 2);
    
    function favorite_notification( $activity_id, $user_id = 0){
    
    	$activities = bp_activity_get_specific( array( 'activity_ids' => $activity_id) );
    
    	bp_core_add_notification( $activity_id, $activities["activities"][0]->user_id, "notifier", "<a href='".get_site_url()."/activity/p/$activity_id'>New Like!</a>" ) ;
    }
    

    @kurtye

    Participant

    @godavid33 where i use this code? bp-functions?

    @aghajoon

    Participant

    this code work for me but java script not work and when user favorite comment user can’t remove notification help me

    define("BP_FAVORITE_NOTIFIER_SLUG","fa_notification");
    
        function bp_favorite_setup_globals() {	
    	global $bp, $current_blog;
        $bp->bp_favorite=new stdClass();
        $bp->bp_favorite->id = 'bp_favorite';
        $bp->bp_favorite->slug = BP_FAVORITE_NOTIFIER_SLUG;
        $bp->bp_favorite->notification_callback = 'bp_favorite_format_notifications';//show the notification   
        $bp->active_components[$bp->bp_favorite->id] = $bp->bp_favorite->id;
    			
                do_action( 'bp_favorite_setup_globals' );
        }
                add_action( 'bp_setup_globals', 'bp_favorite_setup_globals' );
         
    
    function bp_favorite_format_notifications(  $action, $activity_id, $secondary_item_id, $total_items,$format='string'  ) { 
    $action_checker = explode('_', $action);
    $activities = bp_activity_get_specific( array( 'activity_ids' => $activity_id, 'display_comments' => true) );
    	$glue = '';
    	$user_names = array();
    
    	$users = find_favorite_involved_persons($activity_id, $action);
    	$total_user = $count = count($users);
    
    	if($count > 2) {
    		$users = array_slice($users, $count - 2);
    		$count = $count - 2;
    		$glue = ", ";
    	} else if($total_user == 2) {
    		$glue = " and ";
    	}
    
    	foreach((array)$users as $user_id) {
    		$user_names[] = bp_core_get_user_displayname($user_id);
    	}
    
    	if(!empty($user_names)) {
    		$favoriting_users = join($glue, $user_names);
    	}
    
    	switch ( $action ) {
    		case 'new_bp_favorite_'.$activity_id:
    			if($total_user > 2) {
    				$text = $favoriting_users.' and '.$count.' liked: '.substr($activities["activities"][0]->content,0,32).'...';
    			} else {
    				$text = $favoriting_users." like: ".substr($activities["activities"][0]->content,0,32)."...";
    			}
    		break;
    	}
    	$url = '<div id="'.$action.'"class="notification"><a href="#" class="social-delete" onclick="deleteAjaxNotification(\''.$action.'\',\''.$activity_id.'\', \''.admin_url( 'admin-ajax.php' ).'\'); return false;">x</a><span class="social-loader"></span></div>';
    	$link = favorite_activity_get_permalink( $activity_id );
    
    	if($format=='string') {
    		return apply_filters( 'bp_activity_multiple_favorite_notifications', '<a href="' . $link. '">' . $text . '</a>'. $url .'' ,$users, $total_user, $count, $glue, $link );
    	} else {
    		return array(
    			'link' => $link,
    			'text' => $text
    		);
    	}
    	return false;
    
    }
    function find_favorite_involved_persons($activity_id, $action) {
    	global $bp,$wpdb;
    	$table = $wpdb->prefix . 'bp_notifications';
    	return $wpdb->get_col($wpdb->prepare("select DISTINCT(secondary_item_id) from {$table} where item_id=%d and secondary_item_id!=%d and component_action = %s",$activity_id,$bp->loggedin_user->id, $action));
    }
    function favorite_activity_get_permalink( $activity_id, $activity_obj = false ) {
    	global $bp;
    
    	if ( !$activity_obj )
    		$activity_obj = new BP_Activity_Activity( $activity_id );
                        
    		if ( 'activity_comment' == $activity_obj->type )
    			$link = bp_get_activity_directory_permalink(). 'p/' . $activity_obj->item_id . '/';
    		else
    			$link = bp_get_activity_directory_permalink() . 'p/' . $activity_obj->id . '/';
    
    	return apply_filters( 'ac_notifier_activity_get_permalink', $link );
    
    }
    
    function favorite_notifier_remove_notification($activity ,$has_access){
           global $bp;
           if($has_access)		       
    	   bp_core_delete_notifications_for_user_by_type( $bp->loggedin_user->id, $bp->bp_favorite->id, 'new_bp_favorite_'.$activity->id );
    	}
    add_action("bp_activity_screen_single_activity_permalink","favorite_notifier_remove_notification", 10,2);
    
    function favorite_notification( $activity_id){  
                       global $bp;                 
    	               $activities = bp_activity_get_specific( array( 'activity_ids' => $activity_id, 'display_comments' => true) );
    				   $author_id = $activities['activities'][0]->user_id;
                       $user_id =  bp_loggedin_user_id();
    	// if favoriting own activity, dont send notification
    	if( $user_id == $author_id ) {
    		return false;
    	}
    				   if ( bp_is_active( 'notifications' ) ) {
    		bp_notifications_add_notification( array(
    			'user_id'           => $author_id,
    			'item_id'           => $activity_id,
    			'secondary_item_id' => $user_id,
    			'component_name'    => $bp->bp_favorite->id,
    			'component_action'  => 'new_bp_favorite_'.$activity_id,
    			'date_notified'     => bp_core_current_time(),
    			'is_new'            => 1,
    		) );
    	}				
    }
    add_action("bp_activity_add_user_favorite","favorite_notification", 10, 2);
    
    function deleteAjaxNotification(){
        global $bp;        
        bp_core_delete_notifications_by_item_id ($bp->loggedin_user->id, $bp->bp_favorite->id, 'new_bp_favorite_'.$activity->id);     
        die();        
    }	
    add_action('wp_ajax_deleteAjaxNotification', 'deleteAjaxNotification' ); 
    
    function bp_like_add_like_action() {
    global $bp, $activities_template; 
    	 if ( bp_activity_can_favorite() ) : 
    		
    				$my_fav_count = bp_activity_get_meta( bp_get_activity_comment_id(), 'favorite_count' );
    				
    				$my_fav_count = "<span>".$my_fav_count."</span>";
    				
    				$is_favorite = apply_filters( 'bp_get_activity_is_favorite', in_array( bp_get_activity_comment_id(), $activities_template->my_favs ) );
    		
    
    			 if ( !$is_favorite ) : ?>
    
    				<a href="<?php echo apply_filters( 'bp_get_activity_favorite_link', wp_nonce_url( home_url( bp_get_activity_root_slug() . '/favorite/' . bp_get_activity_comment_id() . '/' ), 'mark_favorite' ) ); ?>" class="tooltip-trigger fav bp-secondary-action bp-primary-action" title="<?php _e( 'Favorite', 'buddypress' ); ?>" style="position:relative;"><?php _e( 'Favorite', 'buddypress' ); ?><?php echo $my_fav_count; ?></a>
    
    			<?php else : ?>
    
    				<a href="<?php echo apply_filters( 'bp_get_activity_favorite_link', wp_nonce_url( home_url( bp_get_activity_root_slug() . '/favorite/' . bp_get_activity_comment_id() . '/' ), 'mark_favorite' ) ); ?>" class="tooltip-trigger unfav bp-secondary-action bp-primary-action" title="<?php _e( 'Remove Favorite', 'buddypress' ); ?>" style="position:relative;"><?php _e( 'Remove Favorite', 'buddypress' ); ?><?php echo $my_fav_count; ?></a>
    
    			<?php endif; 
    
    		 endif; 
    }
    add_filter( 'bp_activity_comment_options' , 'bp_like_add_like_action', 1000 );
    
    

    and java code

    function deleteAjaxNotification(action_id, activity_id, adminUrl){
        jQuery('#'+action_id).children(".social-delete").html("");
        jQuery('#'+action_id ).children(".social-loader").show(); 
    
        jQuery.ajax({
            type: 'post',
            url: adminUrl,
            data: { action: "deleteAjaxNotification", action_id:action_id, activity_id:activity_id },
            success:
            function(data) {
            	jQuery('#'+action_id).parent().hide();
            	
            }
         });  
    }

    @danbp

    Participant

    hi @aghajoon,
    don’t jump into old post please !

    Much more convenient, would be to use the buddypress notification manager plugin !

    Works with 4.0 and bp 2.02

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Activity Favorite Notification’ is closed to new replies.
Skip to toolbar