bp_core_record_activity

Listener function for the logged-in user's 'last_activity' metadata.

Description

(bool|null) bp_core_record_activity(); 

Many functions use a last active feature to show the length of time since the user was last active. This function will update that time as a usermeta setting for the user every 5 minutes while the user is actively browsing the site.

Returns (bool|null)

Returns false if there is nothing to do.


Usage

  1. if ( !function_exists( 'bp_core_record_activity' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'buddypress/bp-core/bp-core-functions.php'; 
  3.  
  4.  
  5. // NOTICE! Understand what this does before running. 
  6. $result = bp_core_record_activity(); 
  7.  

Defined (1)

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

/bp-core/bp-core-functions.php  
  1. function bp_core_record_activity() { 
  2.  
  3. // Bail if user is not logged in. 
  4. if ( ! is_user_logged_in() ) { 
  5. return false; 
  6.  
  7. // Get the user ID. 
  8. $user_id = bp_loggedin_user_id(); 
  9.  
  10. // Bail if user is not active. 
  11. if ( bp_is_user_inactive( $user_id ) ) { 
  12. return false; 
  13.  
  14. // Get the user's last activity. 
  15. $activity = bp_get_user_last_activity( $user_id ); 
  16.  
  17. // Make sure it's numeric. 
  18. if ( ! is_numeric( $activity ) ) { 
  19. $activity = strtotime( $activity ); 
  20.  
  21. // Get current time. 
  22. $current_time = bp_core_current_time( true, 'timestamp' ); 
  23.  
  24. // Use this action to detect the very first activity for a given member. 
  25. if ( empty( $activity ) ) { 
  26.  
  27. /** 
  28. * Fires inside the recording of an activity item. 
  29. * Use this action to detect the very first activity for a given member. 
  30. * @since 1.6.0 
  31. * @param int $user_id ID of the user whose activity is recorded. 
  32. */ 
  33.  
  34. // If it's been more than 5 minutes, record a newer last-activity time. 
  35. if ( empty( $activity ) || ( $current_time >= strtotime( '+5 minutes', $activity ) ) ) { 
  36. bp_update_user_last_activity( $user_id, date( 'Y-m-d H:i:s', $current_time ) );