WP_HTTP_Polling_Sync_Server::process_awareness_updateprivateWP 7.0.0

Processes and stores an awareness update from a client.

Method of the class: WP_HTTP_Polling_Sync_Server{}

No Hooks.

Returns

Array. array<string, mixed>> Map of client ID to awareness state.

Usage

// private - for code of main (parent) class only
$result = $this->process_awareness_update( $room, $client_id, ?array $awareness_update ): array;
$room(string) (required)
Room identifier.
$client_id(int) (required)
Client identifier.
?array $awareness_update(required)
.

Changelog

Since 7.0.0 Introduced.

WP_HTTP_Polling_Sync_Server::process_awareness_update() code WP 7.0

private function process_awareness_update( string $room, int $client_id, ?array $awareness_update ): array {
	$existing_awareness = $this->storage->get_awareness_state( $room );
	$updated_awareness  = array();
	$current_time       = time();

	foreach ( $existing_awareness as $entry ) {
		// Remove this client's entry (it will be updated below).
		if ( $client_id === $entry['client_id'] ) {
			continue;
		}

		// Remove entries that have expired.
		if ( $current_time - $entry['updated_at'] >= self::AWARENESS_TIMEOUT ) {
			continue;
		}

		$updated_awareness[] = $entry;
	}

	// Add this client's awareness state.
	if ( null !== $awareness_update ) {
		$updated_awareness[] = array(
			'client_id'  => $client_id,
			'state'      => $awareness_update,
			'updated_at' => $current_time,
			'wp_user_id' => get_current_user_id(),
		);
	}

	// This action can fail, but it shouldn't fail the entire request.
	$this->storage->set_awareness_state( $room, $updated_awareness );

	// Convert to client_id => state map for response.
	$response = array();
	foreach ( $updated_awareness as $entry ) {
		$response[ $entry['client_id'] ] = $entry['state'];
	}

	return $response;
}