From b0f4c2c37e68a808c7b16a1ff5267baac621f430 Mon Sep 17 00:00:00 2001 From: gziolo Date: Mon, 12 Sep 2022 13:14:13 +0000 Subject: [PATCH] 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. Built from https://develop.svn.wordpress.org/trunk@54132 git-svn-id: http://core.svn.wordpress.org/trunk@53691 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/blocks.php | 28 ++++++++++++++++++++++++++++ wp-includes/version.php | 2 +- 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/wp-includes/blocks.php b/wp-includes/blocks.php index e731f5bd5b..0d21094b0d 100644 --- a/wp-includes/blocks.php +++ b/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/wp-includes/version.php b/wp-includes/version.php index 0a477f71bb..e3d8ab83d4 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -16,7 +16,7 @@ * * @global string $wp_version */ -$wp_version = '6.1-alpha-54131'; +$wp_version = '6.1-alpha-54132'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.