Start implementation of 'get_comment_delimited_template' method

This commit is contained in:
Nathan Schneider 2024-06-10 14:37:24 -03:00
parent 214d6341a0
commit 9096ddd331
4 changed files with 71 additions and 0 deletions

View File

@ -152,4 +152,9 @@ interface BlockInterface {
* @return array The block configuration as a formatted template.
*/
public function get_formatted_template(): array;
/**
* TODO
*/
public function get_comment_delimited_template();
}

View File

@ -200,6 +200,26 @@ trait BlockContainerTrait {
return $arr;
}
/**
* TODO
*/
public function get_comment_delimited_template() {
$arr = [];
$inner_blocks = $this->get_inner_blocks_sorted_by_order();
if ( ! empty( $inner_blocks ) ) {
$arr = array_map(
function( BlockInterface $block ) {
return $block->get_comment_delimited_template();
},
$inner_blocks
);
}
return get_comment_delimited_block_content( $this->get_name(), $this->get_attributes(), "\n<div>\n" . implode( "\n", $arr ) . "\n</div>\n" );
}
/**
* Do the `woocommerce_block_template_after_add_block` action.
* Handle exceptions thrown by the action.

View File

@ -31,6 +31,12 @@ trait BlockFormattedTemplateTrait {
return $arr;
}
/**
* TODO
*/
public function get_comment_delimited_template() {
return get_comment_delimited_block_content( $this->get_name(), $this->get_attributes(), '' );
}
/**
* Get the block hide conditions formatted for inclusion in a formatted template.

View File

@ -674,4 +674,44 @@ class BlockTest extends WC_Unit_Test_Case {
$this->assertSame( 'test-value', $block->get_attributes()['test-attr'] );
}
public function test_get_comment_delimited_formatted_template() {
$template = new BlockTemplate();
$block = $template->add_block(
array(
'id' => 'test-block-id',
'blockName' => 'test-block-name',
'attributes' => array(
'attr-1' => 'value-1',
'attr-2' => 'value-2',
),
)
);
$block->add_hide_condition( 'foo === bar' );
$block->add_disable_condition( 'test > 100' );
$block->add_block(
array(
'id' => 'test-block-id-2',
'blockName' => 'test-block-name-2',
'attributes' => array(
'attr-3' => 'value-3',
'attr-4' => 'value-4',
),
)
);
$block->add_block(
array(
'id' => 'test-block-id-3',
'blockName' => 'test-block-name-3',
)
);
$this->assertSame( 'test-value', $block->get_comment_delimited_template() ); // TODO it's a wrong assumption.
}
}