diff --git a/plugins/woocommerce/src/Admin/BlockTemplates/BlockInterface.php b/plugins/woocommerce/src/Admin/BlockTemplates/BlockInterface.php index e4c4a382ab9..12a784b50ac 100644 --- a/plugins/woocommerce/src/Admin/BlockTemplates/BlockInterface.php +++ b/plugins/woocommerce/src/Admin/BlockTemplates/BlockInterface.php @@ -70,6 +70,11 @@ interface BlockInterface { */ public function &get_root_template(): BlockTemplateInterface; + /** + * Remove the block from its parent. + */ + public function remove(); + /** * Check if the block is detached from its parent or root template. * diff --git a/plugins/woocommerce/src/Internal/Admin/BlockTemplates/AbstractBlock.php b/plugins/woocommerce/src/Internal/Admin/BlockTemplates/AbstractBlock.php index 0e1266192da..2af5c74c0c1 100644 --- a/plugins/woocommerce/src/Internal/Admin/BlockTemplates/AbstractBlock.php +++ b/plugins/woocommerce/src/Internal/Admin/BlockTemplates/AbstractBlock.php @@ -173,6 +173,13 @@ class AbstractBlock implements BlockInterface { return $this->parent; } + /** + * Remove the block from its parent. + */ + public function remove() { + $this->parent->remove_block( $this->id ); + } + /** * Check if the block is detached from its parent block container or the template it belongs to. * diff --git a/plugins/woocommerce/tests/php/src/Internal/Admin/BlockTemplates/BlockTest.php b/plugins/woocommerce/tests/php/src/Internal/Admin/BlockTemplates/BlockTest.php index 17ab308fe59..93aeb45b223 100644 --- a/plugins/woocommerce/tests/php/src/Internal/Admin/BlockTemplates/BlockTest.php +++ b/plugins/woocommerce/tests/php/src/Internal/Admin/BlockTemplates/BlockTest.php @@ -232,6 +232,27 @@ class BlockTest extends WC_Unit_Test_Case { ); } + /** + * Test that removing a block by calling remove on it detaches it. + */ + public function test_remove_block_self() { + $template = new BlockTemplate(); + + $block = $template->add_block( + [ + 'id' => 'test-block-id', + 'blockName' => 'test-block-name', + ] + ); + + $block->remove(); + + $this->assertTrue( + $block->is_detached(), + 'Failed asserting that the block is detached from its parent and root template.' + ); + } + /** * Test that adding nested blocks sets the parent and root template correctly. */