Skip to content

Commit

Permalink
Set internal stacks to null outside of directive processing
Browse files Browse the repository at this point in the history
  • Loading branch information
DAreRodz committed Jun 3, 2024
1 parent 66af20e commit 670022b
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
32 changes: 28 additions & 4 deletions src/wp-includes/interactivity-api/class-wp-interactivity-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,9 @@ final class WP_Interactivity_API {
* remain empty afterwards.
*
* @since 6.6.0
* @var array<string>
* @var array<string>|null
*/
private $namespace_stack = array();
private $namespace_stack = null;

/**
* Stack of contexts defined by `data-wp-context` directives, in
Expand All @@ -93,9 +93,9 @@ final class WP_Interactivity_API {
* remain empty afterwards.
*
* @since 6.6.0
* @var array<array<mixed>>
* @var array<array<mixed>>|null
*/
private $context_stack = array();
private $context_stack = null;

/**
* Gets and/or sets the initial state of an Interactivity API store for a
Expand Down Expand Up @@ -134,6 +134,15 @@ public function state( ?string $store_namespace = null, ?array $state = null ):
);
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 );
}
if ( ! isset( $this->state_data[ $store_namespace ] ) ) {
Expand Down Expand Up @@ -238,6 +247,15 @@ public function print_client_interactivity_data() {
* @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(
Expand Down Expand Up @@ -302,8 +320,14 @@ public function process_directives( string $html ): string {
return $html;
}

$this->namespace_stack = array();
$this->context_stack = array();

$result = $this->_process_directives( $html );

$this->namespace_stack = null;
$this->context_stack = null;

return null === $result ? $html : $result;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1327,6 +1327,7 @@ public function test_evaluate_derived_state_that_throws() {
},
)
);
$this->set_internal_context_stack();
$this->set_internal_namespace_stack( 'myPlugin' );

$result = $this->evaluate( 'state.derivedThatThrows' );
Expand Down

0 comments on commit 670022b

Please sign in to comment.