Make WordPress Core


Ignore:
Timestamp:
09/12/2022 01:12:21 PM (22 months ago)
Author:
gziolo
Message:

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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/blocks.php

    r53892 r54132  
    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
     
    344345            'style'
    345346        );
     347    }
     348
     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        }
    346374    }
    347375
Note: See TracChangeset for help on using the changeset viewer.