<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[58327] trunk: Interactivity API: Directives cannot derive state on the server</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { white-space: pre-line; overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="https://core.trac.wordpress.org/changeset/58327">58327</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"https://core.trac.wordpress.org/changeset/58327","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>gziolo</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2024-06-04 10:59:01 +0000 (Tue, 04 Jun 2024)</dd>
</dl>

<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>Interactivity API: Directives cannot derive state on the server

The Interactivity API has a concept of "derived state" but it only worked on the client (JavaScript). This is the implementation that mirrors it, so derived state has good server-side solution.

Props jonsurrell, darerodz, gziolo, luisherranz, cbravobernal.
Fixes <a href="https://core.trac.wordpress.org/ticket/61037">#61037</a>.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpincludesinteractivityapiclasswpinteractivityapiphp">trunk/src/wp-includes/interactivity-api/class-wp-interactivity-api.php</a></li>
<li><a href="#trunksrcwpincludesinteractivityapiinteractivityapiphp">trunk/src/wp-includes/interactivity-api/interactivity-api.php</a></li>
<li><a href="#trunktestsphpunittestsinteractivityapiwpInteractivityAPIwpeachphp">trunk/tests/phpunit/tests/interactivity-api/wpInteractivityAPI-wp-each.php</a></li>
<li><a href="#trunktestsphpunittestsinteractivityapiwpInteractivityAPIwptextphp">trunk/tests/phpunit/tests/interactivity-api/wpInteractivityAPI-wp-text.php</a></li>
<li><a href="#trunktestsphpunittestsinteractivityapiwpInteractivityAPIphp">trunk/tests/phpunit/tests/interactivity-api/wpInteractivityAPI.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpincludesinteractivityapiclasswpinteractivityapiphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/interactivity-api/class-wp-interactivity-api.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/interactivity-api/class-wp-interactivity-api.php    2024-06-04 10:21:11 UTC (rev 58326)
+++ trunk/src/wp-includes/interactivity-api/class-wp-interactivity-api.php      2024-06-04 10:59:01 UTC (rev 58327)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -74,6 +74,28 @@
</span><span class="cx" style="display: block; padding: 0 10px">        private $has_processed_router_region = false;
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         * Stack of namespaces defined by `data-wp-interactive` directives, in
+        * the order they are processed.
+        *
+        * This is only available during directive processing, otherwise it is `null`.
+        *
+        * @since 6.6.0
+        * @var array<string>|null
+        */
+       private $namespace_stack = null;
+
+       /**
+        * Stack of contexts defined by `data-wp-context` directives, in
+        * the order they are processed.
+        *
+        * This is only available during directive processing, otherwise it is `null`.
+        *
+        * @since 6.6.0
+        * @var array<array<mixed>>|null
+        */
+       private $context_stack = null;
+
+       /**
</ins><span class="cx" style="display: block; padding: 0 10px">          * Gets and/or sets the initial state of an Interactivity API store for a
</span><span class="cx" style="display: block; padding: 0 10px">         * given namespace.
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -80,15 +102,47 @@
</span><span class="cx" style="display: block; padding: 0 10px">         * If state for that store namespace already exists, it merges the new
</span><span class="cx" style="display: block; padding: 0 10px">         * provided state with the existing one.
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         * When no namespace is specified, it returns the state defined for the
+        * current value in the internal namespace stack during a `process_directives` call.
+        *
</ins><span class="cx" style="display: block; padding: 0 10px">          * @since 6.5.0
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         * @since 6.6.0 The `$store_namespace` param is optional.
</ins><span class="cx" style="display: block; padding: 0 10px">          *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-         * @param string $store_namespace The unique store namespace identifier.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+  * @param string $store_namespace Optional. The unique store namespace identifier.
</ins><span class="cx" style="display: block; padding: 0 10px">          * @param array  $state           Optional. The array that will be merged with the existing state for the specified
</span><span class="cx" style="display: block; padding: 0 10px">         *                                store namespace.
</span><span class="cx" style="display: block; padding: 0 10px">         * @return array The current state for the specified store namespace. This will be the updated state if a $state
</span><span class="cx" style="display: block; padding: 0 10px">         *               argument was provided.
</span><span class="cx" style="display: block; padding: 0 10px">         */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        public function state( string $store_namespace, array $state = array() ): array {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ public function state( ?string $store_namespace = null, ?array $state = null ): array {
+               if ( ! $store_namespace ) {
+                       if ( $state ) {
+                               _doing_it_wrong(
+                                       __METHOD__,
+                                       __( 'The namespace is required when state data is passed.' ),
+                                       '6.6.0'
+                               );
+                               return array();
+                       }
+                       if ( null !== $store_namespace ) {
+                               _doing_it_wrong(
+                                       __METHOD__,
+                                       __( 'The namespace should be a non-empty string.' ),
+                                       '6.6.0'
+                               );
+                               return array();
+                       }
+                       if ( null === $this->namespace_stack ) {
+                               _doing_it_wrong(
+                                       __METHOD__,
+                                       __( 'The namespace can only be omitted during directive processing.' ),
+                                       '6.6.0'
+                               );
+                               return array();
+                       }
+
+                       $store_namespace = end( $this->namespace_stack );
+               }
</ins><span class="cx" style="display: block; padding: 0 10px">                 if ( ! isset( $this->state_data[ $store_namespace ] ) ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        $this->state_data[ $store_namespace ] = array();
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -212,6 +266,46 @@
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         * Returns the latest value on the context stack with the passed namespace.
+        *
+        * When the namespace is omitted, it uses the current namespace on the
+        * namespace stack during a `process_directives` call.
+        *
+        * @since 6.6.0
+        *
+        * @param string $store_namespace Optional. The unique store namespace identifier.
+        */
+       public function get_context( ?string $store_namespace = null ): array {
+               if ( null === $this->context_stack ) {
+                       _doing_it_wrong(
+                               __METHOD__,
+                               __( 'The context can only be read during directive processing.' ),
+                               '6.6.0'
+                       );
+                       return array();
+               }
+
+               if ( ! $store_namespace ) {
+                       if ( null !== $store_namespace ) {
+                               _doing_it_wrong(
+                                       __METHOD__,
+                                       __( 'The namespace should be a non-empty string.' ),
+                                       '6.6.0'
+                               );
+                               return array();
+                       }
+
+                       $store_namespace = end( $this->namespace_stack );
+               }
+
+               $context = end( $this->context_stack );
+
+               return ( $store_namespace && $context && isset( $context[ $store_namespace ] ) )
+                       ? $context[ $store_namespace ]
+                       : array();
+       }
+
+       /**
</ins><span class="cx" style="display: block; padding: 0 10px">          * Registers the `@wordpress/interactivity` script modules.
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="cx" style="display: block; padding: 0 10px">         * @since 6.5.0
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -258,9 +352,14 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        return $html;
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $context_stack   = array();
-               $namespace_stack = array();
-               $result          = $this->process_directives_args( $html, $context_stack, $namespace_stack );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $this->namespace_stack = array();
+               $this->context_stack   = array();
+
+               $result = $this->_process_directives( $html );
+
+               $this->namespace_stack = null;
+               $this->context_stack   = null;
+
</ins><span class="cx" style="display: block; padding: 0 10px">                 return null === $result ? $html : $result;
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -268,18 +367,17 @@
</span><span class="cx" style="display: block; padding: 0 10px">         * Processes the interactivity directives contained within the HTML content
</span><span class="cx" style="display: block; padding: 0 10px">         * and updates the markup accordingly.
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-         * It needs the context and namespace stacks to be passed by reference, and
-        * it returns null if the HTML contains unbalanced tags.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+  * It uses the WP_Interactivity_API instance's context and namespace stacks,
+        * which are shared between all calls.
</ins><span class="cx" style="display: block; padding: 0 10px">          *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-         * @since 6.5.0
-        * @since 6.6.0 The function displays a warning message when the HTML contains unbalanced tags or a directive appears in a MATH or SVG tag.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+  * This method returns null if the HTML contains unbalanced tags.
</ins><span class="cx" style="display: block; padding: 0 10px">          *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-         * @param string $html            The HTML content to process.
-        * @param array  $context_stack   The reference to the array used to keep track of contexts during processing.
-        * @param array  $namespace_stack The reference to the array used to manage namespaces during processing.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+  * @since 6.6.0
+        *
+        * @param string $html The HTML content to process.
</ins><span class="cx" style="display: block; padding: 0 10px">          * @return string|null The processed HTML content. It returns null when the HTML contains unbalanced tags.
</span><span class="cx" style="display: block; padding: 0 10px">         */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        private function process_directives_args( string $html, array &$context_stack, array &$namespace_stack ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ private function _process_directives( string $html ) {
</ins><span class="cx" style="display: block; padding: 0 10px">                 $p          = new WP_Interactivity_API_Directives_Processor( $html );
</span><span class="cx" style="display: block; padding: 0 10px">                $tag_stack  = array();
</span><span class="cx" style="display: block; padding: 0 10px">                $unbalanced = false;
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -287,6 +385,13 @@
</span><span class="cx" style="display: block; padding: 0 10px">                $directive_processor_prefixes          = array_keys( self::$directive_processors );
</span><span class="cx" style="display: block; padding: 0 10px">                $directive_processor_prefixes_reversed = array_reverse( $directive_processor_prefixes );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                /*
+                * Save the current size for each stack to restore them in case
+                * the processing finds unbalanced tags.
+                */
+               $namespace_stack_size = count( $this->namespace_stack );
+               $context_stack_size   = count( $this->context_stack );
+
</ins><span class="cx" style="display: block; padding: 0 10px">                 while ( $p->next_tag( array( 'tag_closers' => 'visit' ) ) ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        $tag_name = $p->get_tag();
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -298,7 +403,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        if ( 'SVG' === $tag_name || 'MATH' === $tag_name ) {
</span><span class="cx" style="display: block; padding: 0 10px">                                if ( $p->get_attribute_names_with_prefix( 'data-wp-' ) ) {
</span><span class="cx" style="display: block; padding: 0 10px">                                        /* translators: 1: SVG or MATH HTML tag, 2: Namespace of the interactive block. */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                        $message = sprintf( __( 'Interactivity directives were detected on an incompatible %1$s tag when processing "%2$s". These directives will be ignored in the server side render.' ), $tag_name, end( $namespace_stack ) );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                                 $message = sprintf( __( 'Interactivity directives were detected on an incompatible %1$s tag when processing "%2$s". These directives will be ignored in the server side render.' ), $tag_name, end( $this->namespace_stack ) );
</ins><span class="cx" style="display: block; padding: 0 10px">                                         _doing_it_wrong( __METHOD__, $message, '6.6.0' );
</span><span class="cx" style="display: block; padding: 0 10px">                                }
</span><span class="cx" style="display: block; padding: 0 10px">                                $p->skip_to_tag_closer();
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -381,13 +486,17 @@
</span><span class="cx" style="display: block; padding: 0 10px">                                                ? self::$directive_processors[ $directive_prefix ]
</span><span class="cx" style="display: block; padding: 0 10px">                                                : array( $this, self::$directive_processors[ $directive_prefix ] );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                        call_user_func_array(
-                                               $func,
-                                               array( $p, $mode, &$context_stack, &$namespace_stack, &$tag_stack )
-                                       );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                                 call_user_func_array( $func, array( $p, $mode, &$tag_stack ) );
</ins><span class="cx" style="display: block; padding: 0 10px">                                 }
</span><span class="cx" style="display: block; padding: 0 10px">                        }
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+               if ( $unbalanced ) {
+                       // Reset the namespace and context stacks to their previous values.
+                       array_splice( $this->namespace_stack, $namespace_stack_size );
+                       array_splice( $this->context_stack, $context_stack_size );
+               }
+
</ins><span class="cx" style="display: block; padding: 0 10px">                 /*
</span><span class="cx" style="display: block; padding: 0 10px">                 * It returns null if the HTML is unbalanced because unbalanced HTML is
</span><span class="cx" style="display: block; padding: 0 10px">                 * not safe to process. In that case, the Interactivity API runtime will
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -397,7 +506,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">                if ( $unbalanced || 0 < count( $tag_stack ) ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        $tag_errored = 0 < count( $tag_stack ) ? end( $tag_stack )[0] : $tag_name;
</span><span class="cx" style="display: block; padding: 0 10px">                        /* translators: %1s: Namespace processed, %2s: The tag that caused the error; could be any HTML tag.  */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        $message = sprintf( __( 'Interactivity directives failed to process in "%1$s" due to a missing "%2$s" end tag.' ), end( $namespace_stack ), $tag_errored );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 $message = sprintf( __( 'Interactivity directives failed to process in "%1$s" due to a missing "%2$s" end tag.' ), end( $this->namespace_stack ), $tag_errored );
</ins><span class="cx" style="display: block; padding: 0 10px">                         _doing_it_wrong( __METHOD__, $message, '6.6.0' );
</span><span class="cx" style="display: block; padding: 0 10px">                        return null;
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -411,15 +520,15 @@
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="cx" style="display: block; padding: 0 10px">         * @since 6.5.0
</span><span class="cx" style="display: block; padding: 0 10px">         * @since 6.6.0 The function now adds a warning when the namespace is null, falsy, or the directive value is empty.
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         * @since 6.6.0 Removed `default_namespace` and `context` arguments.
</ins><span class="cx" style="display: block; padding: 0 10px">          *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-         * @param string|true $directive_value   The directive attribute value string or `true` when it's a boolean attribute.
-        * @param string      $default_namespace The default namespace to use if none is explicitly defined in the directive
-        *                                       value.
-        * @param array|false $context           The current context for evaluating the directive or false if there is no
-        *                                       context.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+  * @param string|true $directive_value The directive attribute value string or `true` when it's a boolean attribute.
</ins><span class="cx" style="display: block; padding: 0 10px">          * @return mixed|null The result of the evaluation. Null if the reference path doesn't exist or the namespace is falsy.
</span><span class="cx" style="display: block; padding: 0 10px">         */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        private function evaluate( $directive_value, string $default_namespace, $context = false ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ private function evaluate( $directive_value ) {
+               $default_namespace = end( $this->namespace_stack );
+               $context           = end( $this->context_stack );
+
</ins><span class="cx" style="display: block; padding: 0 10px">                 list( $ns, $path ) = $this->extract_directive_value( $directive_value, $default_namespace );
</span><span class="cx" style="display: block; padding: 0 10px">                if ( ! $ns || ! $path ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        /* translators: %s: The directive value referenced. */
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -450,6 +559,33 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        }
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                if ( $current instanceof Closure ) {
+                       /*
+                        * This state getter's namespace is added to the stack so that
+                        * `state()` or `get_config()` read that namespace when called
+                        * without specifying one.
+                        */
+                       array_push( $this->namespace_stack, $ns );
+                       try {
+                               $current = $current();
+                       } catch ( Throwable $e ) {
+                               _doing_it_wrong(
+                                       __METHOD__,
+                                       sprintf(
+                                               /* translators: 1: Path pointing to an Interactivity API state property, 2: Namespace for an Interactivity API store. */
+                                               __( 'Uncaught error executing a derived state callback with path "%1$s" and namespace "%2$s".' ),
+                                               $path,
+                                               $ns
+                                       ),
+                                       '6.6.0'
+                               );
+                               return null;
+                       } finally {
+                               // Remove the property's namespace from the stack.
+                               array_pop( $this->namespace_stack );
+                       }
+               }
+
</ins><span class="cx" style="display: block; padding: 0 10px">                 // Returns the opposite if it contains a negation operator (!).
</span><span class="cx" style="display: block; padding: 0 10px">                return $should_negate_value ? ! $current : $current;
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -551,15 +687,13 @@
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="cx" style="display: block; padding: 0 10px">         * @since 6.5.0
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-         * @param WP_Interactivity_API_Directives_Processor $p               The directives processor instance.
-        * @param string                                    $mode            Whether the processing is entering or exiting the tag.
-        * @param array                                     $context_stack   The reference to the context stack.
-        * @param array                                     $namespace_stack The reference to the store namespace stack.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+  * @param WP_Interactivity_API_Directives_Processor $p    The directives processor instance.
+        * @param string                                    $mode Whether the processing is entering or exiting the tag.
</ins><span class="cx" style="display: block; padding: 0 10px">          */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        private function data_wp_interactive_processor( WP_Interactivity_API_Directives_Processor $p, string $mode, array &$context_stack, array &$namespace_stack ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ private function data_wp_interactive_processor( WP_Interactivity_API_Directives_Processor $p, string $mode ) {
</ins><span class="cx" style="display: block; padding: 0 10px">                 // When exiting tags, it removes the last namespace from the stack.
</span><span class="cx" style="display: block; padding: 0 10px">                if ( 'exit' === $mode ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        array_pop( $namespace_stack );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 array_pop( $this->namespace_stack );
</ins><span class="cx" style="display: block; padding: 0 10px">                         return;
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -583,9 +717,9 @@
</span><span class="cx" style="display: block; padding: 0 10px">                                $new_namespace = $attribute_value;
</span><span class="cx" style="display: block; padding: 0 10px">                        }
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $namespace_stack[] = ( $new_namespace && 1 === preg_match( '/^([\w\-_\/]+)/', $new_namespace ) )
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $this->namespace_stack[] = ( $new_namespace && 1 === preg_match( '/^([\w\-_\/]+)/', $new_namespace ) )
</ins><span class="cx" style="display: block; padding: 0 10px">                         ? $new_namespace
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        : end( $namespace_stack );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 : end( $this->namespace_stack );
</ins><span class="cx" style="display: block; padding: 0 10px">         }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -598,18 +732,16 @@
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="cx" style="display: block; padding: 0 10px">         * @param WP_Interactivity_API_Directives_Processor $p               The directives processor instance.
</span><span class="cx" style="display: block; padding: 0 10px">         * @param string                                    $mode            Whether the processing is entering or exiting the tag.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-         * @param array                                     $context_stack   The reference to the context stack.
-        * @param array                                     $namespace_stack The reference to the store namespace stack.
</del><span class="cx" style="display: block; padding: 0 10px">          */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        private function data_wp_context_processor( WP_Interactivity_API_Directives_Processor $p, string $mode, array &$context_stack, array &$namespace_stack ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ private function data_wp_context_processor( WP_Interactivity_API_Directives_Processor $p, string $mode ) {
</ins><span class="cx" style="display: block; padding: 0 10px">                 // When exiting tags, it removes the last context from the stack.
</span><span class="cx" style="display: block; padding: 0 10px">                if ( 'exit' === $mode ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        array_pop( $context_stack );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 array_pop( $this->context_stack );
</ins><span class="cx" style="display: block; padding: 0 10px">                         return;
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                $attribute_value = $p->get_attribute( 'data-wp-context' );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $namespace_value = end( $namespace_stack );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $namespace_value = end( $this->namespace_stack );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                // Separates the namespace from the context JSON object.
</span><span class="cx" style="display: block; padding: 0 10px">                list( $namespace_value, $decoded_json ) = is_string( $attribute_value ) && ! empty( $attribute_value )
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -621,8 +753,8 @@
</span><span class="cx" style="display: block; padding: 0 10px">                 * previous context with the new one.
</span><span class="cx" style="display: block; padding: 0 10px">                 */
</span><span class="cx" style="display: block; padding: 0 10px">                if ( is_string( $namespace_value ) ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        $context_stack[] = array_replace_recursive(
-                               end( $context_stack ) !== false ? end( $context_stack ) : array(),
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 $this->context_stack[] = array_replace_recursive(
+                               end( $this->context_stack ) !== false ? end( $this->context_stack ) : array(),
</ins><span class="cx" style="display: block; padding: 0 10px">                                 array( $namespace_value => is_array( $decoded_json ) ? $decoded_json : array() )
</span><span class="cx" style="display: block; padding: 0 10px">                        );
</span><span class="cx" style="display: block; padding: 0 10px">                } else {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -631,7 +763,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">                         * It needs to do so because the function pops out the current context
</span><span class="cx" style="display: block; padding: 0 10px">                         * from the stack whenever it finds a `data-wp-context`'s closing tag.
</span><span class="cx" style="display: block; padding: 0 10px">                         */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        $context_stack[] = end( $context_stack );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 $this->context_stack[] = end( $this->context_stack );
</ins><span class="cx" style="display: block; padding: 0 10px">                 }
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -645,10 +777,8 @@
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="cx" style="display: block; padding: 0 10px">         * @param WP_Interactivity_API_Directives_Processor $p               The directives processor instance.
</span><span class="cx" style="display: block; padding: 0 10px">         * @param string                                    $mode            Whether the processing is entering or exiting the tag.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-         * @param array                                     $context_stack   The reference to the context stack.
-        * @param array                                     $namespace_stack The reference to the store namespace stack.
</del><span class="cx" style="display: block; padding: 0 10px">          */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        private function data_wp_bind_processor( WP_Interactivity_API_Directives_Processor $p, string $mode, array &$context_stack, array &$namespace_stack ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ private function data_wp_bind_processor( WP_Interactivity_API_Directives_Processor $p, string $mode ) {
</ins><span class="cx" style="display: block; padding: 0 10px">                 if ( 'enter' === $mode ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        $all_bind_directives = $p->get_attribute_names_with_prefix( 'data-wp-bind--' );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -659,7 +789,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">                                }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                                $attribute_value = $p->get_attribute( $attribute_name );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                $result          = $this->evaluate( $attribute_value, end( $namespace_stack ), end( $context_stack ) );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                         $result          = $this->evaluate( $attribute_value );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                                if (
</span><span class="cx" style="display: block; padding: 0 10px">                                        null !== $result &&
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -699,10 +829,8 @@
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="cx" style="display: block; padding: 0 10px">         * @param WP_Interactivity_API_Directives_Processor $p               The directives processor instance.
</span><span class="cx" style="display: block; padding: 0 10px">         * @param string                                    $mode            Whether the processing is entering or exiting the tag.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-         * @param array                                     $context_stack   The reference to the context stack.
-        * @param array                                     $namespace_stack The reference to the store namespace stack.
</del><span class="cx" style="display: block; padding: 0 10px">          */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        private function data_wp_class_processor( WP_Interactivity_API_Directives_Processor $p, string $mode, array &$context_stack, array &$namespace_stack ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ private function data_wp_class_processor( WP_Interactivity_API_Directives_Processor $p, string $mode ) {
</ins><span class="cx" style="display: block; padding: 0 10px">                 if ( 'enter' === $mode ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        $all_class_directives = $p->get_attribute_names_with_prefix( 'data-wp-class--' );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -713,7 +841,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">                                }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                                $attribute_value = $p->get_attribute( $attribute_name );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                $result          = $this->evaluate( $attribute_value, end( $namespace_stack ), end( $context_stack ) );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                         $result          = $this->evaluate( $attribute_value );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                                if ( $result ) {
</span><span class="cx" style="display: block; padding: 0 10px">                                        $p->add_class( $class_name );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -734,10 +862,8 @@
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="cx" style="display: block; padding: 0 10px">         * @param WP_Interactivity_API_Directives_Processor $p               The directives processor instance.
</span><span class="cx" style="display: block; padding: 0 10px">         * @param string                                    $mode            Whether the processing is entering or exiting the tag.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-         * @param array                                     $context_stack   The reference to the context stack.
-        * @param array                                     $namespace_stack The reference to the store namespace stack.
</del><span class="cx" style="display: block; padding: 0 10px">          */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        private function data_wp_style_processor( WP_Interactivity_API_Directives_Processor $p, string $mode, array &$context_stack, array &$namespace_stack ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ private function data_wp_style_processor( WP_Interactivity_API_Directives_Processor $p, string $mode ) {
</ins><span class="cx" style="display: block; padding: 0 10px">                 if ( 'enter' === $mode ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        $all_style_attributes = $p->get_attribute_names_with_prefix( 'data-wp-style--' );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -748,7 +874,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">                                }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                                $directive_attribute_value = $p->get_attribute( $attribute_name );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                $style_property_value      = $this->evaluate( $directive_attribute_value, end( $namespace_stack ), end( $context_stack ) );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                         $style_property_value      = $this->evaluate( $directive_attribute_value );
</ins><span class="cx" style="display: block; padding: 0 10px">                                 $style_attribute_value     = $p->get_attribute( 'style' );
</span><span class="cx" style="display: block; padding: 0 10px">                                $style_attribute_value     = ( $style_attribute_value && ! is_bool( $style_attribute_value ) ) ? $style_attribute_value : '';
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -827,13 +953,11 @@
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="cx" style="display: block; padding: 0 10px">         * @param WP_Interactivity_API_Directives_Processor $p               The directives processor instance.
</span><span class="cx" style="display: block; padding: 0 10px">         * @param string                                    $mode            Whether the processing is entering or exiting the tag.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-         * @param array                                     $context_stack   The reference to the context stack.
-        * @param array                                     $namespace_stack The reference to the store namespace stack.
</del><span class="cx" style="display: block; padding: 0 10px">          */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        private function data_wp_text_processor( WP_Interactivity_API_Directives_Processor $p, string $mode, array &$context_stack, array &$namespace_stack ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ private function data_wp_text_processor( WP_Interactivity_API_Directives_Processor $p, string $mode ) {
</ins><span class="cx" style="display: block; padding: 0 10px">                 if ( 'enter' === $mode ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        $attribute_value = $p->get_attribute( 'data-wp-text' );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        $result          = $this->evaluate( $attribute_value, end( $namespace_stack ), end( $context_stack ) );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 $result          = $this->evaluate( $attribute_value );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                        /*
</span><span class="cx" style="display: block; padding: 0 10px">                         * Follows the same logic as Preact in the client and only changes the
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -965,17 +1089,15 @@
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="cx" style="display: block; padding: 0 10px">         * @param WP_Interactivity_API_Directives_Processor $p               The directives processor instance.
</span><span class="cx" style="display: block; padding: 0 10px">         * @param string                                    $mode            Whether the processing is entering or exiting the tag.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-         * @param array                                     $context_stack   The reference to the context stack.
-        * @param array                                     $namespace_stack The reference to the store namespace stack.
</del><span class="cx" style="display: block; padding: 0 10px">          * @param array                                     $tag_stack       The reference to the tag stack.
</span><span class="cx" style="display: block; padding: 0 10px">         */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        private function data_wp_each_processor( WP_Interactivity_API_Directives_Processor $p, string $mode, array &$context_stack, array &$namespace_stack, array &$tag_stack ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ private function data_wp_each_processor( WP_Interactivity_API_Directives_Processor $p, string $mode, array &$tag_stack ) {
</ins><span class="cx" style="display: block; padding: 0 10px">                 if ( 'enter' === $mode && 'TEMPLATE' === $p->get_tag() ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        $attribute_name   = $p->get_attribute_names_with_prefix( 'data-wp-each' )[0];
</span><span class="cx" style="display: block; padding: 0 10px">                        $extracted_suffix = $this->extract_prefix_and_suffix( $attribute_name );
</span><span class="cx" style="display: block; padding: 0 10px">                        $item_name        = isset( $extracted_suffix[1] ) ? $this->kebab_to_camel_case( $extracted_suffix[1] ) : 'item';
</span><span class="cx" style="display: block; padding: 0 10px">                        $attribute_value  = $p->get_attribute( $attribute_name );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        $result           = $this->evaluate( $attribute_value, end( $namespace_stack ), end( $context_stack ) );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 $result           = $this->evaluate( $attribute_value );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                        // Gets the content between the template tags and leaves the cursor in the closer tag.
</span><span class="cx" style="display: block; padding: 0 10px">                        $inner_content = $p->get_content_between_balanced_template_tags();
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1009,7 +1131,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                        // Extracts the namespace from the directive attribute value.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        $namespace_value         = end( $namespace_stack );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 $namespace_value         = end( $this->namespace_stack );
</ins><span class="cx" style="display: block; padding: 0 10px">                         list( $namespace_value ) = is_string( $attribute_value ) && ! empty( $attribute_value )
</span><span class="cx" style="display: block; padding: 0 10px">                                ? $this->extract_directive_value( $attribute_value, $namespace_value )
</span><span class="cx" style="display: block; padding: 0 10px">                                : array( $namespace_value, null );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1018,17 +1140,17 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        $processed_content = '';
</span><span class="cx" style="display: block; padding: 0 10px">                        foreach ( $result as $item ) {
</span><span class="cx" style="display: block; padding: 0 10px">                                // Creates a new context that includes the current item of the array.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                $context_stack[] = array_replace_recursive(
-                                       end( $context_stack ) !== false ? end( $context_stack ) : array(),
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                         $this->context_stack[] = array_replace_recursive(
+                                       end( $this->context_stack ) !== false ? end( $this->context_stack ) : array(),
</ins><span class="cx" style="display: block; padding: 0 10px">                                         array( $namespace_value => array( $item_name => $item ) )
</span><span class="cx" style="display: block; padding: 0 10px">                                );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                                // Processes the inner content with the new context.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                $processed_item = $this->process_directives_args( $inner_content, $context_stack, $namespace_stack );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                         $processed_item = $this->_process_directives( $inner_content );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                                if ( null === $processed_item ) {
</span><span class="cx" style="display: block; padding: 0 10px">                                        // If the HTML is unbalanced, stop processing it.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                        array_pop( $context_stack );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                                 array_pop( $this->context_stack );
</ins><span class="cx" style="display: block; padding: 0 10px">                                         return;
</span><span class="cx" style="display: block; padding: 0 10px">                                }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1041,7 +1163,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">                                $processed_content .= $i->get_updated_html();
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                                // Removes the current context from the stack.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                array_pop( $context_stack );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                         array_pop( $this->context_stack );
</ins><span class="cx" style="display: block; padding: 0 10px">                         }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                        // Appends the processed content after the tag closer of the template.
</span></span></pre></div>
<a id="trunksrcwpincludesinteractivityapiinteractivityapiphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/interactivity-api/interactivity-api.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/interactivity-api/interactivity-api.php     2024-06-04 10:21:11 UTC (rev 58326)
+++ trunk/src/wp-includes/interactivity-api/interactivity-api.php       2024-06-04 10:59:01 UTC (rev 58327)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -47,7 +47,11 @@
</span><span class="cx" style="display: block; padding: 0 10px">  * If state for that store namespace already exists, it merges the new
</span><span class="cx" style="display: block; padding: 0 10px">  * provided state with the existing one.
</span><span class="cx" style="display: block; padding: 0 10px">  *
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * The namespace can be omitted inside derived state getters, using the
+ * namespace where the getter is defined.
+ *
</ins><span class="cx" style="display: block; padding: 0 10px">  * @since 6.5.0
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @since 6.6.0 The namespace can be omitted when called inside derived state getters.
</ins><span class="cx" style="display: block; padding: 0 10px">  *
</span><span class="cx" style="display: block; padding: 0 10px">  * @param string $store_namespace The unique store namespace identifier.
</span><span class="cx" style="display: block; padding: 0 10px">  * @param array  $state           Optional. The array that will be merged with the existing state for the specified
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -55,7 +59,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">  * @return array The state for the specified store namespace. This will be the updated state if a $state argument was
</span><span class="cx" style="display: block; padding: 0 10px">  *               provided.
</span><span class="cx" style="display: block; padding: 0 10px">  */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-function wp_interactivity_state( string $store_namespace, array $state = array() ): array {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+function wp_interactivity_state( ?string $store_namespace = null, array $state = array() ): array {
</ins><span class="cx" style="display: block; padding: 0 10px">         return wp_interactivity()->state( $store_namespace, $state );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -103,3 +107,21 @@
</span><span class="cx" style="display: block; padding: 0 10px">                ( empty( $context ) ? '{}' : wp_json_encode( $context, JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP ) ) .
</span><span class="cx" style="display: block; padding: 0 10px">                '\'';
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+/**
+ * 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 );
+}
</ins></span></pre></div>
<a id="trunktestsphpunittestsinteractivityapiwpInteractivityAPIwpeachphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/tests/interactivity-api/wpInteractivityAPI-wp-each.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/interactivity-api/wpInteractivityAPI-wp-each.php        2024-06-04 10:21:11 UTC (rev 58326)
+++ trunk/tests/phpunit/tests/interactivity-api/wpInteractivityAPI-wp-each.php  2024-06-04 10:59:01 UTC (rev 58327)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -581,7 +581,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="cx" style="display: block; padding: 0 10px">         * @covers ::process_directives
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-         * @expectedIncorrectUsage WP_Interactivity_API::process_directives_args
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+  * @expectedIncorrectUsage WP_Interactivity_API::_process_directives
</ins><span class="cx" style="display: block; padding: 0 10px">          */
</span><span class="cx" style="display: block; padding: 0 10px">        public function test_wp_each_unbalanced_tags() {
</span><span class="cx" style="display: block; padding: 0 10px">                $original = '' .
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -601,7 +601,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="cx" style="display: block; padding: 0 10px">         * @covers ::process_directives
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-         * @expectedIncorrectUsage WP_Interactivity_API::process_directives_args
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+  * @expectedIncorrectUsage WP_Interactivity_API::_process_directives
</ins><span class="cx" style="display: block; padding: 0 10px">          */
</span><span class="cx" style="display: block; padding: 0 10px">        public function test_wp_each_unbalanced_tags_in_nested_template_tags() {
</span><span class="cx" style="display: block; padding: 0 10px">                $this->interactivity->state( 'myPlugin', array( 'list2' => array( 3, 4 ) ) );
</span></span></pre></div>
<a id="trunktestsphpunittestsinteractivityapiwpInteractivityAPIwptextphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/tests/interactivity-api/wpInteractivityAPI-wp-text.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/interactivity-api/wpInteractivityAPI-wp-text.php        2024-06-04 10:21:11 UTC (rev 58326)
+++ trunk/tests/phpunit/tests/interactivity-api/wpInteractivityAPI-wp-text.php  2024-06-04 10:59:01 UTC (rev 58327)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -132,7 +132,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="cx" style="display: block; padding: 0 10px">         * @covers ::process_directives
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-         * @expectedIncorrectUsage WP_Interactivity_API::process_directives_args
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+  * @expectedIncorrectUsage WP_Interactivity_API::_process_directives
</ins><span class="cx" style="display: block; padding: 0 10px">          */
</span><span class="cx" style="display: block; padding: 0 10px">        public function test_wp_text_fails_with_unbalanced_and_same_tags_inside_content() {
</span><span class="cx" style="display: block; padding: 0 10px">                $html     = '<div data-wp-text="myPlugin::state.text">Text<div></div>';
</span></span></pre></div>
<a id="trunktestsphpunittestsinteractivityapiwpInteractivityAPIphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/tests/interactivity-api/wpInteractivityAPI.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/interactivity-api/wpInteractivityAPI.php        2024-06-04 10:21:11 UTC (rev 58326)
+++ trunk/tests/phpunit/tests/interactivity-api/wpInteractivityAPI.php  2024-06-04 10:59:01 UTC (rev 58327)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -32,6 +32,34 @@
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         * Modifies the internal namespace stack as if the WP_Interactivity_API
+        * instance had found `data-wp-interactive` directives during
+        * `process_directives` execution.
+        *
+        * @param array<string> $stack Values for the internal namespace stack.
+        */
+       private function set_internal_namespace_stack( ...$stack ) {
+               $interactivity   = new ReflectionClass( $this->interactivity );
+               $namespace_stack = $interactivity->getProperty( 'namespace_stack' );
+               $namespace_stack->setAccessible( true );
+               $namespace_stack->setValue( $this->interactivity, $stack );
+       }
+
+       /**
+        * Modifies the internal context stack as if the WP_Interactivity_API
+        * instance had found `data-wp-context` directives during
+        * `process_directives` execution.
+        *
+        * @param array<array<mixed>> $stack Values for the internal context stack.
+        */
+       private function set_internal_context_stack( ...$stack ) {
+               $interactivity = new ReflectionClass( $this->interactivity );
+               $context_stack = $interactivity->getProperty( 'context_stack' );
+               $context_stack->setAccessible( true );
+               $context_stack->setValue( $this->interactivity, $stack );
+       }
+
+       /**
</ins><span class="cx" style="display: block; padding: 0 10px">          * Tests that the state and config methods return an empty array at the
</span><span class="cx" style="display: block; padding: 0 10px">         * beginning.
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -426,6 +454,222 @@
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         * Test that calling state without a namespace arg returns the state data
+        * for the current namespace in the internal namespace stack.
+        *
+        * @ticket 61037
+        *
+        * @covers ::state
+        */
+       public function test_state_without_namespace() {
+               $this->set_internal_namespace_stack( 'myPlugin' );
+
+               $this->interactivity->state( 'myPlugin', array( 'a' => 1 ) );
+               $this->interactivity->state( 'otherPlugin', array( 'b' => 2 ) );
+
+               $this->assertEquals(
+                       array( 'a' => 1 ),
+                       $this->interactivity->state()
+               );
+       }
+
+       /**
+        * Test that passing state data without a valid namespace does nothing and
+        * just returns an empty array.
+        *
+        * @ticket 61037
+        *
+        * @covers ::state
+        * @expectedIncorrectUsage WP_Interactivity_API::state
+        */
+       public function test_state_with_data_and_invalid_namespace() {
+               $this->set_internal_namespace_stack( 'myPlugin' );
+
+               $this->interactivity->state( 'myPlugin', array( 'a' => 1 ) );
+               $this->interactivity->state( 'otherPlugin', array( 'b' => 2 ) );
+
+               $this->assertEquals(
+                       array(),
+                       $this->interactivity->state( null, array( 'newProp' => 'value' ) )
+               );
+       }
+
+       /**
+        * Test that calling state with an empty string as namespace is not allowed.
+        *
+        * @ticket 61037
+        *
+        * @covers ::state
+        * @expectedIncorrectUsage WP_Interactivity_API::state
+        */
+       public function test_state_with_empty_string_as_namespace() {
+               $this->set_internal_namespace_stack( 'myPlugin' );
+
+               $this->interactivity->state( 'myPlugin', array( 'a' => 1 ) );
+               $this->interactivity->state( 'otherPlugin', array( 'b' => 2 ) );
+
+               $this->assertEquals(
+                       array(),
+                       $this->interactivity->state( '' )
+               );
+       }
+
+       /**
+        * Tests that calling state without namespace outside of
+        * `process_directives` execution is not allowed.
+        *
+        * @ticket 61037
+        *
+        * @covers ::state
+        * @expectedIncorrectUsage WP_Interactivity_API::state
+        */
+       public function test_state_without_namespace_outside_directive_processing() {
+               $this->assertEquals(
+                       array(),
+                       $this->interactivity->state()
+               );
+       }
+
+       /**
+        * Test that `get_context` returns the latest context value for the given
+        * namespace.
+        *
+        * @ticket 61037
+        *
+        * @covers ::get_context
+        */
+       public function test_get_context_with_namespace() {
+               $this->set_internal_namespace_stack( 'myPlugin' );
+               $this->set_internal_context_stack(
+                       array(
+                               'myPlugin' => array( 'a' => 0 ),
+                       ),
+                       array(
+                               'myPlugin'    => array( 'a' => 1 ),
+                               'otherPlugin' => array( 'b' => 2 ),
+                       )
+               );
+
+               $this->assertEquals(
+                       array( 'a' => 1 ),
+                       $this->interactivity->get_context( 'myPlugin' )
+               );
+               $this->assertEquals(
+                       array( 'b' => 2 ),
+                       $this->interactivity->get_context( 'otherPlugin' )
+               );
+       }
+
+       /**
+        * Test that `get_context` uses the current namespace in the internal
+        * namespace stack when the parameter is omitted.
+        *
+        * @ticket 61037
+        *
+        * @covers ::get_context
+        */
+       public function test_get_context_without_namespace() {
+               $this->set_internal_namespace_stack( 'myPlugin' );
+               $this->set_internal_context_stack(
+                       array(
+                               'myPlugin' => array( 'a' => 0 ),
+                       ),
+                       array(
+                               'myPlugin'    => array( 'a' => 1 ),
+                               'otherPlugin' => array( 'b' => 2 ),
+                       )
+               );
+
+               $this->assertEquals(
+                       array( 'a' => 1 ),
+                       $this->interactivity->get_context()
+               );
+       }
+
+       /**
+        * Test that `get_context` returns an empty array when the context stack is
+        * empty.
+        *
+        * @ticket 61037
+        *
+        * @covers ::get_context
+        */
+       public function test_get_context_with_empty_context_stack() {
+               $this->set_internal_namespace_stack( 'myPlugin' );
+               $this->set_internal_context_stack();
+
+               $this->assertEquals(
+                       array(),
+                       $this->interactivity->get_context( 'myPlugin' )
+               );
+       }
+
+       /**
+        * Test that `get_context` returns an empty array if the given namespace is
+        * not defined.
+        *
+        * @ticket 61037
+        *
+        * @covers ::get_context
+        */
+       public function test_get_context_with_undefined_namespace() {
+               $this->set_internal_namespace_stack( 'myPlugin' );
+               $this->set_internal_context_stack(
+                       array(
+                               'myPlugin' => array( 'a' => 0 ),
+                       ),
+                       array(
+                               'myPlugin' => array( 'a' => 1 ),
+                       )
+               );
+
+               $this->assertEquals(
+                       array(),
+                       $this->interactivity->get_context( 'otherPlugin' )
+               );
+       }
+
+       /**
+        * Test that `get_context` should not be called with an empty string.
+        *
+        * @ticket 61037
+        *
+        * @covers ::get_context
+        * @expectedIncorrectUsage WP_Interactivity_API::get_context
+        */
+       public function test_get_context_with_empty_namespace() {
+               $this->set_internal_namespace_stack( 'myPlugin' );
+               $this->set_internal_context_stack(
+                       array(
+                               'myPlugin' => array( 'a' => 0 ),
+                       ),
+                       array(
+                               'myPlugin' => array( 'a' => 1 ),
+                       )
+               );
+
+               $this->assertEquals(
+                       array(),
+                       $this->interactivity->get_context( '' )
+               );
+       }
+
+
+       /**
+        * Tests that `get_context` should not be called outside of
+        * `process_directives` execution.
+        *
+        * @ticket 61037
+        *
+        * @covers ::get_context
+        * @expectedIncorrectUsage WP_Interactivity_API::get_context
+        */
+       public function test_get_context_outside_of_directive_processing() {
+               $context = $this->interactivity->get_context();
+               $this->assertEquals( array(), $context );
+       }
+
+       /**
</ins><span class="cx" style="display: block; padding: 0 10px">          * Tests extracting directive values from different string formats.
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="cx" style="display: block; padding: 0 10px">         * @ticket 60356
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -649,7 +893,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="cx" style="display: block; padding: 0 10px">         * @dataProvider data_html_with_unbalanced_tags
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-         * @expectedIncorrectUsage WP_Interactivity_API::process_directives_args
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+  * @expectedIncorrectUsage WP_Interactivity_API::_process_directives
</ins><span class="cx" style="display: block; padding: 0 10px">          *
</span><span class="cx" style="display: block; padding: 0 10px">         * @param string $html HTML containing unbalanced tags and also a directive.
</span><span class="cx" style="display: block; padding: 0 10px">         */
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -748,7 +992,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">         * @ticket 60517
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="cx" style="display: block; padding: 0 10px">         * @covers ::process_directives
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-         * @expectedIncorrectUsage WP_Interactivity_API::process_directives_args
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+  * @expectedIncorrectUsage WP_Interactivity_API::_process_directives
</ins><span class="cx" style="display: block; padding: 0 10px">          */
</span><span class="cx" style="display: block; padding: 0 10px">        public function test_process_directives_change_html_if_contains_math() {
</span><span class="cx" style="display: block; padding: 0 10px">                $this->interactivity->state(
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -783,7 +1027,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">         * @ticket 60517
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="cx" style="display: block; padding: 0 10px">         * @covers ::process_directives
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-         * @expectedIncorrectUsage WP_Interactivity_API::process_directives_args
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+  * @expectedIncorrectUsage WP_Interactivity_API::_process_directives
</ins><span class="cx" style="display: block; padding: 0 10px">          * @expectedIncorrectUsage WP_Interactivity_API_Directives_Processor::skip_to_tag_closer
</span><span class="cx" style="display: block; padding: 0 10px">         */
</span><span class="cx" style="display: block; padding: 0 10px">        public function test_process_directives_does_not_change_inner_html_in_math() {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -813,24 +1057,51 @@
</span><span class="cx" style="display: block; padding: 0 10px">         * Invokes the private `evaluate` method of WP_Interactivity_API class.
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="cx" style="display: block; padding: 0 10px">         * @param string $directive_value   The directive attribute value to evaluate.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-         * @param string $default_namespace The default namespace used with directives.
</del><span class="cx" style="display: block; padding: 0 10px">          * @return mixed The result of the evaluate method.
</span><span class="cx" style="display: block; padding: 0 10px">         */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        private function evaluate( $directive_value, $default_namespace = 'myPlugin' ) {
-               $generate_state = function ( $name ) {
-                       $obj       = new stdClass();
-                       $obj->prop = $name;
-                       return array(
-                               'key'       => $name,
-                               'nested'    => array( 'key' => $name . '-nested' ),
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ private function evaluate( $directive_value ) {
+               /*
+                * The global WP_Interactivity_API instance is momentarily replaced to
+                * make global functions like `wp_interactivity_state` and
+                * `wp_interactivity_get_config` work as expected.
+                */
+               global $wp_interactivity;
+               $wp_interactivity_prev = $wp_interactivity;
+               $wp_interactivity      = $this->interactivity;
+
+               $evaluate = new ReflectionMethod( $this->interactivity, 'evaluate' );
+               $evaluate->setAccessible( true );
+
+               $result = $evaluate->invokeArgs( $this->interactivity, array( $directive_value ) );
+
+               // Restore the original WP_Interactivity_API instance.
+               $wp_interactivity = $wp_interactivity_prev;
+
+               return $result;
+       }
+
+       /**
+        * Tests that the `evaluate` method operates correctly for valid expressions.
+        *
+        * @ticket 60356
+        *
+        * @covers ::evaluate
+        */
+       public function test_evaluate_value() {
+               $obj       = new stdClass();
+               $obj->prop = 'object property';
+               $this->interactivity->state(
+                       'myPlugin',
+                       array(
+                               'key'       => 'myPlugin-state',
</ins><span class="cx" style="display: block; padding: 0 10px">                                 'obj'       => $obj,
</span><span class="cx" style="display: block; padding: 0 10px">                                'arrAccess' => new class() implements ArrayAccess {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                        #[\ReturnTypeWillChange]
-                                       public function offsetExists( $offset ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                                 public function offsetExists( $offset ): bool {
</ins><span class="cx" style="display: block; padding: 0 10px">                                                 return true;
</span><span class="cx" style="display: block; padding: 0 10px">                                        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                        public function offsetGet( $offset ): string {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                                 #[\ReturnTypeWillChange]
+                                       public function offsetGet( $offset ) {
</ins><span class="cx" style="display: block; padding: 0 10px">                                                 return $offset;
</span><span class="cx" style="display: block; padding: 0 10px">                                        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -838,28 +1109,17 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                                        public function offsetUnset( $offset ): void {}
</span><span class="cx" style="display: block; padding: 0 10px">                                },
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        );
-               };
-               $this->interactivity->state( 'myPlugin', $generate_state( 'myPlugin-state' ) );
-               $this->interactivity->state( 'otherPlugin', $generate_state( 'otherPlugin-state' ) );
-               $context  = array(
-                       'myPlugin'    => $generate_state( 'myPlugin-context' ),
-                       'otherPlugin' => $generate_state( 'otherPlugin-context' ),
-                       'obj'         => new stdClass(),
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 )
</ins><span class="cx" style="display: block; padding: 0 10px">                 );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $evaluate = new ReflectionMethod( $this->interactivity, 'evaluate' );
-               $evaluate->setAccessible( true );
-               return $evaluate->invokeArgs( $this->interactivity, array( $directive_value, $default_namespace, $context ) );
-       }
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $this->interactivity->state( 'otherPlugin', array( 'key' => 'otherPlugin-state' ) );
+               $this->set_internal_context_stack(
+                       array(
+                               'myPlugin'    => array( 'key' => 'myPlugin-context' ),
+                               'otherPlugin' => array( 'key' => 'otherPlugin-context' ),
+                       )
+               );
+               $this->set_internal_namespace_stack( 'myPlugin' );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        /**
-        * Tests that the `evaluate` method operates correctly for valid expressions.
-        *
-        * @ticket 60356
-        *
-        * @covers ::evaluate
-        */
-       public function test_evaluate_value() {
</del><span class="cx" style="display: block; padding: 0 10px">                 $result = $this->evaluate( 'state.key' );
</span><span class="cx" style="display: block; padding: 0 10px">                $this->assertEquals( 'myPlugin-state', $result );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -873,7 +1133,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">                $this->assertEquals( 'otherPlugin-context', $result );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                $result = $this->evaluate( 'state.obj.prop' );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $this->assertSame( 'myPlugin-state', $result );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $this->assertSame( 'object property', $result );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                $result = $this->evaluate( 'state.arrAccess.1' );
</span><span class="cx" style="display: block; padding: 0 10px">                $this->assertSame( '1', $result );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -888,6 +1148,16 @@
</span><span class="cx" style="display: block; padding: 0 10px">         * @covers ::evaluate
</span><span class="cx" style="display: block; padding: 0 10px">         */
</span><span class="cx" style="display: block; padding: 0 10px">        public function test_evaluate_value_negation() {
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                $this->interactivity->state( 'myPlugin', array( 'key' => 'myPlugin-state' ) );
+               $this->interactivity->state( 'otherPlugin', array( 'key' => 'otherPlugin-state' ) );
+               $this->set_internal_context_stack(
+                       array(
+                               'myPlugin'    => array( 'key' => 'myPlugin-context' ),
+                               'otherPlugin' => array( 'key' => 'otherPlugin-context' ),
+                       )
+               );
+               $this->set_internal_namespace_stack( 'myPlugin' );
+
</ins><span class="cx" style="display: block; padding: 0 10px">                 $result = $this->evaluate( '!state.key' );
</span><span class="cx" style="display: block; padding: 0 10px">                $this->assertFalse( $result );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -909,6 +1179,16 @@
</span><span class="cx" style="display: block; padding: 0 10px">         * @covers ::evaluate
</span><span class="cx" style="display: block; padding: 0 10px">         */
</span><span class="cx" style="display: block; padding: 0 10px">        public function test_evaluate_non_existent_path() {
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                $this->interactivity->state( 'myPlugin', array( 'key' => 'myPlugin-state' ) );
+               $this->interactivity->state( 'otherPlugin', array( 'key' => 'otherPlugin-state' ) );
+               $this->set_internal_context_stack(
+                       array(
+                               'myPlugin'    => array( 'key' => 'myPlugin-context' ),
+                               'otherPlugin' => array( 'key' => 'otherPlugin-context' ),
+                       )
+               );
+               $this->set_internal_namespace_stack( 'myPlugin' );
+
</ins><span class="cx" style="display: block; padding: 0 10px">                 $result = $this->evaluate( 'state.nonExistentKey' );
</span><span class="cx" style="display: block; padding: 0 10px">                $this->assertNull( $result );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -936,6 +1216,30 @@
</span><span class="cx" style="display: block; padding: 0 10px">         * @covers ::evaluate
</span><span class="cx" style="display: block; padding: 0 10px">         */
</span><span class="cx" style="display: block; padding: 0 10px">        public function test_evaluate_nested_value() {
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                $this->interactivity->state(
+                       'myPlugin',
+                       array(
+                               'nested' => array( 'key' => 'myPlugin-state-nested' ),
+                       )
+               );
+               $this->interactivity->state(
+                       'otherPlugin',
+                       array(
+                               'nested' => array( 'key' => 'otherPlugin-state-nested' ),
+                       )
+               );
+               $this->set_internal_context_stack(
+                       array(
+                               'myPlugin'    => array(
+                                       'nested' => array( 'key' => 'myPlugin-context-nested' ),
+                               ),
+                               'otherPlugin' => array(
+                                       'nested' => array( 'key' => 'otherPlugin-context-nested' ),
+                               ),
+                       )
+               );
+               $this->set_internal_namespace_stack( 'myPlugin' );
+
</ins><span class="cx" style="display: block; padding: 0 10px">                 $result = $this->evaluate( 'state.nested.key' );
</span><span class="cx" style="display: block; padding: 0 10px">                $this->assertEquals( 'myPlugin-state-nested', $result );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -958,6 +1262,9 @@
</span><span class="cx" style="display: block; padding: 0 10px">         * @expectedIncorrectUsage WP_Interactivity_API::evaluate
</span><span class="cx" style="display: block; padding: 0 10px">         */
</span><span class="cx" style="display: block; padding: 0 10px">        public function test_evaluate_unvalid_namespaces() {
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                $this->set_internal_context_stack( array() );
+               $this->set_internal_namespace_stack();
+
</ins><span class="cx" style="display: block; padding: 0 10px">                 $result = $this->evaluate( 'path', 'null' );
</span><span class="cx" style="display: block; padding: 0 10px">                $this->assertNull( $result );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -969,6 +1276,155 @@
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         * Tests the `evaluate` method for derived state functions.
+        *
+        * @ticket 61037
+        *
+        * @covers ::evaluate
+        * @covers wp_interactivity_state
+        * @covers wp_interactivity_get_context
+        */
+       public function test_evaluate_derived_state() {
+               $this->interactivity->state(
+                       'myPlugin',
+                       array(
+                               'key'     => 'myPlugin-state',
+                               'derived' => function () {
+                                       $state   = wp_interactivity_state();
+                                       $context = wp_interactivity_get_context();
+                                       return 'Derived state: ' .
+                                               $state['key'] .
+                                               "\n" .
+                                               'Derived context: ' .
+                                               $context['key'];
+                               },
+                       )
+               );
+               $this->set_internal_context_stack(
+                       array(
+                               'myPlugin' => array(
+                                       'key' => 'myPlugin-context',
+                               ),
+                       )
+               );
+               $this->set_internal_namespace_stack( 'myPlugin' );
+
+               $result = $this->evaluate( 'state.derived' );
+               $this->assertSame( "Derived state: myPlugin-state\nDerived context: myPlugin-context", $result );
+       }
+
+       /**
+        * Tests the `evaluate` method for derived state functions accessing a
+        * different namespace.
+        *
+        * @ticket 61037
+        *
+        * @covers ::evaluate
+        * @covers wp_interactivity_state
+        * @covers wp_interactivity_get_context
+        */
+       public function test_evaluate_derived_state_accessing_different_namespace() {
+               $this->interactivity->state(
+                       'myPlugin',
+                       array(
+                               'key'     => 'myPlugin-state',
+                               'derived' => function () {
+                                       $state   = wp_interactivity_state( 'otherPlugin' );
+                                       $context = wp_interactivity_get_context( 'otherPlugin' );
+                                       return 'Derived state: ' .
+                                               $state['key'] .
+                                               "\n" .
+                                               'Derived context: ' .
+                                               $context['key'];
+                               },
+                       )
+               );
+               $this->interactivity->state( 'otherPlugin', array( 'key' => 'otherPlugin-state' ) );
+               $this->set_internal_context_stack(
+                       array(
+                               'myPlugin'    => array(
+                                       'key' => 'myPlugin-context',
+                               ),
+                               'otherPlugin' => array(
+                                       'key' => 'otherPlugin-context',
+                               ),
+                       )
+               );
+               $this->set_internal_namespace_stack( 'myPlugin' );
+
+               $result = $this->evaluate( 'state.derived' );
+               $this->assertSame( "Derived state: otherPlugin-state\nDerived context: otherPlugin-context", $result );
+       }
+
+       /**
+        * Tests the `evaluate` method for derived state functions defined in a
+        * different namespace.
+        *
+        * @ticket 61037
+        *
+        * @covers ::evaluate
+        * @covers wp_interactivity_state
+        * @covers wp_interactivity_get_context
+        */
+       public function test_evaluate_derived_state_defined_in_different_namespace() {
+               $this->interactivity->state( 'myPlugin', array( 'key' => 'myPlugin-state' ) );
+               $this->interactivity->state(
+                       'otherPlugin',
+                       array(
+                               'key'     => 'otherPlugin-state',
+                               'derived' => function () {
+                                       $state   = wp_interactivity_state();
+                                       $context = wp_interactivity_get_context();
+                                       return 'Derived state: ' .
+                                               $state['key'] .
+                                               "\n" .
+                                               'Derived context: ' .
+                                               $context['key'];
+                               },
+                       )
+               );
+               $this->set_internal_context_stack(
+                       array(
+                               'myPlugin'    => array(
+                                       'key' => 'myPlugin-context',
+                               ),
+                               'otherPlugin' => array(
+                                       'key' => 'otherPlugin-context',
+                               ),
+                       )
+               );
+               $this->set_internal_namespace_stack( 'myPlugin' );
+
+               $result = $this->evaluate( 'otherPlugin::state.derived' );
+               $this->assertSame( "Derived state: otherPlugin-state\nDerived context: otherPlugin-context", $result );
+       }
+
+
+       /**
+        * Tests the `evaluate` method for derived state functions that throw.
+        *
+        * @ticket 61037
+        *
+        * @covers ::evaluate
+        * @expectedIncorrectUsage WP_Interactivity_API::evaluate
+        */
+       public function test_evaluate_derived_state_that_throws() {
+               $this->interactivity->state(
+                       'myPlugin',
+                       array(
+                               'derivedThatThrows' => function () {
+                                       throw new Error( 'Something bad happened.' );
+                               },
+                       )
+               );
+               $this->set_internal_context_stack();
+               $this->set_internal_namespace_stack( 'myPlugin' );
+
+               $result = $this->evaluate( 'state.derivedThatThrows' );
+               $this->assertNull( $result );
+       }
+
+       /**
</ins><span class="cx" style="display: block; padding: 0 10px">          * Tests the `kebab_to_camel_case` method.
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="cx" style="display: block; padding: 0 10px">         * @covers ::kebab_to_camel_case
</span></span></pre>
</div>
</div>

</body>
</html>