Skip to content

Commit

Permalink
style: phpcs fix
Browse files Browse the repository at this point in the history
  • Loading branch information
theodesp committed Jun 28, 2024
1 parent b4f050c commit cf81fc3
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 19 deletions.
6 changes: 3 additions & 3 deletions includes/Data/ContentBlocksResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@

namespace WPGraphQL\ContentBlocks\Data;

use WPGraphQL\Model\Post;
use WPGraphQL\ContentBlocks\Utilities\TraverseHelpers;
use WPGraphQL\Model\Post;

/**
* Class ContentBlocksResolver
Expand All @@ -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 ) {

Expand Down Expand Up @@ -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 );
Expand Down
40 changes: 24 additions & 16 deletions includes/Utilities/TraverseHelpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) {

Check failure on line 24 in includes/Utilities/TraverseHelpers.php

View workflow job for this annotation

GitHub Actions / phpstan

Method WPGraphQL\ContentBlocks\Utilities\TraverseHelpers::traverse_blocks() has no return type specified.
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 ) {

Check failure on line 38 in includes/Utilities/TraverseHelpers.php

View workflow job for this annotation

GitHub Actions / phpstan

Method WPGraphQL\ContentBlocks\Utilities\TraverseHelpers::replace_reusable_blocks() has no return type specified.
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 );
}

}
}
}

0 comments on commit cf81fc3

Please sign in to comment.