bp_activity_add_user_favorite

Add an activity stream item as a favorite for a user.

Description

bp_activity_add_user_favorite( (int) $activity_id, (int) $user_id = 0 ); 

Parameters (2)

0. $activity_id (int)
ID of the activity item being favorited.
1. $user_id — Optional. (int)
ID of the user favoriting the activity item.

Usage

  1. if ( !function_exists( 'bp_activity_add_user_favorite' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'buddypress/bp-activity/bp-activity-functions.php'; 
  3.  
  4. // ID of the activity item being favorited. 
  5. $activity_id = -1; 
  6.  
  7. // ID of the user favoriting the activity item. 
  8. $user_id = -1; 
  9.  
  10. // NOTICE! Understand what this does before running. 
  11. $result = bp_activity_add_user_favorite($activity_id, $user_id); 
  12.  

Defined (1)

The function is defined in the following location(s).

/bp-activity/bp-activity-functions.php  
  1. function bp_activity_add_user_favorite( $activity_id, $user_id = 0 ) { 
  2.  
  3. // Favorite activity stream items are for logged in users only. 
  4. if ( ! is_user_logged_in() ) { 
  5. return false; 
  6.  
  7. // Fallback to logged in user if no user_id is passed. 
  8. if ( empty( $user_id ) ) { 
  9. $user_id = bp_loggedin_user_id(); 
  10.  
  11. $my_favs = bp_get_user_meta( $user_id, 'bp_favorite_activities', true ); 
  12. if ( empty( $my_favs ) || ! is_array( $my_favs ) ) { 
  13. $my_favs = array(); 
  14.  
  15. // Bail if the user has already favorited this activity item. 
  16. if ( in_array( $activity_id, $my_favs ) ) { 
  17. return false; 
  18.  
  19. // Add to user's favorites. 
  20. $my_favs[] = $activity_id; 
  21.  
  22. // Update the total number of users who have favorited this activity. 
  23. $fav_count = bp_activity_get_meta( $activity_id, 'favorite_count' ); 
  24. $fav_count = !empty( $fav_count ) ? (int) $fav_count + 1 : 1; 
  25.  
  26. // Update user meta. 
  27. bp_update_user_meta( $user_id, 'bp_favorite_activities', $my_favs ); 
  28.  
  29. // Update activity meta counts. 
  30. if ( bp_activity_update_meta( $activity_id, 'favorite_count', $fav_count ) ) { 
  31.  
  32. /** 
  33. * Fires if bp_activity_update_meta() for favorite_count is successful and before returning a true value for success. 
  34. * @since 1.2.1 
  35. * @param int $activity_id ID of the activity item being favorited. 
  36. * @param int $user_id ID of the user doing the favoriting. 
  37. */ 
  38. do_action( 'bp_activity_add_user_favorite', $activity_id, $user_id ); 
  39.  
  40. // Success. 
  41. return true; 
  42.  
  43. // Saving meta was unsuccessful for an unknown reason. 
  44. } else { 
  45.  
  46. /** 
  47. * Fires if bp_activity_update_meta() for favorite_count is unsuccessful and before returning a false value for failure. 
  48. * @since 1.5.0 
  49. * @param int $activity_id ID of the activity item being favorited. 
  50. * @param int $user_id ID of the user doing the favoriting. 
  51. */ 
  52. do_action( 'bp_activity_add_user_favorite_fail', $activity_id, $user_id ); 
  53.  
  54. return false;