diff --git a/includes/Data/ContentBlocksResolver.php b/includes/Data/ContentBlocksResolver.php index 7af99df8..7438424c 100644 --- a/includes/Data/ContentBlocksResolver.php +++ b/includes/Data/ContentBlocksResolver.php @@ -7,8 +7,8 @@ namespace WPGraphQL\ContentBlocks\Data; -use WPGraphQL\Model\Post; use WPGraphQL\ContentBlocks\Utilities\TraverseHelpers; +use WPGraphQL\Model\Post; /** * Class ContentBlocksResolver @@ -21,7 +21,7 @@ final class ContentBlocksResolver { * @param array $args GraphQL query args to pass to the connection resolver. * @param array $allowed_block_names The list of allowed block names to filter. */ - public static function resolve_content_blocks( $node, $args, $allowed_block_names = [] ): array { + public static function resolve_content_blocks( $node, $args, $allowed_block_names = [] ): array { $content = null; if ( $node instanceof Post ) { @@ -84,7 +84,7 @@ static function ( $parsed_block ) { $parsed_blocks ); // Resolve reusable blocks - replaces "core/block" with the corresponding block(s) from the reusable ref ID - TraverseHelpers::traverse_blocks($parsed_blocks, ['WPGraphQL\ContentBlocks\Utilities\TraverseHelpers', 'replace_reusable_blocks'], 0, PHP_INT_MAX); + TraverseHelpers::traverse_blocks( $parsed_blocks, [ 'WPGraphQL\ContentBlocks\Utilities\TraverseHelpers', 'replace_reusable_blocks' ], 0, PHP_INT_MAX ); // Flatten block list here if requested or if 'flat' value is not selected (default) if ( ! isset( $args['flat'] ) || 'true' == $args['flat'] ) { // phpcs:ignore Universal.Operators.StrictComparisons.LooseEqual $parsed_blocks = self::flatten_block_list( $parsed_blocks ); diff --git a/includes/Utilities/TraverseHelpers.php b/includes/Utilities/TraverseHelpers.php index c648a2d5..c0a757ea 100644 --- a/includes/Utilities/TraverseHelpers.php +++ b/includes/Utilities/TraverseHelpers.php @@ -7,34 +7,42 @@ namespace WPGraphQL\ContentBlocks\Utilities; +/** + * Class TraverseHelpers + * + * Provides utility functions to traverse and manipulate blocks. + */ final class TraverseHelpers { - /** - * Traverse blocks and apply a callback with optional depth limit. - * - * @param array &$blocks The blocks to traverse. - * @param callable $callback The callback function to apply to each block. - * @param int $depth The current depth of traversal. - * @param int $maxDepth The maximum depth to traverse. - */ - static function traverse_blocks( &$blocks, $callback, $depth = 0, $maxDepth = PHP_INT_MAX ) { - foreach ($blocks as &$block) { + * Traverse blocks and apply a callback with optional depth limit. + * + * @param array &$blocks The blocks to traverse. + * @param callable $callback The callback function to apply to each block. + * @param int $depth The current depth of traversal. + * @param int $max_depth The maximum depth to traverse. + */ + public static function traverse_blocks( &$blocks, $callback, $depth = 0, $max_depth = PHP_INT_MAX ) { + foreach ( $blocks as &$block ) { $callback( $block ); - if ( ! empty( $block['innerBlocks'] ) && $depth < $maxDepth ) { - self::traverse_blocks( $block['innerBlocks'], $callback, $depth + 1, $maxDepth ); + if ( ! empty( $block['innerBlocks'] ) && $depth < $max_depth ) { + self::traverse_blocks( $block['innerBlocks'], $callback, $depth + 1, $max_depth ); } } } - static function replace_reusable_blocks( &$block ) { + + /** + * Example callback function to replace reusable blocks. + * + * @param array $block The block to potentially replace. + */ + public static function replace_reusable_blocks( &$block ) { if ( 'core/block' === $block['blockName'] && isset( $block['attrs']['ref'] ) ) { - $post = get_post( $block['attrs']['ref'] ); + $post = get_post( $block['attrs']['ref'] ); $reusable_blocks = ! empty( $post->post_content ) ? parse_blocks( $post->post_content ) : null; if ( ! empty( $reusable_blocks ) ) { $block = array_merge( ...$reusable_blocks ); } - } } } -