From 0e41b5d52fdc6af6442816b5f465c9db9f84e126 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Thu, 19 Sep 2024 06:51:18 +0200 Subject: Adding upstream version 6.6.1+dfsg1. Signed-off-by: Daniel Baumann --- .../interactivity-api/interactivity-api.php | 89 ++++++---------------- 1 file changed, 23 insertions(+), 66 deletions(-) (limited to 'wp-includes/interactivity-api/interactivity-api.php') diff --git a/wp-includes/interactivity-api/interactivity-api.php b/wp-includes/interactivity-api/interactivity-api.php index b8f3e50..e007d51 100644 --- a/wp-includes/interactivity-api/interactivity-api.php +++ b/wp-includes/interactivity-api/interactivity-api.php @@ -7,71 +7,6 @@ * @since 6.5.0 */ -/** - * Processes the directives on the rendered HTML of the interactive blocks. - * - * This processes only one root interactive block at a time because the - * rendered HTML of that block contains the rendered HTML of all its inner - * blocks, including any interactive block. It does so by ignoring all the - * interactive inner blocks until the root interactive block is processed. - * - * @since 6.5.0 - * - * @param array $parsed_block The parsed block. - * @return array The same parsed block. - */ -function wp_interactivity_process_directives_of_interactive_blocks( array $parsed_block ): array { - static $root_interactive_block = null; - - /* - * Checks whether a root interactive block is already annotated for - * processing, and if it is, it ignores the subsequent ones. - */ - if ( null === $root_interactive_block ) { - $block_name = $parsed_block['blockName']; - $block_type = WP_Block_Type_Registry::get_instance()->get_registered( $block_name ); - - if ( - isset( $block_name ) && - ( ( isset( $block_type->supports['interactivity'] ) && true === $block_type->supports['interactivity'] ) || - ( isset( $block_type->supports['interactivity']['interactive'] ) && true === $block_type->supports['interactivity']['interactive'] ) ) - ) { - // Annotates the root interactive block for processing. - $root_interactive_block = array( $block_name, $parsed_block ); - - /* - * Adds a filter to process the root interactive block once it has - * finished rendering. - */ - $process_interactive_blocks = static function ( string $content, array $parsed_block ) use ( &$root_interactive_block, &$process_interactive_blocks ): string { - // Checks whether the current block is the root interactive block. - list($root_block_name, $root_parsed_block) = $root_interactive_block; - if ( $root_block_name === $parsed_block['blockName'] && $parsed_block === $root_parsed_block ) { - // The root interactive blocks has finished rendering, process it. - $content = wp_interactivity_process_directives( $content ); - // Removes the filter and reset the root interactive block. - remove_filter( 'render_block_' . $parsed_block['blockName'], $process_interactive_blocks ); - $root_interactive_block = null; - } - return $content; - }; - - /* - * Uses a priority of 100 to ensure that other filters can add additional - * directives before the processing starts. - */ - add_filter( 'render_block_' . $block_name, $process_interactive_blocks, 100, 2 ); - } - } - - return $parsed_block; -} -/* - * Uses a priority of 100 to ensure that other filters can add additional attributes to - * $parsed_block before the processing starts. - */ -add_filter( 'render_block_data', 'wp_interactivity_process_directives_of_interactive_blocks', 100, 1 ); - /** * Retrieves the main WP_Interactivity_API instance. * @@ -112,7 +47,11 @@ function wp_interactivity_process_directives( string $html ): string { * If state for that store namespace already exists, it merges the new * provided state with the existing one. * + * The namespace can be omitted inside derived state getters, using the + * namespace where the getter is defined. + * * @since 6.5.0 + * @since 6.6.0 The namespace can be omitted when called inside derived state getters. * * @param string $store_namespace The unique store namespace identifier. * @param array $state Optional. The array that will be merged with the existing state for the specified @@ -120,7 +59,7 @@ function wp_interactivity_process_directives( string $html ): string { * @return array The state for the specified store namespace. This will be the updated state if a $state argument was * provided. */ -function wp_interactivity_state( string $store_namespace, array $state = array() ): array { +function wp_interactivity_state( ?string $store_namespace = null, array $state = array() ): array { return wp_interactivity()->state( $store_namespace, $state ); } @@ -168,3 +107,21 @@ function wp_interactivity_data_wp_context( array $context, string $store_namespa ( empty( $context ) ? '{}' : wp_json_encode( $context, JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP ) ) . '\''; } + +/** + * Gets the current Interactivity API context for a given namespace. + * + * The function should be used only during directive processing. If the + * `$store_namespace` parameter is omitted, it uses the current namespace value + * on the internal namespace stack. + * + * It returns an empty array when the specified namespace is not defined. + * + * @since 6.6.0 + * + * @param string $store_namespace Optional. The unique store namespace identifier. + * @return array The context for the specified store namespace. + */ +function wp_interactivity_get_context( ?string $store_namespace = null ): array { + return wp_interactivity()->get_context( $store_namespace ); +} -- cgit v1.2.3