bp_activity_remove_user_favorite

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

Description

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

Parameters (2)

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

Usage

  1. if ( !function_exists( 'bp_activity_remove_user_favorite' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'buddypress/bp-activity/bp-activity-functions.php'; 
  3.  
  4. // ID of the activity item being unfavorited. 
  5. $activity_id = -1; 
  6.  
  7. // ID of the user unfavoriting the activity item. 
  8. $user_id = -1; 
  9.  
  10. // NOTICE! Understand what this does before running. 
  11. $result = bp_activity_remove_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_remove_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. $my_favs = array_flip( (array) $my_favs ); 
  13.  
  14. // Bail if the user has not previously favorited the item. 
  15. if ( ! isset( $my_favs[ $activity_id ] ) ) { 
  16. return false; 
  17.  
  18. // Remove the fav from the user's favs. 
  19. unset( $my_favs[$activity_id] ); 
  20. $my_favs = array_unique( array_flip( $my_favs ) ); 
  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. if ( ! empty( $fav_count ) ) { 
  25.  
  26. // Deduct from total favorites. 
  27. if ( bp_activity_update_meta( $activity_id, 'favorite_count', (int) $fav_count - 1 ) ) { 
  28.  
  29. // Update users favorites. 
  30. if ( bp_update_user_meta( $user_id, 'bp_favorite_activities', $my_favs ) ) { 
  31.  
  32. /** 
  33. * Fires if bp_update_user_meta() 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 unfavorited. 
  36. * @param int $user_id ID of the user doing the unfavoriting. 
  37. */ 
  38. do_action( 'bp_activity_remove_user_favorite', $activity_id, $user_id ); 
  39.  
  40. // Success. 
  41. return true; 
  42.  
  43. // Error updating. 
  44. } else { 
  45. return false; 
  46.  
  47. // Error updating favorite count. 
  48. } else { 
  49. return false; 
  50.  
  51. // Error getting favorite count. 
  52. } else { 
  53. return false;