summaryrefslogtreecommitdiffstats
path: root/wp-includes/interactivity-api/interactivity-api.php
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-09-19 04:51:18 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-09-19 04:51:18 +0000
commit0e41b5d52fdc6af6442816b5f465c9db9f84e126 (patch)
treee139a90049b158d4eed892d1662ee7f5c358fa31 /wp-includes/interactivity-api/interactivity-api.php
parentAdding upstream version 6.5.5+dfsg1. (diff)
downloadwordpress-0e41b5d52fdc6af6442816b5f465c9db9f84e126.tar.xz
wordpress-0e41b5d52fdc6af6442816b5f465c9db9f84e126.zip
Adding upstream version 6.6.1+dfsg1.upstream/6.6.1+dfsg1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'wp-includes/interactivity-api/interactivity-api.php')
-rw-r--r--wp-includes/interactivity-api/interactivity-api.php89
1 files changed, 23 insertions, 66 deletions
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
@@ -8,71 +8,6 @@
*/
/**
- * 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.
*
* It provides access to the WP_Interactivity_API instance, creating one if it
@@ -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 );
+}