From ac10fc68963a733aa2a4d291a801278586d92f34 Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Sat, 28 Oct 2023 01:02:26 +0000 Subject: [PATCH] Blocks: Parse the arguments earlier in `register_block_type_from_metadata()`. This makes it possible to register a block by passing an array of arguments, without the presence of a `block.json` file. Follow-up to [48141], [49948]. Props aristath, spacedmonkey, mukesh27, costdev, audrasjb, oglekler, felipeelia, hellofromTonya. Fixes #56865. Built from https://develop.svn.wordpress.org/trunk@57026 git-svn-id: http://core.svn.wordpress.org/trunk@56537 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/blocks.php | 90 ++++++++++++++++++++++------------------- wp-includes/version.php | 2 +- 2 files changed, 49 insertions(+), 43 deletions(-) diff --git a/wp-includes/blocks.php b/wp-includes/blocks.php index 01cc2070ac..ce5853d32d 100644 --- a/wp-includes/blocks.php +++ b/wp-includes/blocks.php @@ -352,13 +352,14 @@ function register_block_type_from_metadata( $file_or_folder, $args = array() ) { $file_or_folder; $is_core_block = str_starts_with( $file_or_folder, ABSPATH . WPINC ); - - if ( ! $is_core_block && ! file_exists( $metadata_file ) ) { + // If the block is not a core block, the metadata file must exist. + $metadata_file_exists = $is_core_block || file_exists( $metadata_file ); + if ( ! $metadata_file_exists && empty( $args['name'] ) ) { return false; } // Try to get metadata from the static cache for core blocks. - $metadata = false; + $metadata = array(); if ( $is_core_block ) { $core_block_name = str_replace( ABSPATH . WPINC . '/blocks/', '', $file_or_folder ); if ( ! empty( $core_blocks_meta[ $core_block_name ] ) ) { @@ -367,14 +368,15 @@ function register_block_type_from_metadata( $file_or_folder, $args = array() ) { } // If metadata is not found in the static cache, read it from the file. - if ( ! $metadata ) { + if ( $metadata_file_exists && empty( $metadata ) ) { $metadata = wp_json_file_decode( $metadata_file, array( 'associative' => true ) ); } - if ( ! is_array( $metadata ) || empty( $metadata['name'] ) ) { + if ( ! is_array( $metadata ) || ( empty( $metadata['name'] ) && empty( $args['name'] ) ) ) { return false; } - $metadata['file'] = wp_normalize_path( realpath( $metadata_file ) ); + + $metadata['file'] = $metadata_file_exists ? wp_normalize_path( realpath( $metadata_file ) ) : null; /** * Filters the metadata provided for registering a block type. @@ -404,6 +406,7 @@ function register_block_type_from_metadata( $file_or_folder, $args = array() ) { $settings = array(); $property_mappings = array( 'apiVersion' => 'api_version', + 'name' => 'name', 'title' => 'title', 'category' => 'category', 'parent' => 'parent', @@ -426,18 +429,50 @@ function register_block_type_from_metadata( $file_or_folder, $args = array() ) { foreach ( $property_mappings as $key => $mapped_key ) { if ( isset( $metadata[ $key ] ) ) { $settings[ $mapped_key ] = $metadata[ $key ]; - if ( $textdomain && isset( $i18n_schema->$key ) ) { + if ( $metadata_file_exists && $textdomain && isset( $i18n_schema->$key ) ) { $settings[ $mapped_key ] = translate_settings_using_i18n_schema( $i18n_schema->$key, $settings[ $key ], $textdomain ); } } } + if ( ! empty( $metadata['render'] ) ) { + $template_path = wp_normalize_path( + realpath( + dirname( $metadata['file'] ) . '/' . + remove_block_asset_path_prefix( $metadata['render'] ) + ) + ); + if ( $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'] = static function ( $attributes, $content, $block ) use ( $template_path ) { + ob_start(); + require $template_path; + return ob_get_clean(); + }; + } + } + + $settings = array_merge( $settings, $args ); + $script_fields = array( 'editorScript' => 'editor_script_handles', 'script' => 'script_handles', 'viewScript' => 'view_script_handles', ); foreach ( $script_fields as $metadata_field_name => $settings_field_name ) { + if ( ! empty( $settings[ $metadata_field_name ] ) ) { + $metadata[ $metadata_field_name ] = $settings[ $metadata_field_name ]; + } if ( ! empty( $metadata[ $metadata_field_name ] ) ) { $scripts = $metadata[ $metadata_field_name ]; $processed_scripts = array(); @@ -470,6 +505,9 @@ function register_block_type_from_metadata( $file_or_folder, $args = array() ) { 'style' => 'style_handles', ); foreach ( $style_fields as $metadata_field_name => $settings_field_name ) { + if ( ! empty( $settings[ $metadata_field_name ] ) ) { + $metadata[ $metadata_field_name ] = $settings[ $metadata_field_name ]; + } if ( ! empty( $metadata[ $metadata_field_name ] ) ) { $styles = $metadata[ $metadata_field_name ]; $processed_styles = array(); @@ -530,33 +568,6 @@ 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 ( $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'] = static function ( $attributes, $content, $block ) use ( $template_path ) { - ob_start(); - require $template_path; - return ob_get_clean(); - }; - } - } - /** * Filters the settings determined from the block type metadata. * @@ -565,14 +576,9 @@ function register_block_type_from_metadata( $file_or_folder, $args = array() ) { * @param array $settings Array of determined settings for registering a block type. * @param array $metadata Metadata provided for registering a block type. */ - $settings = apply_filters( - 'block_type_metadata_settings', - array_merge( - $settings, - $args - ), - $metadata - ); + $settings = apply_filters( 'block_type_metadata_settings', $settings, $metadata ); + + $metadata['name'] = ! empty( $settings['name'] ) ? $settings['name'] : $metadata['name']; return WP_Block_Type_Registry::get_instance()->register( $metadata['name'], diff --git a/wp-includes/version.php b/wp-includes/version.php index d185e5cc11..aed2d2e4af 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -16,7 +16,7 @@ * * @global string $wp_version */ -$wp_version = '6.5-alpha-57022'; +$wp_version = '6.5-alpha-57026'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.