-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: reusable block isn't resolved inside inner blocks (#251)
* fix: reusable block isn't resolved inside inner blocks * style: phpcs fix * style: add return type in TraverseHelpers * style: use TraverseHelpers::class
- Loading branch information
Showing
5 changed files
with
142 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@wpengine/wp-graphql-content-blocks": patch | ||
--- | ||
|
||
Bug fix. Reusable block isn't resolved inside innerBlocks. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php | ||
/** | ||
* Helper functions for traversing the list of blocks | ||
* | ||
* @package WPGraphQL\ContentBlocks\Utilities | ||
*/ | ||
|
||
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 $max_depth The maximum depth to traverse. | ||
*/ | ||
public static function traverse_blocks( &$blocks, $callback, $depth = 0, $max_depth = PHP_INT_MAX ): void { | ||
foreach ( $blocks as &$block ) { | ||
$callback( $block ); | ||
if ( ! empty( $block['innerBlocks'] ) && $depth < $max_depth ) { | ||
self::traverse_blocks( $block['innerBlocks'], $callback, $depth + 1, $max_depth ); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Example callback function to replace reusable blocks. | ||
* | ||
* @param array $block The block to potentially replace. | ||
*/ | ||
public static function replace_reusable_blocks( &$block ): void { | ||
if ( 'core/block' === $block['blockName'] && isset( $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 ); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
<?php | ||
|
||
namespace WPGraphQL\ContentBlocks\Unit; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use WPGraphQL\ContentBlocks\Utilities\TraverseHelpers; | ||
|
||
class TraverseHelpersTest extends PluginTestCase { | ||
public $post_id; | ||
public function setUp(): void { | ||
parent::setUp(); | ||
global $wpdb; | ||
|
||
$this->post_id = wp_insert_post( | ||
array( | ||
'post_title' => 'Post Title', | ||
'post_content' => preg_replace( | ||
'/\s+/', | ||
' ', | ||
trim( | ||
' | ||
<!-- wp:paragraph --> | ||
<p class="has-background" style="background-color:#a62929">Test</p> | ||
<!-- /wp:paragraph -->' | ||
) | ||
), | ||
'post_status' => 'publish', | ||
) | ||
); | ||
} | ||
|
||
public function tearDown(): void { | ||
// your tear down methods here | ||
parent::tearDown(); | ||
wp_delete_post( $this->post_id, true ); | ||
} | ||
public function testTraverseBlocks() { | ||
// Sample blocks data | ||
$blocks = [ | ||
[ | ||
'blockName' => 'core/group', | ||
'attrs' => [], | ||
'innerBlocks' => [ | ||
[ | ||
'blockName' => 'core/block', | ||
'attrs' => [ 'ref' => $this->post_id ], | ||
'innerBlocks' => [] | ||
] | ||
] | ||
], | ||
[ | ||
'blockName' => 'core/block', | ||
'attrs' => [ 'ref' => $this->post_id ], | ||
'innerBlocks' => [] | ||
] | ||
]; | ||
|
||
// Expected result after replacing reusable blocks | ||
$expected = [ | ||
[ | ||
'blockName' => 'core/group', | ||
'attrs' => [], | ||
'innerBlocks' => [ | ||
[ | ||
'blockName' => 'core/paragraph', | ||
'attrs' => [], | ||
'innerBlocks' => [], | ||
'innerHTML' => ' <p class="has-background" style="background-color:#a62929">Test</p> ', | ||
'innerContent' => [ 0 => ' <p class="has-background" style="background-color:#a62929">Test</p> '] | ||
] | ||
] | ||
], | ||
[ | ||
'blockName' => 'core/paragraph', | ||
'attrs' => [], | ||
'innerBlocks' => [], | ||
'innerHTML' => ' <p class="has-background" style="background-color:#a62929">Test</p> ', | ||
'innerContent' => [ 0 => ' <p class="has-background" style="background-color:#a62929">Test</p> '] | ||
] | ||
]; | ||
|
||
TraverseHelpers::traverse_blocks( $blocks, [ TraverseHelpers::class, 'replace_reusable_blocks' ], 0, PHP_INT_MAX ); | ||
$this->assertEquals( $expected, $blocks ); | ||
} | ||
} |