Skip to content

Commit

Permalink
Blocks: Add new render property in block.json for block types
Browse files Browse the repository at this point in the history
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
  • Loading branch information
gziolo authored and = committed Nov 4, 2022
1 parent ecc2f0f commit 9cf9e30
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 7 deletions.
28 changes: 28 additions & 0 deletions src/wp-includes/blocks.php
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
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
@@ -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
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
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

0 comments on commit 9cf9e30

Please sign in to comment.