Skip to content

Commit

Permalink
Small refactor.
Browse files Browse the repository at this point in the history
  • Loading branch information
mauteri committed Dec 15, 2024
1 parent ab1fd3f commit 33efe92
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 30 deletions.
34 changes: 4 additions & 30 deletions includes/core/classes/blocks/class-rsvp-template.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
// Exit if accessed directly.
defined( 'ABSPATH' ) || exit; // @codeCoverageIgnore

use GatherPress\Core\Block;
use GatherPress\Core\Event;
use GatherPress\Core\Traits\Singleton;
use WP_Block;
Expand Down Expand Up @@ -58,34 +59,6 @@ protected function setup_hooks(): void {
add_filter( 'render_block', array( $this, 'ensure_block_styles_loaded' ), 10, 2 );
}

/**
* Recursively retrieves all block names from a given array of blocks.
*
* This method traverses a nested block structure and collects the block names,
* including those of any inner blocks, into a flat array.
*
* @since 1.0.0
*
* @param array $blocks An array of block data, typically including `blockName` and `innerBlocks`.
*
* @return array An array of block names found within the provided block structure.
*/
private function get_block_names( array $blocks ): array {
$block_names = array();

if ( isset( $blocks['blockName'] ) ) {
$block_names[] = $blocks['blockName'];
}

if ( ! empty( $blocks['innerBlocks'] ) ) {
foreach ( $blocks['innerBlocks'] as $inner_block ) {
$block_names = array_merge( $block_names, $this->get_block_names( $inner_block ) );
}
}

return $block_names;
}

/**
* Ensures that the required block styles are loaded for the `gatherpress/rsvp-template` block.
*
Expand All @@ -101,11 +74,12 @@ private function get_block_names( array $blocks ): array {
*/
public function ensure_block_styles_loaded( string $block_content, array $block ): string {
if ( 'gatherpress/rsvp-template' === $block['blockName'] ) {
$tag = new WP_HTML_Tag_Processor( $block_content );
$block_instance = Block::get_instance();
$tag = new WP_HTML_Tag_Processor( $block_content );

if ( $tag->next_tag() ) {
$inner_blocks = (array) json_decode( $tag->get_attribute( 'data-blocks' ), true );
$inner_blocks = $this->get_block_names( $inner_blocks );
$inner_blocks = $block_instance->get_block_names( $inner_blocks );

foreach ( $inner_blocks as $inner_block ) {
$block_registry = WP_Block_Type_Registry::get_instance();
Expand Down
28 changes: 28 additions & 0 deletions includes/core/classes/class-block.php
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,34 @@ public function modify_hooked_blocks_in_patterns( ?array $parsed_hooked_block, s
return $parsed_hooked_block;
}

/**
* Recursively retrieves all block names from a given array of blocks.
*
* This method traverses a nested block structure and collects the block names,
* including those of any inner blocks, into a flat array.
*
* @since 1.0.0
*
* @param array $blocks An array of block data, typically including `blockName` and `innerBlocks`.
*
* @return array An array of block names found within the provided block structure.
*/
public function get_block_names( array $blocks ): array {
$block_names = array();

if ( isset( $blocks['blockName'] ) ) {
$block_names[] = $blocks['blockName'];
}

if ( ! empty( $blocks['innerBlocks'] ) ) {
foreach ( $blocks['innerBlocks'] as $inner_block ) {
$block_names = array_merge( $block_names, $this->get_block_names( $inner_block ) );
}
}

return $block_names;
}

/**
* Locates a specific tag within a block structure.
*
Expand Down

0 comments on commit 33efe92

Please sign in to comment.