BP_Core_User::get_last_activity( int|array $user_id )

Get last activity data for a user or set of users.

Description

Parameters

$user_id

(Required) User IDs or multiple user IDs.

Return

(false|array)

Source

File: bp-core/classes/class-bp-core-user.php

	public static function get_last_activity( $user_id ) {
		global $wpdb;

		// Sanitize and remove empty values.
		$user_ids = array_filter( wp_parse_id_list( $user_id ) );

		if ( empty( $user_ids ) ) {
			return false;
		}

		$uncached_user_ids = bp_get_non_cached_ids( $user_ids, 'bp_last_activity' );
		if ( ! empty( $uncached_user_ids ) ) {
			$bp = buddypress();

			$user_ids_sql = implode( ',', $uncached_user_ids );
			$user_count   = count( $uncached_user_ids );

			$last_activities = $wpdb->get_results( $wpdb->prepare( "SELECT id, user_id, date_recorded FROM {$bp->members->table_name_last_activity} WHERE component = %s AND type = 'last_activity' AND user_id IN ({$user_ids_sql}) LIMIT {$user_count}", $bp->members->id ) );

			foreach ( $last_activities as $last_activity ) {
				wp_cache_set( $last_activity->user_id, array(
					'user_id'       => $last_activity->user_id,
					'date_recorded' => $last_activity->date_recorded,
					'activity_id'   => $last_activity->id,
				), 'bp_last_activity' );
			}
		}

		// Fetch all user data from the cache.
		$retval = array();
		foreach ( $user_ids as $user_id ) {
			$retval[ $user_id ] = wp_cache_get( $user_id, 'bp_last_activity' );

			if ( isset( $retval['user_id'] ) ) {
				$retval[ $user_id ]['user_id']     = (int) $retval[ $user_id ]['user_id'];
			}
			if ( isset( $retval['activity_id'] ) ) {
				$retval[ $user_id ]['activity_id'] = (int) $retval[ $user_id ]['activity_id'];
			}
		}

		return $retval;
	}

Questions?

We're always happy to help with code or other questions you might have! Search our developer docs, contact support, or connect with our sales team.