diff --git a/src/wp-includes/blocks.php b/src/wp-includes/blocks.php index e731f5bd5b834..0d21094b0d8c9 100644 --- a/src/wp-includes/blocks.php +++ b/src/wp-includes/blocks.php @@ -235,6 +235,7 @@ function get_block_metadata_i18n_schema() { * @since 5.5.0 * @since 5.7.0 Added support for `textdomain` field and i18n handling for all translatable fields. * @since 5.9.0 Added support for `variations` and `viewScript` fields. + * @since 6.1.0 Added support for `render` field. * * @param string $file_or_folder Path to the JSON file with metadata definition for * the block or path to the folder where the `block.json` file is located. @@ -345,6 +346,33 @@ function register_block_type_from_metadata( $file_or_folder, $args = array() ) { ); } + if ( ! empty( $metadata['render'] ) ) { + $template_path = wp_normalize_path( + realpath( + dirname( $metadata['file'] ) . '/' . + remove_block_asset_path_prefix( $metadata['render'] ) + ) + ); + if ( file_exists( $template_path ) ) { + /** + * Renders the block on the server. + * + * @since 6.1.0 + * + * @param array $attributes Block attributes. + * @param string $content Block default content. + * @param WP_Block $block Block instance. + * + * @return string Returns the block content. + */ + $settings['render_callback'] = function( $attributes, $content, $block ) use ( $template_path ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable + ob_start(); + require $template_path; + return ob_get_clean(); + }; + } + } + /** * Filters the settings determined from the block type metadata. * diff --git a/tests/phpunit/data/blocks/notice/block.json b/tests/phpunit/data/blocks/notice/block.json index 56ffe73407a07..7874cc41bea3d 100644 --- a/tests/phpunit/data/blocks/notice/block.json +++ b/tests/phpunit/data/blocks/notice/block.json @@ -24,9 +24,7 @@ "textdomain": "notice", "attributes": { "message": { - "type": "string", - "source": "html", - "selector": ".message" + "type": "string" } }, "supports": { @@ -61,5 +59,6 @@ "script": "tests-notice-script", "viewScript": "tests-notice-view-script", "editorStyle": "tests-notice-editor-style", - "style": "tests-notice-style" + "style": "tests-notice-style", + "render": "file:./render.php" } diff --git a/tests/phpunit/data/blocks/notice/render.php b/tests/phpunit/data/blocks/notice/render.php new file mode 100644 index 0000000000000..12a49c1a5958c --- /dev/null +++ b/tests/phpunit/data/blocks/notice/render.php @@ -0,0 +1 @@ +

>

diff --git a/tests/phpunit/tests/blocks/register.php b/tests/phpunit/tests/blocks/register.php index 1b40841b50546..e80e550b30369 100644 --- a/tests/phpunit/tests/blocks/register.php +++ b/tests/phpunit/tests/blocks/register.php @@ -389,9 +389,7 @@ public function test_block_registers_with_metadata_fixture() { $this->assertSame( array( 'message' => array( - 'type' => 'string', - 'source' => 'html', - 'selector' => '.message', + 'type' => 'string', ), 'lock' => array( 'type' => 'object' ), ), @@ -455,6 +453,9 @@ public function test_block_registers_with_metadata_fixture() { wp_normalize_path( realpath( DIR_TESTDATA . '/blocks/notice/block.css' ) ), wp_normalize_path( wp_styles()->get_data( 'unit-tests-test-block-style', 'path' ) ) ); + + // @ticket 53148 + $this->assertIsCallable( $result->render_callback ); } /** diff --git a/tests/phpunit/tests/blocks/render.php b/tests/phpunit/tests/blocks/render.php index 67742e48148d8..e119517d4e498 100644 --- a/tests/phpunit/tests/blocks/render.php +++ b/tests/phpunit/tests/blocks/render.php @@ -47,6 +47,9 @@ public function tear_down() { if ( $registry->is_registered( 'core/dynamic' ) ) { $registry->unregister( 'core/dynamic' ); } + if ( $registry->is_registered( 'tests/notice' ) ) { + $registry->unregister( 'tests/notice' ); + } parent::tear_down(); } @@ -237,6 +240,19 @@ public function test_do_block_output( $html_filename, $server_html_filename ) { ); } + /** + * @ticket 53148 + */ + public function test_render_field_in_block_json() { + $result = register_block_type( + DIR_TESTDATA . '/blocks/notice' + ); + + $actual_content = do_blocks( '' ); + $this->assertSame( '

Hello from the test

', trim( $actual_content ) ); + } + + /** * @ticket 45109 */