From 8ab984897ff3ce341f6158bdd22b57ff6d8d4de0 Mon Sep 17 00:00:00 2001 From: Bernhard Reiter Date: Wed, 24 Jul 2024 14:11:08 +0000 Subject: [PATCH] block.json: Allow passing PHP filename as `variations` field. Previously, the `variations` field in a block.json file could be used to provide a static list of the block's variations (i.e., an array). Alternatively, the block's `variation_callback` could be set during server-side block registration to point to a PHP function to generate those variations. This changeset makes it so that the block.json `variations` field can be alternatively set to a string, which will be interpreted as the filename of a PHP file that generates the variations. It is loosely modeled after [54132], which introduced the `render` field for `block.json`, as a way to point to a PHP file instead of providing a `render_callback`. Props bernhard-reiter, gziolo. Fixes #61280. Built from https://develop.svn.wordpress.org/trunk@58801 git-svn-id: http://core.svn.wordpress.org/trunk@58197 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/blocks.php | 29 +++++++++++++++++++++++++++++ wp-includes/version.php | 2 +- 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/wp-includes/blocks.php b/wp-includes/blocks.php index 3b1fc25d48..9b24b989d4 100644 --- a/wp-includes/blocks.php +++ b/wp-includes/blocks.php @@ -385,6 +385,7 @@ function get_block_metadata_i18n_schema() { * @since 6.3.0 Added `selectors` field. * @since 6.4.0 Added support for `blockHooks` field. * @since 6.5.0 Added support for `allowedBlocks`, `viewScriptModule`, and `viewStyle` fields. + * @since 6.7.0 Allow PHP filename as `variations` argument. * * @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. @@ -522,6 +523,34 @@ function register_block_type_from_metadata( $file_or_folder, $args = array() ) { } } + // If `variations` is a string, it's the name of a PHP file that + // generates the variations. + if ( ! empty( $metadata['variations'] ) && is_string( $metadata['variations'] ) ) { + $variations_path = wp_normalize_path( + realpath( + dirname( $metadata['file'] ) . '/' . + remove_block_asset_path_prefix( $metadata['variations'] ) + ) + ); + if ( $variations_path ) { + /** + * Generates the list of block variations. + * + * @since 6.7.0 + * + * @return string Returns the list of block variations. + */ + $settings['variation_callback'] = static function () use ( $variations_path ) { + $variations = require $variations_path; + return $variations; + }; + // The block instance's `variations` field is only allowed to be an array + // (of known block variations). We unset it so that the block instance will + // provide a getter that returns the result of the `variation_callback` instead. + unset( $settings['variations'] ); + } + } + $settings = array_merge( $settings, $args ); $script_fields = array( diff --git a/wp-includes/version.php b/wp-includes/version.php index 77dfabaf23..f2964dce2a 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -16,7 +16,7 @@ * * @global string $wp_version */ -$wp_version = '6.7-alpha-58800'; +$wp_version = '6.7-alpha-58801'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.