Skip to content
Closed
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
28 changes: 28 additions & 0 deletions src/wp-includes/blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
*
Expand Down
7 changes: 3 additions & 4 deletions tests/phpunit/data/blocks/notice/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@
"textdomain": "notice",
"attributes": {
"message": {
"type": "string",
"source": "html",
"selector": ".message"
"type": "string"
}
},
"supports": {
Expand Down Expand Up @@ -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"
}
1 change: 1 addition & 0 deletions tests/phpunit/data/blocks/notice/render.php
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<p <?php echo get_block_wrapper_attributes(); ?>><?php echo esc_html( $attributes['message'] ); ?></p>
7 changes: 4 additions & 3 deletions tests/phpunit/tests/blocks/register.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' ),
),
Expand Down Expand Up @@ -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 );
}

/**
Expand Down
16 changes: 16 additions & 0 deletions tests/phpunit/tests/blocks/render.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down Expand Up @@ -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( '<!-- wp:tests/notice {"message":"Hello from the test"} --><!-- /wp:tests/notice -->' );
$this->assertSame( '<p class="wp-block-tests-notice">Hello from the test</p>', trim( $actual_content ) );
}


/**
* @ticket 45109
*/
Expand Down