WP_Sync_Post_Meta_Storage::get_storage_post_idprivateWP 7.0.0

Gets or creates the storage post for a given room.

Each room gets its own dedicated post so that post meta cache invalidation is scoped to a single room rather than all of them.

Method of the class: WP_Sync_Post_Meta_Storage{}

No Hooks.

Returns

Int|null. Post ID.

Usage

// private - for code of main (parent) class only
$result = $this->get_storage_post_id( $room ): ?int;
$room(string) (required)
Room identifier.

Changelog

Since 7.0.0 Introduced.

WP_Sync_Post_Meta_Storage::get_storage_post_id() code WP 7.0

private function get_storage_post_id( string $room ): ?int {
	$room_hash = md5( $room );

	if ( isset( self::$storage_post_ids[ $room_hash ] ) ) {
		return self::$storage_post_ids[ $room_hash ];
	}

	// Try to find an existing post for this room.
	$posts = get_posts(
		array(
			'post_type'      => self::POST_TYPE,
			'posts_per_page' => 1,
			'post_status'    => 'publish',
			'name'           => $room_hash,
			'fields'         => 'ids',
			'orderby'        => 'ID',
			'order'          => 'ASC',
		)
	);

	$post_id = array_first( $posts );
	if ( is_int( $post_id ) ) {
		self::$storage_post_ids[ $room_hash ] = $post_id;
		return $post_id;
	}

	// Create new post for this room.
	$post_id = wp_insert_post(
		array(
			'post_type'   => self::POST_TYPE,
			'post_status' => 'publish',
			'post_title'  => 'Sync Storage',
			'post_name'   => $room_hash,
		)
	);

	if ( is_int( $post_id ) ) {
		self::$storage_post_ids[ $room_hash ] = $post_id;
		return $post_id;
	}

	return null;
}