From 33efe92ea556746f4921ce619ec97e3391ea119b Mon Sep 17 00:00:00 2001 From: Mike Auteri Date: Sun, 15 Dec 2024 16:09:17 -0500 Subject: [PATCH] Small refactor. --- .../classes/blocks/class-rsvp-template.php | 34 +++---------------- includes/core/classes/class-block.php | 28 +++++++++++++++ 2 files changed, 32 insertions(+), 30 deletions(-) diff --git a/includes/core/classes/blocks/class-rsvp-template.php b/includes/core/classes/blocks/class-rsvp-template.php index 4a1776b4d..840905be2 100644 --- a/includes/core/classes/blocks/class-rsvp-template.php +++ b/includes/core/classes/blocks/class-rsvp-template.php @@ -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; @@ -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. * @@ -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(); diff --git a/includes/core/classes/class-block.php b/includes/core/classes/class-block.php index c1ea3eda3..28a5d1e08 100644 --- a/includes/core/classes/class-block.php +++ b/includes/core/classes/class-block.php @@ -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. *