Skip to content

Commit

Permalink
Back to array for references and comment delimiters
Browse files Browse the repository at this point in the history
  • Loading branch information
cbravobernal committed Nov 28, 2023
1 parent 585e8fd commit ca33458
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 42 deletions.
31 changes: 16 additions & 15 deletions lib/experimental/interactivity-api/class-wp-directive-processor.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,30 +70,31 @@ public static function has_root_block() {
}

/**
* A string containing the main children of interactive.
* An array containing the main children of interactive.
*
* @var string
* @var array
*/
public static $children_of_interactive_block = array();

/**
* Add a root block to the variable.
*
* @param array $block The block to add.
*
* @return void
*/
public static $children_of_interactive_block = null;

/**
* Add a root block to the variable.
*
* @param array $block The block to add.
*
* @return void
*/
public static function mark_children_of_interactive_block( $block ) {
self::$children_of_interactive_block = md5( serialize( $block ) );
self::$children_of_interactive_block[] = md5( serialize( $block ) );
}

/**
* Remove a root block to the variable.
*
* @param array $block The block to remove.
* @return void
*/
public static function unmark_children_of_interactive_block() {
self::$children_of_interactive_block = null;
public static function unmark_children_of_interactive_block( $block ) {
self::$children_of_interactive_block = array_diff( self::$children_of_interactive_block, array( md5( serialize( $block ) ) ) );
}

/**
Expand All @@ -104,7 +105,7 @@ public static function unmark_children_of_interactive_block() {
* @return bool True if block is a root block, false otherwise.
*/
public static function is_marked_as_children_of_interactive_block( $block ) {
return md5( serialize( $block ) ) === self::$children_of_interactive_block;
return in_array( md5( serialize( $block ) ), self::$children_of_interactive_block, true );
}

/**
Expand Down
47 changes: 20 additions & 27 deletions lib/experimental/interactivity-api/directive-processing.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
* @subpackage Interactivity API
*/

global $children_of_interactive_block;
$children_of_interactive_block = array();
/**
* Mark if the block is a root block. Checks that there is already a root block
* in order not to mark template-parts or synced patterns as root blocks, where
Expand Down Expand Up @@ -88,36 +86,32 @@ function gutenberg_mark_interactive_block_children( $parsed_block, $source_block
*/
function gutenberg_mark_block_interactivity( $block_content, $block, $block_instance ) {
if (
isset( $block_instance->block_type->supports['interactivity'] ) &&
$block_instance->block_type->supports['interactivity']
) {
isset( $block_instance->block_type->supports['interactivity'] ) &&
$block_instance->block_type->supports['interactivity']
) {
// Mark interactive blocks so we can process them later.
/**
* Debugging purposes only. Nested comments are not allowed.
* We wrap a hidden textarea to save the block content delimited
* by comments so we can later process it.
*/
return sprintf(
'<div data-wp-delimiter="interactivity-wrapper-start" style="display:none"></div>%s<div data-wp-delimiter="interactivity-wrapper-end" style="display:none"></div>',
return get_comment_delimited_block_content(
'core/interactivity-wrapper',
array(
'blockName' => $block['blockName'],
// We can put extra information about the block here.
),
$block_content
);
} elseif ( WP_Directive_Processor::is_marked_as_children_of_interactive_block( $block ) ) {
WP_Directive_Processor::unmark_children_of_interactive_block( $block );
// Mark children of interactive blocks that are not interactive themselves
// to so we can skip them later.
WP_Directive_Processor::unmark_children_of_interactive_block();

/**
* Debugging purposes only. Nested comments are not allowed.
* We wrap a hidden textarea to save the block content delimited
* by comments so we can later process it.
*/
return sprintf(
'<div data-wp-delimiter="non-interactivity-wrapper-start" style="display:none"></div>%s<div data-wp-delimiter="non-interactivity-wrapper-end" style="display:none"></div>',
return get_comment_delimited_block_content(
'core/non-interactivity-wrapper',
array(
'blockName' => $block['blockName'],
// We can put extra information about the block here.
),
$block_content
);
}

return $block_content;
return $block_content;
}

add_filter( 'render_block', 'gutenberg_mark_block_interactivity', 10, 3 );
Expand All @@ -141,10 +135,9 @@ function gutenberg_interactivity_evaluate_reference( $path, array $context = arr
* passed context) using the subsequent path should be negated.
*/
$should_negate_value = '!' === $path[0];

$path = $should_negate_value ? substr( $path, 1 ) : $path;
$path_segments = explode( '.', $path );
$current = $store;
$path = $should_negate_value ? substr( $path, 1 ) : $path;
$path_segments = explode( '.', $path );
$current = $store;
foreach ( $path_segments as $p ) {
if ( isset( $current[ $p ] ) ) {
$current = $current[ $p ];
Expand Down

0 comments on commit ca33458

Please sign in to comment.