Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Merl-862] Add support for Classic Blocks #59

Merged
merged 3 commits into from
May 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions includes/Blocks/Block.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public function __construct( WP_Block_Type $block, Registry $block_registry ) {
* @return void
*/
public function register_fields() { }


private function register_block_type() {
$this->register_block_attributes_as_fields();
Expand Down Expand Up @@ -203,7 +203,21 @@ private function resolve( $block, array $args, AppContext $context, ResolveInfo
private function resolve_block_attributes( $block, $attribute_name, $attribute_config ) {
// Get default value.
$default = isset( $attribute_config['default'] ) ? $attribute_config['default'] : null;

// Case when only source defined: Classic Blocks
if ( isset( $attribute_config['source'] ) && ! isset( $attribute_config['selector'] ) ) {
$rendered_block = wp_unslash( render_block( $block ) );
$value = null;
if ( empty( $rendered_block ) ) {
return $value;
}
switch ( $attribute_config['source'] ) {
case 'html':
$value = $rendered_block;
break;
}
return $value;
}
// Case when both selector and source are defined
if ( isset( $attribute_config['selector'], $attribute_config['source'] ) ) {
$rendered_block = wp_unslash( render_block( $block ) );
$value = null;
Expand Down
18 changes: 14 additions & 4 deletions includes/Data/ContentBlocksResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,19 +50,25 @@ public static function resolve_content_blocks( $node, $args, $allowed_block_name
if ( empty( $parsed_blocks ) ) {
return array();
}
// 1st Level filtering of blocks that have no name
// 1st Level filtering of blocks that are empty
$parsed_blocks = array_filter(
$parsed_blocks,
function ( $parsed_block ) {
return isset( $parsed_block['blockName'] ) && ! empty( $parsed_block['blockName'] );
// Strip empty comments and spaces
return ! empty( trim( preg_replace('/<!--(.*)-->/Uis', '', $parsed_block['innerHTML']) ) );
},
ARRAY_FILTER_USE_BOTH
);

// 1st Level assigning of unique id's
// 1st Level assigning of unique id's and missing blockNames
$parsed_blocks = array_map(
function ( $parsed_block ) {
$parsed_block['clientId'] = uniqid();
// Since Gutenberg assigns an empty blockName for Classic block
// we define the name here
if ( empty( $parsed_block['blockName'] ) ) {
$parsed_block['blockName'] = 'core/freeform';
}
return $parsed_block;
},
$parsed_blocks
Expand All @@ -78,7 +84,7 @@ function ( $parsed_block ) {
$parsed_blocks = array_filter(
$parsed_blocks,
function ( $parsed_block ) use ( $allowed_block_names ) {
return isset( $parsed_block['blockName'] ) && in_array( $parsed_block['blockName'], $allowed_block_names, true );
return in_array( $parsed_block['blockName'], $allowed_block_names, true );
},
ARRAY_FILTER_USE_BOTH
);
Expand All @@ -88,6 +94,8 @@ function ( $parsed_block ) use ( $allowed_block_names ) {

/**
* Flattens a list blocks into a single array
*
* @param mixed $blocks A list of blocks to flatten.
*/
private static function flatten_block_list( $blocks ) {
$result = array();
Expand All @@ -99,6 +107,8 @@ private static function flatten_block_list( $blocks ) {

/**
* Flattens a block and it's inner blocks into a single while attaching unique clientId's
*
* @param mixed $block A block.
*/
private static function flatten_inner_blocks( $block ) {
$result = array();
Expand Down
2 changes: 1 addition & 1 deletion includes/Type/InterfaceType/EditorBlockInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ public static function register_type( TypeRegistry $type_registry ) {
),
'resolveType' => function ( $block ) use ( $type_registry ) {
if ( empty( $block['blockName'] ) ) {
$block['blockName'] = 'core/html';
$block['blockName'] = 'core/freeform';
}

$type_name = lcfirst( ucwords( $block['blockName'], '/' ) );
Expand Down
2 changes: 1 addition & 1 deletion includes/Type/InterfaceType/PostTypeBlockInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public static function register_type( string $post_type, $block_names, TypeRegis
),
'resolveType' => function ( $block ) use ( $type_registry ) {
if ( empty( $block['blockName'] ) ) {
$block['blockName'] = 'core/html';
$block['blockName'] = 'core/freeform';
}

$type_name = lcfirst( ucwords( $block['blockName'], '/' ) );
Expand Down
5 changes: 2 additions & 3 deletions tests/unit/BlockSupportsAnchorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ public function setUp(): void {
'
<!-- wp:paragraph -->
<p id="example">Example paragraph with Anchor</p>
<!-- /wp:paragraph --></div>
<!-- /wp:paragraph -->
<!-- wp:paragraph -->
<p>Example paragraph without Anchor</p>
<!-- /wp:paragraph --></div>
<!-- /wp:paragraph -->
'
)
),
Expand Down Expand Up @@ -109,7 +109,6 @@ public function test_register_anchor_query_field() {
}';
$actual = graphql( array( 'query' => $query ) );
$node = $actual['data']['posts']['nodes'][0];

$this->assertEquals( count( $node['editorBlocks'] ), 2 );
$this->assertEquals( $node['editorBlocks'][0]['name'], 'core/paragraph' );
$this->assertEquals( $node['editorBlocks'][0]['anchor'], 'example' );
Expand Down
13 changes: 11 additions & 2 deletions tests/unit/ContentBlocksResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ public function setUp(): void {
<!-- /wp:paragraph --></div>
<!-- /wp:column --></div>
<!-- /wp:columns -->

<!-- Classic Block -->
<p>Hello Classic Block</p>
'
)
),
Expand All @@ -58,12 +61,18 @@ public function tearDown(): void {
public function test_resolve_content_blocks_filters_empty_blocks() {
$post_model = new Post( get_post( $this->post_id ) );
$actual = $this->instance->resolve_content_blocks( $post_model, array( 'flat' => true ) );

// There should return only the non-empty blocks
$this->assertEquals( count( $actual ), 5 );
$this->assertEquals( count( $actual ), 6 );
$this->assertEquals( $actual[0]['blockName'], 'core/columns' );
}

public function test_resolve_content_blocks_resolves_classic_blocks() {
$post_model = new Post( get_post( $this->post_id ) );
$actual = $this->instance->resolve_content_blocks( $post_model, array( 'flat' => true ) );

$this->assertEquals( $actual[5]['blockName'], 'core/freeform' );
}

public function test_resolve_content_blocks_filters_blocks_not_from_allow_list() {
$post_model = new Post( get_post( $this->post_id ) );
$allowed = array( 'core/column', 'core/paragraph' );
Expand Down