WP_Sync_Post_Meta_Storage::get_updates_after_cursorpublicWP 7.0.0

Retrieves sync updates from a room after the given cursor.

Method of the class: WP_Sync_Post_Meta_Storage{}

No Hooks.

Returns

Array. mixed> Sync updates.

Usage

$WP_Sync_Post_Meta_Storage = new WP_Sync_Post_Meta_Storage();
$WP_Sync_Post_Meta_Storage->get_updates_after_cursor( $room, $cursor ): array;
$room(string) (required)
Room identifier.
$cursor(int) (required)
Return updates after this cursor (meta_id).

Notes

  • Global. wpdb. $wpdb WordPress database abstraction object.

Changelog

Since 7.0.0 Introduced.

WP_Sync_Post_Meta_Storage::get_updates_after_cursor() code WP 7.0

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