Interactivity API: Allow server derived state to appear in non-final position

In some cases, derived state returns an associative array. Directives may wish to continue to access properties of the associative array, when using the syntax `state.arrayReturnedByClosure.property`. This patch continues evaluating the path after the associative array has been returned by the Closure.

Props jonsurrell, luisherranz.

Fixes #61741.
Built from https://develop.svn.wordpress.org/trunk@58825


git-svn-id: http://core.svn.wordpress.org/trunk@58221 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
luisherranz 2024-07-29 11:10:23 +00:00
parent 2fecc77a68
commit 77f5a025b2
2 changed files with 27 additions and 26 deletions

View File

@ -494,6 +494,7 @@ final class WP_Interactivity_API {
* @since 6.5.0 * @since 6.5.0
* @since 6.6.0 The function now adds a warning when the namespace is null, falsy, or the directive value is empty. * @since 6.6.0 The function now adds a warning when the namespace is null, falsy, or the directive value is empty.
* @since 6.6.0 Removed `default_namespace` and `context` arguments. * @since 6.6.0 Removed `default_namespace` and `context` arguments.
* @since 6.6.0 Add support for derived state.
* *
* @param string|true $directive_value The directive attribute value string or `true` when it's a boolean attribute. * @param string|true $directive_value The directive attribute value string or `true` when it's a boolean attribute.
* @return mixed|null The result of the evaluation. Null if the reference path doesn't exist or the namespace is falsy. * @return mixed|null The result of the evaluation. Null if the reference path doesn't exist or the namespace is falsy.
@ -530,32 +531,32 @@ final class WP_Interactivity_API {
} else { } else {
return null; return null;
} }
}
if ( $current instanceof Closure ) { if ( $current instanceof Closure ) {
/* /*
* This state getter's namespace is added to the stack so that * This state getter's namespace is added to the stack so that
* `state()` or `get_config()` read that namespace when called * `state()` or `get_config()` read that namespace when called
* without specifying one. * without specifying one.
*/ */
array_push( $this->namespace_stack, $ns ); array_push( $this->namespace_stack, $ns );
try { try {
$current = $current(); $current = $current();
} catch ( Throwable $e ) { } catch ( Throwable $e ) {
_doing_it_wrong( _doing_it_wrong(
__METHOD__, __METHOD__,
sprintf( sprintf(
/* translators: 1: Path pointing to an Interactivity API state property, 2: Namespace for an Interactivity API store. */ /* 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".' ), __( 'Uncaught error executing a derived state callback with path "%1$s" and namespace "%2$s".' ),
$path, $path,
$ns $ns
), ),
'6.6.0' '6.6.0'
); );
return null; return null;
} finally { } finally {
// Remove the property's namespace from the stack. // Remove the property's namespace from the stack.
array_pop( $this->namespace_stack ); array_pop( $this->namespace_stack );
}
} }
} }

View File

@ -16,7 +16,7 @@
* *
* @global string $wp_version * @global string $wp_version
*/ */
$wp_version = '6.7-alpha-58824'; $wp_version = '6.7-alpha-58825';
/** /**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema. * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.