mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2026-07-04 17:20:07 +00:00
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: https://github.com/WordPress/gutenberg/pull/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
This commit is contained in:
@@ -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.
|
||||
*
|
||||
|
||||
@@ -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"
|
||||
}
|
||||
|
||||
1
tests/phpunit/data/blocks/notice/render.php
Normal file
1
tests/phpunit/data/blocks/notice/render.php
Normal file
@@ -0,0 +1 @@
|
||||
<p <?php echo get_block_wrapper_attributes(); ?>><?php echo esc_html( $attributes['message'] ); ?></p>
|
||||
@@ -389,9 +389,7 @@ class Tests_Blocks_Register extends WP_UnitTestCase {
|
||||
$this->assertSame(
|
||||
array(
|
||||
'message' => array(
|
||||
'type' => 'string',
|
||||
'source' => 'html',
|
||||
'selector' => '.message',
|
||||
'type' => 'string',
|
||||
),
|
||||
'lock' => array( 'type' => 'object' ),
|
||||
),
|
||||
@@ -455,6 +453,9 @@ class Tests_Blocks_Register extends WP_UnitTestCase {
|
||||
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 );
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -47,6 +47,9 @@ class Tests_Blocks_Render extends WP_UnitTestCase {
|
||||
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 @@ class Tests_Blocks_Render extends WP_UnitTestCase {
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @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
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user