/**
 * Get the last activity for a given user.
 *
 * @since 1.9.0
 *
 * @param int $user_id The ID of the user.
 * @return string Time of last activity, in 'Y-m-d H:i:s' format, or an empty
 *                string if none is found.
 */
function bp_get_user_last_activity($user_id = 0)
{
    $activity = '';
    $last_activity = BP_Core_User::get_last_activity($user_id);
    if (!empty($last_activity[$user_id])) {
        $activity = $last_activity[$user_id]['date_recorded'];
    }
    /**
     * Filters the last activity for a given user.
     *
     * @since 1.9.0
     *
     * @param string $activity Time of last activity, in 'Y-m-d H:i:s' format or
     *                         an empty string if none found.
     * @param int    $user_id  ID of the user being checked.
     */
    return apply_filters('bp_get_user_last_activity', $activity, $user_id);
}
 /**
  * Perform a database query to populate any extra metadata we might need.
  *
  * Different components will hook into the 'bp_user_query_populate_extras'
  * action to loop in the things they want.
  *
  * @since 1.7.0
  *
  * @global WPDB $wpdb Global WordPress database access object.
  */
 public function populate_extras()
 {
     global $wpdb;
     // Bail if no users.
     if (empty($this->user_ids) || empty($this->results)) {
         return;
     }
     // Bail if the populate_extras flag is set to false
     // In the case of the 'popular' sort type, we force
     // populate_extras to true, because we need the friend counts.
     if ('popular' == $this->query_vars['type']) {
         $this->query_vars['populate_extras'] = 1;
     }
     if (!(bool) $this->query_vars['populate_extras']) {
         return;
     }
     // Turn user ID's into a query-usable, comma separated value.
     $user_ids_sql = implode(',', wp_parse_id_list($this->user_ids));
     /**
      * Allows users to independently populate custom extras.
      *
      * Note that anything you add here should query using $user_ids_sql, to
      * avoid running multiple queries per user in the loop.
      *
      * Two BuddyPress components currently do this:
      * - XProfile: To override display names.
      * - Friends:  To set whether or not a user is the current users friend.
      *
      * @see bp_xprofile_filter_user_query_populate_extras()
      * @see bp_friends_filter_user_query_populate_extras()
      *
      * @since 1.7.0
      *
      * @param BP_User_Query $this         Current BP_User_Query instance.
      * @param string        $user_ids_sql Comma-separated string of user IDs.
      */
     do_action_ref_array('bp_user_query_populate_extras', array($this, $user_ids_sql));
     // Fetch last_active data from the activity table.
     $last_activities = BP_Core_User::get_last_activity($this->user_ids);
     // Set a last_activity value for each user, even if it's empty.
     foreach ($this->results as $user_id => $user) {
         $user_last_activity = isset($last_activities[$user_id]) ? $last_activities[$user_id]['date_recorded'] : '';
         $this->results[$user_id]->last_activity = $user_last_activity;
     }
     // Fetch usermeta data
     // We want the three following pieces of info from usermeta:
     // - friend count
     // - latest update.
     $total_friend_count_key = bp_get_user_meta_key('total_friend_count');
     $bp_latest_update_key = bp_get_user_meta_key('bp_latest_update');
     // Total_friend_count must be set for each user, even if its
     // value is 0.
     foreach ($this->results as $uindex => $user) {
         $this->results[$uindex]->total_friend_count = 0;
     }
     // Create, prepare, and run the separate usermeta query.
     $user_metas = $wpdb->get_results($wpdb->prepare("SELECT user_id, meta_key, meta_value FROM {$wpdb->usermeta} WHERE meta_key IN (%s,%s) AND user_id IN ({$user_ids_sql})", $total_friend_count_key, $bp_latest_update_key));
     // The $members_template global expects the index key to be different
     // from the meta_key in some cases, so we rejig things here.
     foreach ($user_metas as $user_meta) {
         switch ($user_meta->meta_key) {
             case $total_friend_count_key:
                 $key = 'total_friend_count';
                 break;
             case $bp_latest_update_key:
                 $key = 'latest_update';
                 break;
         }
         if (isset($this->results[$user_meta->user_id])) {
             $this->results[$user_meta->user_id]->{$key} = $user_meta->meta_value;
         }
     }
     // When meta_key or meta_value have been passed to the query,
     // fetch the resulting values for use in the template functions.
     if (!empty($this->query_vars['meta_key'])) {
         $meta_sql = array('select' => "SELECT user_id, meta_key, meta_value", 'from' => "FROM {$wpdb->usermeta}", 'where' => $wpdb->prepare("WHERE meta_key = %s", $this->query_vars['meta_key']));
         if (false !== $this->query_vars['meta_value']) {
             $meta_sql['where'] .= $wpdb->prepare(" AND meta_value = %s", $this->query_vars['meta_value']);
         }
         $metas = $wpdb->get_results("{$meta_sql['select']} {$meta_sql['from']} {$meta_sql['where']}");
         if (!empty($metas)) {
             foreach ($metas as $meta) {
                 if (isset($this->results[$meta->user_id])) {
                     $this->results[$meta->user_id]->meta_key = $meta->meta_key;
                     if (!empty($meta->meta_value)) {
                         $this->results[$meta->user_id]->meta_value = $meta->meta_value;
                     }
                 }
             }
         }
     }
 }
    /**
     * Get the last active date of many users at once.
     *
     * @todo Why is this in the Friends component?
     *
     * @param array $user_ids IDs of users whose last_active meta is
     *                        being queried.
     *
     * @return array $retval Array of last_active values + user_ids.
     */
    public static function get_bulk_last_active($user_ids)
    {
        global $wpdb;
        $last_activities = BP_Core_User::get_last_activity($user_ids);
        // Sort and structure as expected in legacy function
        usort($last_activities, create_function('$a, $b', '
			if ( $a["date_recorded"] == $b["date_recorded"] ) {
				return 0;
			}

			return ( strtotime( $a["date_recorded"] ) < strtotime( $b["date_recorded"] ) ) ? 1 : -1;
		'));
        $retval = array();
        foreach ($last_activities as $last_activity) {
            $u = new stdClass();
            $u->last_activity = $last_activity['date_recorded'];
            $u->user_id = $last_activity['user_id'];
            $retval[] = $u;
        }
        return $retval;
    }
Esempio n. 4
0
/**
 * Get the last activity for a given user.
 *
 * @param int $user_id The ID of the user.
 * @return string Time of last activity, in 'Y-m-d H:i:s' format, or an empty
 *         string if none is found.
 */
function bp_get_user_last_activity($user_id = 0)
{
    $activity = '';
    $last_activity = BP_Core_User::get_last_activity($user_id);
    if (!empty($last_activity[$user_id])) {
        $activity = $last_activity[$user_id]['date_recorded'];
    }
    return apply_filters('bp_get_user_last_activity', $activity, $user_id);
}
 /**
  * @group last_activity
  */
 public function test_delete_last_activity()
 {
     $u = $this->factory->user->create();
     $time = bp_core_current_time();
     BP_Core_User::update_last_activity($u, $time);
     $a = BP_Core_User::get_last_activity($u);
     $found = isset($a[$u]['date_recorded']) ? $a[$u]['date_recorded'] : '';
     $this->assertEquals($time, $found);
     BP_Core_User::delete_last_activity($u);
     $a = BP_Core_User::get_last_activity($u);
     $found = isset($a[$u]['date_recorded']) ? $a[$u]['date_recorded'] : '';
     $this->assertEquals('', $found);
 }