Skip to content

Commit 7053062

Browse files
committed
Blocks: Add new render property in block.json for block types
New `render` field in `block.json` file that accepts a string value. It allows to pass a path to the PHP file that is going to be used to render the block on the server. Related PR in Gutenberg: WordPress/gutenberg#42430. Props spacedmonkey, luisherranz, welcher, noisysocks, matveb, fabiankaegy, aristath, zieladam. Fixes #53148. git-svn-id: https://develop.svn.wordpress.org/trunk@54132 602fd350-edb4-49c9-b593-d223f7449a82
1 parent b80ba26 commit 7053062

File tree

5 files changed

+52
-7
lines changed

5 files changed

+52
-7
lines changed

src/wp-includes/blocks.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,7 @@ function get_block_metadata_i18n_schema() {
235235
* @since 5.5.0
236236
* @since 5.7.0 Added support for `textdomain` field and i18n handling for all translatable fields.
237237
* @since 5.9.0 Added support for `variations` and `viewScript` fields.
238+
* @since 6.1.0 Added support for `render` field.
238239
*
239240
* @param string $file_or_folder Path to the JSON file with metadata definition for
240241
* 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() ) {
345346
);
346347
}
347348

349+
if ( ! empty( $metadata['render'] ) ) {
350+
$template_path = wp_normalize_path(
351+
realpath(
352+
dirname( $metadata['file'] ) . '/' .
353+
remove_block_asset_path_prefix( $metadata['render'] )
354+
)
355+
);
356+
if ( file_exists( $template_path ) ) {
357+
/**
358+
* Renders the block on the server.
359+
*
360+
* @since 6.1.0
361+
*
362+
* @param array $attributes Block attributes.
363+
* @param string $content Block default content.
364+
* @param WP_Block $block Block instance.
365+
*
366+
* @return string Returns the block content.
367+
*/
368+
$settings['render_callback'] = function( $attributes, $content, $block ) use ( $template_path ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
369+
ob_start();
370+
require $template_path;
371+
return ob_get_clean();
372+
};
373+
}
374+
}
375+
348376
/**
349377
* Filters the settings determined from the block type metadata.
350378
*

tests/phpunit/data/blocks/notice/block.json

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,7 @@
2424
"textdomain": "notice",
2525
"attributes": {
2626
"message": {
27-
"type": "string",
28-
"source": "html",
29-
"selector": ".message"
27+
"type": "string"
3028
}
3129
},
3230
"supports": {
@@ -61,5 +59,6 @@
6159
"script": "tests-notice-script",
6260
"viewScript": "tests-notice-view-script",
6361
"editorStyle": "tests-notice-editor-style",
64-
"style": "tests-notice-style"
62+
"style": "tests-notice-style",
63+
"render": "file:./render.php"
6564
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<p <?php echo get_block_wrapper_attributes(); ?>><?php echo esc_html( $attributes['message'] ); ?></p>

tests/phpunit/tests/blocks/register.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -389,9 +389,7 @@ public function test_block_registers_with_metadata_fixture() {
389389
$this->assertSame(
390390
array(
391391
'message' => array(
392-
'type' => 'string',
393-
'source' => 'html',
394-
'selector' => '.message',
392+
'type' => 'string',
395393
),
396394
'lock' => array( 'type' => 'object' ),
397395
),
@@ -455,6 +453,9 @@ public function test_block_registers_with_metadata_fixture() {
455453
wp_normalize_path( realpath( DIR_TESTDATA . '/blocks/notice/block.css' ) ),
456454
wp_normalize_path( wp_styles()->get_data( 'unit-tests-test-block-style', 'path' ) )
457455
);
456+
457+
// @ticket 53148
458+
$this->assertIsCallable( $result->render_callback );
458459
}
459460

460461
/**

tests/phpunit/tests/blocks/render.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ public function tear_down() {
4747
if ( $registry->is_registered( 'core/dynamic' ) ) {
4848
$registry->unregister( 'core/dynamic' );
4949
}
50+
if ( $registry->is_registered( 'tests/notice' ) ) {
51+
$registry->unregister( 'tests/notice' );
52+
}
5053

5154
parent::tear_down();
5255
}
@@ -237,6 +240,19 @@ public function test_do_block_output( $html_filename, $server_html_filename ) {
237240
);
238241
}
239242

243+
/**
244+
* @ticket 53148
245+
*/
246+
public function test_render_field_in_block_json() {
247+
$result = register_block_type(
248+
DIR_TESTDATA . '/blocks/notice'
249+
);
250+
251+
$actual_content = do_blocks( '<!-- wp:tests/notice {"message":"Hello from the test"} --><!-- /wp:tests/notice -->' );
252+
$this->assertSame( '<p class="wp-block-tests-notice">Hello from the test</p>', trim( $actual_content ) );
253+
}
254+
255+
240256
/**
241257
* @ticket 45109
242258
*/

0 commit comments

Comments
 (0)