diff --git a/src/wp-includes/blocks.php b/src/wp-includes/blocks.php index b04696d591..2ec3a07e3e 100644 --- a/src/wp-includes/blocks.php +++ b/src/wp-includes/blocks.php @@ -7,6 +7,39 @@ * @since 5.0.0 */ +/** + * Registers a block type. + * + * @since 5.0.0 + * + * @param string|WP_Block_Type $name Block type name including namespace, or alternatively a + * complete WP_Block_Type instance. In case a WP_Block_Type + * is provided, the $args parameter will be ignored. + * @param array $args { + * Optional. Array of block type arguments. Any arguments may be defined, however the + * ones described below are supported by default. Default empty array. + * + * @type callable $render_callback Callback used to render blocks of this block type. + * } + * @return WP_Block_Type|false The registered block type on success, or false on failure. + */ +function register_block_type( $name, $args = array() ) { + return WP_Block_Type_Registry::get_instance()->register( $name, $args ); +} + +/** + * Unregisters a block type. + * + * @since 5.0.0 + * + * @param string|WP_Block_Type $name Block type name including namespace, or alternatively a + * complete WP_Block_Type instance. + * @return WP_Block_Type|false The unregistered block type on success, or false on failure. + */ +function unregister_block_type( $name ) { + return WP_Block_Type_Registry::get_instance()->unregister( $name ); +} + /** * Determine whether a post or content string has blocks. * @@ -59,3 +92,23 @@ function has_block( $block_type, $post = null ) { return false !== strpos( $post, ' + +

First Gutenberg Paragraph

+ +

Second Auto Paragraph

+ + +

Third Gutenberg Paragraph

+ +

Third Auto Paragraph

+ +

[someshortcode]

+

And some content?!

+

[/someshortcode]

+ diff --git a/tests/phpunit/data/blocks/do-blocks-original.html b/tests/phpunit/data/blocks/do-blocks-original.html new file mode 100644 index 0000000000..bbd2eb4bb1 --- /dev/null +++ b/tests/phpunit/data/blocks/do-blocks-original.html @@ -0,0 +1,25 @@ +

First Auto Paragraph

+ + + + +

First Gutenberg Paragraph

+ + +

Second Auto Paragraph

+ + + + +

Third Gutenberg Paragraph

+ + +

Third Auto Paragraph

+ + +[someshortcode] + +And some content?! + +[/someshortcode] + diff --git a/tests/phpunit/tests/blocks/register.php b/tests/phpunit/tests/blocks/register.php new file mode 100644 index 0000000000..8271c9babd --- /dev/null +++ b/tests/phpunit/tests/blocks/register.php @@ -0,0 +1,141 @@ +post->create( + array( + 'post_content' => file_get_contents( DIR_TESTDATA . '/blocks/do-blocks-original.html' ), + ) + ); + } + + /** + * Tear down after class. + * + * @since 5.0.0 + */ + public static function wpTearDownAfterClass() { + // Also deletes revisions. + wp_delete_post( self::$post_id, true ); + } + + /** + * Empty render function for tests to use. + */ + function render_stub() {} + + /** + * Tear down after each test. + * + * @since 5.0.0 + */ + function tearDown() { + parent::tearDown(); + + $registry = WP_Block_Type_Registry::get_instance(); + + foreach ( array( 'test-static', 'test-dynamic' ) as $block_name ) { + $block_name = 'core/' . $block_name; + + if ( $registry->is_registered( $block_name ) ) { + $registry->unregister( $block_name ); + } + } + } + + /** + * @ticket 45109 + */ + function test_register_affects_main_registry() { + $name = 'core/test-static'; + $settings = array( + 'icon' => 'text', + ); + + register_block_type( $name, $settings ); + + $registry = WP_Block_Type_Registry::get_instance(); + $this->assertTrue( $registry->is_registered( $name ) ); + } + + /** + * @ticket 45109 + */ + function test_unregister_affects_main_registry() { + $name = 'core/test-static'; + $settings = array( + 'icon' => 'text', + ); + + register_block_type( $name, $settings ); + unregister_block_type( $name ); + + $registry = WP_Block_Type_Registry::get_instance(); + $this->assertFalse( $registry->is_registered( $name ) ); + } + + /** + * @ticket 45109 + */ + function test_get_dynamic_block_names() { + register_block_type( 'core/test-static', array() ); + register_block_type( 'core/test-dynamic', array( 'render_callback' => array( $this, 'render_stub' ) ) ); + + $dynamic_block_names = get_dynamic_block_names(); + + $this->assertContains( 'core/test-dynamic', $dynamic_block_names ); + $this->assertNotContains( 'core/test-static', $dynamic_block_names ); + } + + /** + * @ticket 45109 + */ + function test_has_blocks() { + // Test with passing post ID. + $this->assertTrue( has_blocks( self::$post_id ) ); + + // Test with passing WP_Post object. + $this->assertTrue( has_blocks( get_post( self::$post_id ) ) ); + + // Test with passing content string. + $this->assertTrue( has_blocks( get_post( self::$post_id ) ) ); + + // Test default. + $this->assertFalse( has_blocks() ); + $query = new WP_Query( array( 'post__in' => array( self::$post_id ) ) ); + $query->the_post(); + $this->assertTrue( has_blocks() ); + + // Test string (without blocks). + $content = file_get_contents( DIR_TESTDATA . '/blocks/do-blocks-expected.html' ); + $this->assertFalse( has_blocks( $content ) ); + } +}