bp_activity_update_mention_count_for_user

Update the mention count for a given user.

Description

bp_activity_update_mention_count_for_user( (int) $user_id, (int) $activity_id, (string) $action = 'add' ); 

This function should be used when you've already parsed your activity item for @mentions.

Parameters (3)

0. $user_id (int)
The user id.
1. $activity_id (int)
The unique ID for the activity item.
2. $action — Optional. (string) => 'add'
'delete' or add.. Default: add..

Usage

  1. if ( !function_exists( 'bp_activity_update_mention_count_for_user' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'buddypress/bp-activity/bp-activity-functions.php'; 
  3.  
  4. // The user id. 
  5. $user_id = -1; 
  6.  
  7. // The unique ID for the activity item. 
  8. $activity_id = -1; 
  9.  
  10. // 'delete' or 'add'. Default: 'add'. 
  11. $action = 'add'; 
  12.  
  13. // NOTICE! Understand what this does before running. 
  14. $result = bp_activity_update_mention_count_for_user($user_id, $activity_id, $action); 
  15.  

Defined (1)

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

/bp-activity/bp-activity-functions.php  
  1. function bp_activity_update_mention_count_for_user( $user_id, $activity_id, $action = 'add' ) { 
  2.  
  3. if ( empty( $user_id ) || empty( $activity_id ) ) { 
  4. return false; 
  5.  
  6. // Adjust the mention list and count for the member. 
  7. $new_mention_count = (int) bp_get_user_meta( $user_id, 'bp_new_mention_count', true ); 
  8. $new_mentions = bp_get_user_meta( $user_id, 'bp_new_mentions', true ); 
  9.  
  10. // Make sure new mentions is an array. 
  11. if ( empty( $new_mentions ) ) { 
  12. $new_mentions = array(); 
  13.  
  14. switch ( $action ) { 
  15. case 'delete' : 
  16. $key = array_search( $activity_id, $new_mentions ); 
  17.  
  18. if ( $key !== false ) { 
  19. unset( $new_mentions[$key] ); 
  20.  
  21. break; 
  22.  
  23. case 'add' : 
  24. default : 
  25. if ( !in_array( $activity_id, $new_mentions ) ) { 
  26. $new_mentions[] = (int) $activity_id; 
  27.  
  28. break; 
  29.  
  30. // Get an updated mention count. 
  31. $new_mention_count = count( $new_mentions ); 
  32.  
  33. // Resave the user_meta. 
  34. bp_update_user_meta( $user_id, 'bp_new_mention_count', $new_mention_count ); 
  35. bp_update_user_meta( $user_id, 'bp_new_mentions', $new_mentions ); 
  36.  
  37. return true;