-
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
- Loading branch information
Showing
5 changed files
with
135 additions
and
21 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,40 @@ | ||
<?php | ||
/** | ||
* Helper functions for traversing the list of blocks | ||
* | ||
* @package WPGraphQL\ContentBlocks\Utilities | ||
*/ | ||
|
||
namespace WPGraphQL\ContentBlocks\Utilities; | ||
|
||
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) { | ||
$callback( $block ); | ||
if ( ! empty( $block['innerBlocks'] ) && $depth < $maxDepth ) { | ||
self::traverse_blocks( $block['innerBlocks'], $callback, $depth + 1, $maxDepth ); | ||
} | ||
} | ||
} | ||
static function replace_reusable_blocks( &$block ) { | ||
Check failure on line 28 in includes/Utilities/TraverseHelpers.php GitHub Actions / phpstan
|
||
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, [ 'WPGraphQL\ContentBlocks\Utilities\TraverseHelpers', 'replace_reusable_blocks' ], 0, PHP_INT_MAX ); | ||
$this->assertEquals( $expected, $blocks ); | ||
} | ||
} |