BP_Core_User::get_last_activity( int|array $user_id )

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


Parameters Parameters

$user_id

(int|array) (Required) User IDs or multiple user IDs.


Top ↑

Return Return

(false|array)


Source 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;
	}


Top ↑

User Contributed Notes User Contributed Notes

You must log in before being able to contribute a note or feedback.