WP_Sync_Post_Meta_Storage::get_updates_after_cursor( string $room, int $cursor ): array<int,

In this article

Retrieves sync updates from a room after the given cursor.

Parameters

$roomstringrequired
Room identifier.
$cursorintrequired
Return updates after this cursor (meta_id).

Return

array<int, mixed> Sync updates.

Source

public function get_updates_after_cursor( string $room, int $cursor ): array {
	global $wpdb;

	$post_id = $this->get_storage_post_id( $room );
	if ( null === $post_id ) {
		$this->room_cursors[ $room ]       = 0;
		$this->room_update_counts[ $room ] = 0;
		return array();
	}

	// Capture the current room state first so the returned cursor is race-safe.
	$stats = $wpdb->get_row(
		$wpdb->prepare(
			"SELECT COUNT(*) AS total_updates, COALESCE( MAX(meta_id), 0 ) AS max_meta_id FROM {$wpdb->postmeta} WHERE post_id = %d AND meta_key = %s",
			$post_id,
			self::SYNC_UPDATE_META_KEY
		)
	);

	$total_updates = $stats ? (int) $stats->total_updates : 0;
	$max_meta_id   = $stats ? (int) $stats->max_meta_id : 0;

	$this->room_update_counts[ $room ] = $total_updates;
	$this->room_cursors[ $room ]       = $max_meta_id;

	if ( $max_meta_id <= $cursor ) {
		return array();
	}

	$rows = $wpdb->get_results(
		$wpdb->prepare(
			"SELECT meta_value FROM {$wpdb->postmeta} WHERE post_id = %d AND meta_key = %s AND meta_id > %d AND meta_id <= %d ORDER BY meta_id ASC",
			$post_id,
			self::SYNC_UPDATE_META_KEY,
			$cursor,
			$max_meta_id
		)
	);

	if ( ! $rows ) {
		return array();
	}

	$updates = array();
	foreach ( $rows as $row ) {
		$decoded = json_decode( $row->meta_value, true );
		if ( null !== $decoded ) {
			$updates[] = $decoded;
		}
	}

	return $updates;
}

Changelog

VersionDescription
7.0.0Introduced.

User Contributed Notes

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