Tribe__Events__Linked_Posts::maybe_get_new_order_from_blocks( int $event_id, string $linked_post_type, array $original_order = array() )

Reorder the meta keys to match the block order.


Parameters

$event_id

(int) (Required) Event ID.

$linked_post_type

(string) (Required) The post type of the linked post.

$original_order

(array) (Optional) The original IDs/order stored in meta.

Default value: array()


Top ↑

Return

(array) The new order of blocks if modified.


Top ↑

Source

File: src/Tribe/Linked_Posts.php

	public function maybe_get_new_order_from_blocks( int $event_id, string $linked_post_type, array $original_order = [] ) {
		// If the post has blocks, we need to update sorting of the post ids to link so it matches block order.
		if ( ! has_blocks( $event_id ) ) {
			return $original_order;
		}

		$new_order = [];
		$blocks = parse_blocks( get_the_content( null, false, $event_id ) );

		$block_name = 'tribe/event-venue';
		$block_id_key = 'venue';
		if ( $linked_post_type === \Tribe__Events__Organizer::POSTTYPE ) {
			$block_name = 'tribe/event-organizer';
			$block_id_key = 'organizer';
		}

		foreach ( $blocks as $block ) {
			if ( $block['blockName'] === $block_name ) {
				$new_order[] = $block['attrs'][ $block_id_key ];
			}
		}

		// To make sure we don't have data loss, let's prioritize blocks followed by the rest of the post ids and then remove duplicates.
		return array_map( 'absint', array_filter( array_unique( array_merge( $new_order, $original_order ) ) ) );
	}

Top ↑

Changelog

Changelog
Version Description
6.2.0 Introduced.