From 2a31ff4e6356168740752223a81a1c55ea30d902 Mon Sep 17 00:00:00 2001 From: Ari Stathopoulos Date: Thu, 20 Oct 2022 09:50:53 +0300 Subject: [PATCH 01/31] pass $args earlier --- src/wp-includes/blocks.php | 110 ++++++++++++++++++------------------- 1 file changed, 55 insertions(+), 55 deletions(-) diff --git a/src/wp-includes/blocks.php b/src/wp-includes/blocks.php index 9a8c3b06f47b3..4f3ceeafe7a7c 100644 --- a/src/wp-includes/blocks.php +++ b/src/wp-includes/blocks.php @@ -321,28 +321,27 @@ function register_block_type_from_metadata( $file_or_folder, $args = array() ) { trailingslashit( $file_or_folder ) . 'block.json' : $file_or_folder; - if ( ! file_exists( $metadata_file ) ) { - return false; - } - - // Try to get metadata from the static cache for core blocks. - $metadata = false; - if ( str_starts_with( $file_or_folder, ABSPATH . WPINC ) ) { - $core_block_name = str_replace( ABSPATH . WPINC . '/blocks/', '', $file_or_folder ); - if ( ! empty( $core_blocks_meta[ $core_block_name ] ) ) { - $metadata = $core_blocks_meta[ $core_block_name ]; + $metadata_file_exists = file_exists( $metadata_file ); + if ( $metadata_file_exists ) { + // Try to get metadata from the static cache for core blocks. + $metadata = false; + if ( str_starts_with( $file_or_folder, ABSPATH . WPINC ) ) { + $core_block_name = str_replace( ABSPATH . WPINC . '/blocks/', '', $file_or_folder ); + if ( ! empty( $core_blocks_meta[ $core_block_name ] ) ) { + $metadata = $core_blocks_meta[ $core_block_name ]; + } } - } - // If metadata is not found in the static cache, read it from the file. - if ( ! $metadata ) { - $metadata = wp_json_file_decode( $metadata_file, array( 'associative' => true ) ); - } + // If metadata is not found in the static cache, read it from the file. + if ( ! $metadata ) { + $metadata = wp_json_file_decode( $metadata_file, array( 'associative' => true ) ); + } - if ( ! is_array( $metadata ) || empty( $metadata['name'] ) ) { - return false; + if ( ! is_array( $metadata ) || empty( $metadata['name'] ) ) { + return false; + } + $metadata['file'] = wp_normalize_path( realpath( $metadata_file ) ); } - $metadata['file'] = wp_normalize_path( realpath( $metadata_file ) ); /** * Filters the metadata provided for registering a block type. @@ -351,7 +350,13 @@ function register_block_type_from_metadata( $file_or_folder, $args = array() ) { * * @param array $metadata Metadata for registering a block type. */ - $metadata = apply_filters( 'block_type_metadata', $metadata ); + $metadata = apply_filters( + 'block_type_metadata', + array_merge( + $metadata, + $args + ) + ); // Add `style` and `editor_style` for core blocks if missing. if ( ! empty( $metadata['name'] ) && 0 === strpos( $metadata['name'], 'core/' ) ) { @@ -365,32 +370,34 @@ function register_block_type_from_metadata( $file_or_folder, $args = array() ) { } } - $settings = array(); - $property_mappings = array( - 'apiVersion' => 'api_version', - 'title' => 'title', - 'category' => 'category', - 'parent' => 'parent', - 'ancestor' => 'ancestor', - 'icon' => 'icon', - 'description' => 'description', - 'keywords' => 'keywords', - 'attributes' => 'attributes', - 'providesContext' => 'provides_context', - 'usesContext' => 'uses_context', - 'supports' => 'supports', - 'styles' => 'styles', - 'variations' => 'variations', - 'example' => 'example', - ); - $textdomain = ! empty( $metadata['textdomain'] ) ? $metadata['textdomain'] : null; - $i18n_schema = get_block_metadata_i18n_schema(); - - foreach ( $property_mappings as $key => $mapped_key ) { - if ( isset( $metadata[ $key ] ) ) { - $settings[ $mapped_key ] = $metadata[ $key ]; - if ( $textdomain && isset( $i18n_schema->$key ) ) { - $settings[ $mapped_key ] = translate_settings_using_i18n_schema( $i18n_schema->$key, $settings[ $key ], $textdomain ); + $settings = array(); + if ( $metadata_file_exists ) { + $property_mappings = array( + 'apiVersion' => 'api_version', + 'title' => 'title', + 'category' => 'category', + 'parent' => 'parent', + 'ancestor' => 'ancestor', + 'icon' => 'icon', + 'description' => 'description', + 'keywords' => 'keywords', + 'attributes' => 'attributes', + 'providesContext' => 'provides_context', + 'usesContext' => 'uses_context', + 'supports' => 'supports', + 'styles' => 'styles', + 'variations' => 'variations', + 'example' => 'example', + ); + $textdomain = ! empty( $metadata['textdomain'] ) ? $metadata['textdomain'] : null; + $i18n_schema = get_block_metadata_i18n_schema(); + + foreach ( $property_mappings as $key => $mapped_key ) { + if ( isset( $metadata[ $key ] ) ) { + $settings[ $mapped_key ] = $metadata[ $key ]; + if ( $textdomain && isset( $i18n_schema->$key ) ) { + $settings[ $mapped_key ] = translate_settings_using_i18n_schema( $i18n_schema->$key, $settings[ $key ], $textdomain ); + } } } } @@ -461,12 +468,12 @@ function register_block_type_from_metadata( $file_or_folder, $args = array() ) { } if ( ! empty( $metadata['render'] ) ) { - $template_path = wp_normalize_path( + $template_path = $metadata_file_exists ? wp_normalize_path( realpath( dirname( $metadata['file'] ) . '/' . remove_block_asset_path_prefix( $metadata['render'] ) ) - ); + ) : ''; if ( $template_path ) { /** * Renders the block on the server. @@ -495,14 +502,7 @@ 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 ); return WP_Block_Type_Registry::get_instance()->register( $metadata['name'], From 7daab55d1d5dcc3d42c11b63b9b38a3bb08b8e64 Mon Sep 17 00:00:00 2001 From: Ari Stathopoulos Date: Thu, 20 Oct 2022 09:59:02 +0300 Subject: [PATCH 02/31] Add empty string as default value for $file_or_folder --- src/wp-includes/blocks.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wp-includes/blocks.php b/src/wp-includes/blocks.php index 4f3ceeafe7a7c..0f467d361713e 100644 --- a/src/wp-includes/blocks.php +++ b/src/wp-includes/blocks.php @@ -305,7 +305,7 @@ function get_block_metadata_i18n_schema() { * on accepted arguments. Default empty array. * @return WP_Block_Type|false The registered block type on success, or false on failure. */ -function register_block_type_from_metadata( $file_or_folder, $args = array() ) { +function register_block_type_from_metadata( $file_or_folder = '', $args = array() ) { /* * Get an array of metadata from a PHP file. * This improves performance for core blocks as it's only necessary to read a single PHP file From 51f6083522892418049db2653d1ed93e026f9fc6 Mon Sep 17 00:00:00 2001 From: Ari Stathopoulos Date: Thu, 20 Oct 2022 10:03:31 +0300 Subject: [PATCH 03/31] $metadata is an array --- src/wp-includes/blocks.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/wp-includes/blocks.php b/src/wp-includes/blocks.php index 0f467d361713e..205be541c04e3 100644 --- a/src/wp-includes/blocks.php +++ b/src/wp-includes/blocks.php @@ -321,10 +321,10 @@ function register_block_type_from_metadata( $file_or_folder = '', $args = array( trailingslashit( $file_or_folder ) . 'block.json' : $file_or_folder; + $metadata = array(); $metadata_file_exists = file_exists( $metadata_file ); if ( $metadata_file_exists ) { // Try to get metadata from the static cache for core blocks. - $metadata = false; if ( str_starts_with( $file_or_folder, ABSPATH . WPINC ) ) { $core_block_name = str_replace( ABSPATH . WPINC . '/blocks/', '', $file_or_folder ); if ( ! empty( $core_blocks_meta[ $core_block_name ] ) ) { @@ -333,7 +333,7 @@ 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 ( empty( $metadata ) ) { $metadata = wp_json_file_decode( $metadata_file, array( 'associative' => true ) ); } From ef39e83206f705d9effa26a611af2961c1335aa9 Mon Sep 17 00:00:00 2001 From: Ari Stathopoulos Date: Thu, 20 Oct 2022 10:10:51 +0300 Subject: [PATCH 04/31] fix for translations conditions --- src/wp-includes/blocks.php | 54 ++++++++++++++++++-------------------- 1 file changed, 26 insertions(+), 28 deletions(-) diff --git a/src/wp-includes/blocks.php b/src/wp-includes/blocks.php index 205be541c04e3..67db62cb0bb49 100644 --- a/src/wp-includes/blocks.php +++ b/src/wp-includes/blocks.php @@ -370,34 +370,32 @@ function register_block_type_from_metadata( $file_or_folder = '', $args = array( } } - $settings = array(); - if ( $metadata_file_exists ) { - $property_mappings = array( - 'apiVersion' => 'api_version', - 'title' => 'title', - 'category' => 'category', - 'parent' => 'parent', - 'ancestor' => 'ancestor', - 'icon' => 'icon', - 'description' => 'description', - 'keywords' => 'keywords', - 'attributes' => 'attributes', - 'providesContext' => 'provides_context', - 'usesContext' => 'uses_context', - 'supports' => 'supports', - 'styles' => 'styles', - 'variations' => 'variations', - 'example' => 'example', - ); - $textdomain = ! empty( $metadata['textdomain'] ) ? $metadata['textdomain'] : null; - $i18n_schema = get_block_metadata_i18n_schema(); - - foreach ( $property_mappings as $key => $mapped_key ) { - if ( isset( $metadata[ $key ] ) ) { - $settings[ $mapped_key ] = $metadata[ $key ]; - if ( $textdomain && isset( $i18n_schema->$key ) ) { - $settings[ $mapped_key ] = translate_settings_using_i18n_schema( $i18n_schema->$key, $settings[ $key ], $textdomain ); - } + $settings = array(); + $property_mappings = array( + 'apiVersion' => 'api_version', + 'title' => 'title', + 'category' => 'category', + 'parent' => 'parent', + 'ancestor' => 'ancestor', + 'icon' => 'icon', + 'description' => 'description', + 'keywords' => 'keywords', + 'attributes' => 'attributes', + 'providesContext' => 'provides_context', + 'usesContext' => 'uses_context', + 'supports' => 'supports', + 'styles' => 'styles', + 'variations' => 'variations', + 'example' => 'example', + ); + $textdomain = ! empty( $metadata['textdomain'] ) ? $metadata['textdomain'] : null; + $i18n_schema = get_block_metadata_i18n_schema(); + + foreach ( $property_mappings as $key => $mapped_key ) { + if ( isset( $metadata[ $key ] ) ) { + $settings[ $mapped_key ] = $metadata[ $key ]; + if ( $metadata_file_exists && $textdomain && isset( $i18n_schema->$key ) ) { + $settings[ $mapped_key ] = translate_settings_using_i18n_schema( $i18n_schema->$key, $settings[ $key ], $textdomain ); } } } From 392b8aea0b0c90abb02f1f2bd22309a96bab5b89 Mon Sep 17 00:00:00 2001 From: Ari Stathopoulos Date: Thu, 20 Oct 2022 10:47:06 +0300 Subject: [PATCH 05/31] Return false if name is not defined --- src/wp-includes/blocks.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/wp-includes/blocks.php b/src/wp-includes/blocks.php index 67db62cb0bb49..323175e9a9ef7 100644 --- a/src/wp-includes/blocks.php +++ b/src/wp-includes/blocks.php @@ -358,6 +358,10 @@ function register_block_type_from_metadata( $file_or_folder = '', $args = array( ) ); + if ( ! isset( $metadata['name'] ) ) { + return false; + } + // Add `style` and `editor_style` for core blocks if missing. if ( ! empty( $metadata['name'] ) && 0 === strpos( $metadata['name'], 'core/' ) ) { $block_name = str_replace( 'core/', '', $metadata['name'] ); From b236265489119a8c41579c4f050704cf847966f4 Mon Sep 17 00:00:00 2001 From: Ari Stathopoulos Date: Thu, 20 Oct 2022 11:02:07 +0300 Subject: [PATCH 06/31] removed default value --- src/wp-includes/blocks.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wp-includes/blocks.php b/src/wp-includes/blocks.php index 323175e9a9ef7..c09868d10d235 100644 --- a/src/wp-includes/blocks.php +++ b/src/wp-includes/blocks.php @@ -305,7 +305,7 @@ function get_block_metadata_i18n_schema() { * on accepted arguments. Default empty array. * @return WP_Block_Type|false The registered block type on success, or false on failure. */ -function register_block_type_from_metadata( $file_or_folder = '', $args = array() ) { +function register_block_type_from_metadata( $file_or_folder, $args = array() ) { /* * Get an array of metadata from a PHP file. * This improves performance for core blocks as it's only necessary to read a single PHP file From 92ac48a8d749c518baf6bfac0a030ceed23e3635 Mon Sep 17 00:00:00 2001 From: Ari Stathopoulos Date: Thu, 20 Oct 2022 11:11:16 +0300 Subject: [PATCH 07/31] simplify condition --- src/wp-includes/blocks.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/wp-includes/blocks.php b/src/wp-includes/blocks.php index c09868d10d235..aaf283a54dc02 100644 --- a/src/wp-includes/blocks.php +++ b/src/wp-includes/blocks.php @@ -317,9 +317,10 @@ function register_block_type_from_metadata( $file_or_folder, $args = array() ) { $core_blocks_meta = include_once ABSPATH . WPINC . '/blocks/blocks-json.php'; } - $metadata_file = ( ! str_ends_with( $file_or_folder, 'block.json' ) ) ? - trailingslashit( $file_or_folder ) . 'block.json' : - $file_or_folder; + $metadata_file = $file_or_folder; + if ( ! str_ends_with( $file_or_folder, 'block.json' ) ) { + $metadata_file = trailingslashit( $file_or_folder ) . 'block.json'; + } $metadata = array(); $metadata_file_exists = file_exists( $metadata_file ); From c5edb5b90c5f97fb74ece7c2d8b1527a6c870f51 Mon Sep 17 00:00:00 2001 From: Ari Stathopoulos Date: Thu, 20 Oct 2022 11:14:06 +0300 Subject: [PATCH 08/31] fix for core blocks caches --- src/wp-includes/blocks.php | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/src/wp-includes/blocks.php b/src/wp-includes/blocks.php index aaf283a54dc02..9eb1c9407ed56 100644 --- a/src/wp-includes/blocks.php +++ b/src/wp-includes/blocks.php @@ -324,15 +324,16 @@ function register_block_type_from_metadata( $file_or_folder, $args = array() ) { $metadata = array(); $metadata_file_exists = file_exists( $metadata_file ); - if ( $metadata_file_exists ) { - // Try to get metadata from the static cache for core blocks. - if ( str_starts_with( $file_or_folder, ABSPATH . WPINC ) ) { - $core_block_name = str_replace( ABSPATH . WPINC . '/blocks/', '', $file_or_folder ); - if ( ! empty( $core_blocks_meta[ $core_block_name ] ) ) { - $metadata = $core_blocks_meta[ $core_block_name ]; - } + + // Try to get metadata from the static cache for core blocks. + if ( str_starts_with( $file_or_folder, ABSPATH . WPINC ) ) { + $core_block_name = str_replace( ABSPATH . WPINC . '/blocks/', '', $file_or_folder ); + if ( ! empty( $core_blocks_meta[ $core_block_name ] ) ) { + $metadata = $core_blocks_meta[ $core_block_name ]; } + } + if ( $metadata_file_exists && empty( $metadata ) ) { // If metadata is not found in the static cache, read it from the file. if ( empty( $metadata ) ) { $metadata = wp_json_file_decode( $metadata_file, array( 'associative' => true ) ); From b5ecea7e6b54aa438d4c9a515549629e6f0585b3 Mon Sep 17 00:00:00 2001 From: Ari Stathopoulos Date: Thu, 20 Oct 2022 11:50:12 +0300 Subject: [PATCH 09/31] move render_callback calculations before parsing the $args --- src/wp-includes/blocks.php | 54 +++++++++++++++++++------------------- 1 file changed, 27 insertions(+), 27 deletions(-) diff --git a/src/wp-includes/blocks.php b/src/wp-includes/blocks.php index 9eb1c9407ed56..1fe1363a79a75 100644 --- a/src/wp-includes/blocks.php +++ b/src/wp-includes/blocks.php @@ -345,6 +345,33 @@ function register_block_type_from_metadata( $file_or_folder, $args = array() ) { $metadata['file'] = wp_normalize_path( realpath( $metadata_file ) ); } + if ( ! empty( $metadata['render'] ) ) { + $template_path = $metadata_file_exists ? 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'] = function( $attributes, $content, $block ) use ( $template_path ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable + ob_start(); + require $template_path; + return ob_get_clean(); + }; + } + } + /** * Filters the metadata provided for registering a block type. * @@ -471,33 +498,6 @@ function register_block_type_from_metadata( $file_or_folder, $args = array() ) { } } - if ( ! empty( $metadata['render'] ) ) { - $template_path = $metadata_file_exists ? 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'] = 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. * From 000ef58e15e6e78b3f1a77c9e1dce4fe58fba8d4 Mon Sep 17 00:00:00 2001 From: Ari Stathopoulos Date: Thu, 20 Oct 2022 12:24:57 +0300 Subject: [PATCH 10/31] more WIP --- src/wp-includes/blocks.php | 90 +++++++++++++++++--------------------- 1 file changed, 39 insertions(+), 51 deletions(-) diff --git a/src/wp-includes/blocks.php b/src/wp-includes/blocks.php index 1fe1363a79a75..85804849130ba 100644 --- a/src/wp-includes/blocks.php +++ b/src/wp-includes/blocks.php @@ -317,15 +317,13 @@ function register_block_type_from_metadata( $file_or_folder, $args = array() ) { $core_blocks_meta = include_once ABSPATH . WPINC . '/blocks/blocks-json.php'; } - $metadata_file = $file_or_folder; - if ( ! str_ends_with( $file_or_folder, 'block.json' ) ) { - $metadata_file = trailingslashit( $file_or_folder ) . 'block.json'; - } - - $metadata = array(); + $metadata_file = ( ! str_ends_with( $file_or_folder, 'block.json' ) ) ? + trailingslashit( $file_or_folder ) . 'block.json' : + $file_or_folder; $metadata_file_exists = file_exists( $metadata_file ); // Try to get metadata from the static cache for core blocks. + $metadata = array(); if ( str_starts_with( $file_or_folder, ABSPATH . WPINC ) ) { $core_block_name = str_replace( ABSPATH . WPINC . '/blocks/', '', $file_or_folder ); if ( ! empty( $core_blocks_meta[ $core_block_name ] ) ) { @@ -333,44 +331,17 @@ 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_file_exists && empty( $metadata ) ) { - // If metadata is not found in the static cache, read it from the file. - if ( empty( $metadata ) ) { - $metadata = wp_json_file_decode( $metadata_file, array( 'associative' => true ) ); - } - - if ( ! is_array( $metadata ) || empty( $metadata['name'] ) ) { - return false; - } - $metadata['file'] = wp_normalize_path( realpath( $metadata_file ) ); + $metadata = wp_json_file_decode( $metadata_file, array( 'associative' => true ) ); } - if ( ! empty( $metadata['render'] ) ) { - $template_path = $metadata_file_exists ? 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'] = function( $attributes, $content, $block ) use ( $template_path ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable - ob_start(); - require $template_path; - return ob_get_clean(); - }; - } + $metadata = array_merge( $metadata, $args ); + + if ( ! is_array( $metadata ) || empty( $metadata['name'] ) ) { + return false; } + $metadata['file'] = $metadata_file_exists ? wp_normalize_path( realpath( $metadata_file ) ) : null; /** * Filters the metadata provided for registering a block type. @@ -379,17 +350,7 @@ function register_block_type_from_metadata( $file_or_folder, $args = array() ) { * * @param array $metadata Metadata for registering a block type. */ - $metadata = apply_filters( - 'block_type_metadata', - array_merge( - $metadata, - $args - ) - ); - - if ( ! isset( $metadata['name'] ) ) { - return false; - } + $metadata = apply_filters( 'block_type_metadata', $metadata ); // Add `style` and `editor_style` for core blocks if missing. if ( ! empty( $metadata['name'] ) && 0 === strpos( $metadata['name'], 'core/' ) ) { @@ -498,6 +459,33 @@ function register_block_type_from_metadata( $file_or_folder, $args = array() ) { } } + if ( ! empty( $metadata['render'] ) ) { + $template_path = $metadata_file_exists ? 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'] = 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. * From 35e526ed6e8e1ccbdf069ac5c30ac90db766cce6 Mon Sep 17 00:00:00 2001 From: Ari Stathopoulos Date: Thu, 20 Oct 2022 12:35:54 +0300 Subject: [PATCH 11/31] more render callback a bit earlier --- src/wp-includes/blocks.php | 58 +++++++++++++++++++------------------- 1 file changed, 29 insertions(+), 29 deletions(-) diff --git a/src/wp-includes/blocks.php b/src/wp-includes/blocks.php index 85804849130ba..3459654cbd257 100644 --- a/src/wp-includes/blocks.php +++ b/src/wp-includes/blocks.php @@ -336,8 +336,6 @@ function register_block_type_from_metadata( $file_or_folder, $args = array() ) { $metadata = wp_json_file_decode( $metadata_file, array( 'associative' => true ) ); } - $metadata = array_merge( $metadata, $args ); - if ( ! is_array( $metadata ) || empty( $metadata['name'] ) ) { return false; } @@ -394,6 +392,35 @@ function register_block_type_from_metadata( $file_or_folder, $args = array() ) { } } + if ( ! empty( $metadata['render'] ) ) { + $template_path = $metadata_file_exists ? 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'] = function( $attributes, $content, $block ) use ( $template_path ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable + ob_start(); + require $template_path; + return ob_get_clean(); + }; + } + } + + $settings = array_merge( $settings, $args ); + $script_fields = array( 'editorScript' => 'editor_script_handles', 'script' => 'script_handles', @@ -459,33 +486,6 @@ function register_block_type_from_metadata( $file_or_folder, $args = array() ) { } } - if ( ! empty( $metadata['render'] ) ) { - $template_path = $metadata_file_exists ? 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'] = 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. * From 14cad2c7677803115b5ceb1520b78d3160e96cad Mon Sep 17 00:00:00 2001 From: Ari Stathopoulos Date: Thu, 20 Oct 2022 12:45:31 +0300 Subject: [PATCH 12/31] tweak the check --- src/wp-includes/blocks.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/wp-includes/blocks.php b/src/wp-includes/blocks.php index 3459654cbd257..062d0e90a3a4a 100644 --- a/src/wp-includes/blocks.php +++ b/src/wp-includes/blocks.php @@ -322,6 +322,10 @@ function register_block_type_from_metadata( $file_or_folder, $args = array() ) { $file_or_folder; $metadata_file_exists = 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 = array(); if ( str_starts_with( $file_or_folder, ABSPATH . WPINC ) ) { From 6d426b50ae7bdd400a2447329b05bf69f67e07fe Mon Sep 17 00:00:00 2001 From: Ari Stathopoulos Date: Thu, 20 Oct 2022 12:46:22 +0300 Subject: [PATCH 13/31] minor tweak --- src/wp-includes/blocks.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/wp-includes/blocks.php b/src/wp-includes/blocks.php index 062d0e90a3a4a..4e116ec59883b 100644 --- a/src/wp-includes/blocks.php +++ b/src/wp-includes/blocks.php @@ -317,11 +317,11 @@ function register_block_type_from_metadata( $file_or_folder, $args = array() ) { $core_blocks_meta = include_once ABSPATH . WPINC . '/blocks/blocks-json.php'; } - $metadata_file = ( ! str_ends_with( $file_or_folder, 'block.json' ) ) ? + $metadata_file = ( ! str_ends_with( $file_or_folder, 'block.json' ) ) ? trailingslashit( $file_or_folder ) . 'block.json' : $file_or_folder; - $metadata_file_exists = file_exists( $metadata_file ); + $metadata_file_exists = file_exists( $metadata_file ); if ( ! $metadata_file_exists && empty( $args['name'] ) ) { return false; } From d2f497746d93fff3edbf1019138f9ca368fbd668 Mon Sep 17 00:00:00 2001 From: Ari Stathopoulos Date: Thu, 2 Mar 2023 11:16:50 +0200 Subject: [PATCH 14/31] Add PHPUnit tests & fix discovered bug --- src/wp-includes/blocks.php | 10 +++- tests/phpunit/tests/blocks/register.php | 66 +++++++++++++++++++++++++ 2 files changed, 75 insertions(+), 1 deletion(-) diff --git a/src/wp-includes/blocks.php b/src/wp-includes/blocks.php index 4e116ec59883b..cf6b79668643e 100644 --- a/src/wp-includes/blocks.php +++ b/src/wp-includes/blocks.php @@ -340,7 +340,7 @@ function register_block_type_from_metadata( $file_or_folder, $args = array() ) { $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'] = $metadata_file_exists ? wp_normalize_path( realpath( $metadata_file ) ) : null; @@ -369,6 +369,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', @@ -431,6 +432,9 @@ function register_block_type_from_metadata( $file_or_folder, $args = array() ) { '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(); @@ -463,6 +467,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(); @@ -500,6 +507,7 @@ function register_block_type_from_metadata( $file_or_folder, $args = array() ) { */ $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'], $settings diff --git a/tests/phpunit/tests/blocks/register.php b/tests/phpunit/tests/blocks/register.php index ca906086a36aa..dfbd9bb16c982 100644 --- a/tests/phpunit/tests/blocks/register.php +++ b/tests/phpunit/tests/blocks/register.php @@ -431,6 +431,72 @@ public function test_metadata_not_found_in_the_current_directory() { $this->assertFalse( $result ); } + /** + * Test registering a block using arguments instead of a block.json file. + * + * @ticket 56865 + */ + public function test_register_block_type_from_metadata_with_arguments() { + $result = register_block_type_from_metadata( + '', + array( + 'api_version' => 2, + 'name' => 'tests/notice-from-array', + 'title' => 'Notice from array', + 'category' => 'common', + 'icon' => 'star', + 'description' => 'Shows warning, error or success notices… (registered from an array)', + 'keywords' => array( + 'alert', + 'message', + ), + 'textdomain' => 'notice-from-array', + ) + ); + + $this->assertInstanceOf( 'WP_Block_Type', $result ); + $this->assertSame( 2, $result->api_version ); + $this->assertSame( 'tests/notice-from-array', $result->name ); + $this->assertSame( 'Notice from array', $result->title ); + $this->assertSame( 'common', $result->category ); + $this->assertSame( 'star', $result->icon ); + $this->assertSame( 'Shows warning, error or success notices… (registered from an array)', $result->description ); + $this->assertSameSets( array( 'alert', 'message' ), $result->keywords ); + } + + /** + * Tests that the function returns the registered block when the `block.json` + * is found in the fixtures directory. + * + * @ticket 56865 + */ + public function test_block_registers_with_args_override() { + $result = register_block_type_from_metadata( + DIR_TESTDATA . '/blocks/notice', + array( + 'name' => 'tests/notice-with-overrides', + 'title' => 'Overriden title', + 'style' => array( 'tests-notice-style-overriden' ), + ) + ); + + $this->assertInstanceOf( 'WP_Block_Type', $result ); + $this->assertSame( 2, $result->api_version ); + $this->assertSame( 'tests/notice-with-overrides', $result->name ); + $this->assertSame( 'Overriden title', $result->title ); + $this->assertSameSets( + array( 'tests-notice-editor-script' ), + $result->editor_script_handles + ); + $this->assertSameSets( + array( 'tests-notice-style-overriden' ), + $result->style_handles + ); + + // @ticket 53148 + $this->assertIsCallable( $result->render_callback ); + } + /** * Tests that the function returns the registered block when the `block.json` * is found in the fixtures directory. From 39c2c0d04951afe7ac25af078d7c295bffda67d9 Mon Sep 17 00:00:00 2001 From: Ari Stathopoulos Date: Thu, 2 Mar 2023 11:17:48 +0200 Subject: [PATCH 15/31] Update src/wp-includes/blocks.php Co-authored-by: Mukesh Panchal --- src/wp-includes/blocks.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/wp-includes/blocks.php b/src/wp-includes/blocks.php index cf6b79668643e..0c812db70bc50 100644 --- a/src/wp-includes/blocks.php +++ b/src/wp-includes/blocks.php @@ -413,7 +413,6 @@ function register_block_type_from_metadata( $file_or_folder, $args = array() ) { * @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 From 536666e8ca7e77d20021975f2c679c35b719be95 Mon Sep 17 00:00:00 2001 From: Ari Stathopoulos Date: Thu, 2 Mar 2023 11:19:48 +0200 Subject: [PATCH 16/31] docs fix (copy/paste error) --- tests/phpunit/tests/blocks/register.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/phpunit/tests/blocks/register.php b/tests/phpunit/tests/blocks/register.php index dfbd9bb16c982..3c3e7cadcbc7f 100644 --- a/tests/phpunit/tests/blocks/register.php +++ b/tests/phpunit/tests/blocks/register.php @@ -465,8 +465,7 @@ public function test_register_block_type_from_metadata_with_arguments() { } /** - * Tests that the function returns the registered block when the `block.json` - * is found in the fixtures directory. + * Tests that the $args can properly override the block.json file. * * @ticket 56865 */ From 45d3229198001f7b11e81f8a91af4ef74f954de2 Mon Sep 17 00:00:00 2001 From: Ari Stathopoulos Date: Thu, 2 Mar 2023 12:11:24 +0200 Subject: [PATCH 17/31] Update tests/phpunit/tests/blocks/register.php Co-authored-by: Colin Stewart <79332690+costdev@users.noreply.github.com> --- tests/phpunit/tests/blocks/register.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/phpunit/tests/blocks/register.php b/tests/phpunit/tests/blocks/register.php index 3c3e7cadcbc7f..a23ac2785ed36 100644 --- a/tests/phpunit/tests/blocks/register.php +++ b/tests/phpunit/tests/blocks/register.php @@ -432,9 +432,11 @@ public function test_metadata_not_found_in_the_current_directory() { } /** - * Test registering a block using arguments instead of a block.json file. + * Tests registering a block using arguments instead of a block.json file. * * @ticket 56865 + * + * @covers ::register_block_type_from_metadata */ public function test_register_block_type_from_metadata_with_arguments() { $result = register_block_type_from_metadata( From 910df65afb0b34444b5200388a84d08ab595a6af Mon Sep 17 00:00:00 2001 From: Ari Stathopoulos Date: Thu, 2 Mar 2023 12:11:35 +0200 Subject: [PATCH 18/31] Update tests/phpunit/tests/blocks/register.php Co-authored-by: Colin Stewart <79332690+costdev@users.noreply.github.com> --- tests/phpunit/tests/blocks/register.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/phpunit/tests/blocks/register.php b/tests/phpunit/tests/blocks/register.php index a23ac2785ed36..3867bae273afa 100644 --- a/tests/phpunit/tests/blocks/register.php +++ b/tests/phpunit/tests/blocks/register.php @@ -470,6 +470,8 @@ public function test_register_block_type_from_metadata_with_arguments() { * Tests that the $args can properly override the block.json file. * * @ticket 56865 + * + * @covers ::register_block_type_from_metadata */ public function test_block_registers_with_args_override() { $result = register_block_type_from_metadata( From fe37d142f6cd3bd11c83eacd50e00093d4ae3a76 Mon Sep 17 00:00:00 2001 From: Ari Stathopoulos Date: Thu, 2 Mar 2023 12:12:38 +0200 Subject: [PATCH 19/31] copy/paste --- tests/phpunit/tests/blocks/register.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/tests/phpunit/tests/blocks/register.php b/tests/phpunit/tests/blocks/register.php index 3867bae273afa..34d86f1df7fa2 100644 --- a/tests/phpunit/tests/blocks/register.php +++ b/tests/phpunit/tests/blocks/register.php @@ -495,8 +495,6 @@ public function test_block_registers_with_args_override() { array( 'tests-notice-style-overriden' ), $result->style_handles ); - - // @ticket 53148 $this->assertIsCallable( $result->render_callback ); } From b0913e1c7917ce75017e43abc5bef1df70fbf479 Mon Sep 17 00:00:00 2001 From: Ari Stathopoulos Date: Thu, 2 Mar 2023 12:26:01 +0200 Subject: [PATCH 20/31] wording improvement --- tests/phpunit/tests/blocks/register.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/phpunit/tests/blocks/register.php b/tests/phpunit/tests/blocks/register.php index 34d86f1df7fa2..32bac24a8ee2e 100644 --- a/tests/phpunit/tests/blocks/register.php +++ b/tests/phpunit/tests/blocks/register.php @@ -467,7 +467,7 @@ public function test_register_block_type_from_metadata_with_arguments() { } /** - * Tests that the $args can properly override the block.json file. + * Tests that defined $args can properly override the block.json file. * * @ticket 56865 * From 7df228e4f647fe0806db3a7445da6dcf968df66d Mon Sep 17 00:00:00 2001 From: Ari Stathopoulos Date: Thu, 2 Mar 2023 13:10:51 +0200 Subject: [PATCH 21/31] Add messages to tests --- tests/phpunit/tests/blocks/register.php | 34 +++++++++++++++---------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/tests/phpunit/tests/blocks/register.php b/tests/phpunit/tests/blocks/register.php index 32bac24a8ee2e..1bdfd24201d74 100644 --- a/tests/phpunit/tests/blocks/register.php +++ b/tests/phpunit/tests/blocks/register.php @@ -456,14 +456,18 @@ public function test_register_block_type_from_metadata_with_arguments() { ) ); - $this->assertInstanceOf( 'WP_Block_Type', $result ); - $this->assertSame( 2, $result->api_version ); - $this->assertSame( 'tests/notice-from-array', $result->name ); - $this->assertSame( 'Notice from array', $result->title ); - $this->assertSame( 'common', $result->category ); - $this->assertSame( 'star', $result->icon ); - $this->assertSame( 'Shows warning, error or success notices… (registered from an array)', $result->description ); - $this->assertSameSets( array( 'alert', 'message' ), $result->keywords ); + $this->assertInstanceOf( 'WP_Block_Type', $result, 'The block was not registered' ); + $this->assertSame( 2, $result->api_version, 'The API version is incorrect' ); + $this->assertSame( 'tests/notice-from-array', $result->name, 'The block name is incorrect' ); + $this->assertSame( 'Notice from array', $result->title, 'The block title is incorrect' ); + $this->assertSame( 'common', $result->category, 'The block category is incorrect' ); + $this->assertSame( 'star', $result->icon, 'The block icon is incorrect' ); + $this->assertSame( + 'Shows warning, error or success notices… (registered from an array)', + $result->description, + 'The block description is incorrect' + ); + $this->assertSameSets( array( 'alert', 'message' ), $result->keywords, 'The block keywords are incorrect' ); } /** @@ -483,17 +487,19 @@ public function test_block_registers_with_args_override() { ) ); - $this->assertInstanceOf( 'WP_Block_Type', $result ); - $this->assertSame( 2, $result->api_version ); - $this->assertSame( 'tests/notice-with-overrides', $result->name ); - $this->assertSame( 'Overriden title', $result->title ); + $this->assertInstanceOf( 'WP_Block_Type', $result, 'The block was not registered' ); + $this->assertSame( 2, $result->api_version, 'The API version is incorrect' ); + $this->assertSame( 'tests/notice-with-overrides', $result->name, 'The block name was not overriden' ); + $this->assertSame( 'Overriden title', $result->title, 'The block title was not overriden' ); $this->assertSameSets( array( 'tests-notice-editor-script' ), - $result->editor_script_handles + $result->editor_script_handles, + 'The block editor script is incorrect' ); $this->assertSameSets( array( 'tests-notice-style-overriden' ), - $result->style_handles + $result->style_handles, + 'The block style was not overriden' ); $this->assertIsCallable( $result->render_callback ); } From 927a6a41001379c17db2cd539f82d997bf11ec14 Mon Sep 17 00:00:00 2001 From: Ari Stathopoulos Date: Fri, 3 Mar 2023 09:32:00 +0200 Subject: [PATCH 22/31] typo fix --- src/wp-includes/class-wp-theme-json.php | 4 ++-- tests/phpunit/tests/blocks/register.php | 10 +++++----- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/wp-includes/class-wp-theme-json.php b/src/wp-includes/class-wp-theme-json.php index 20f15e015bfca..3494cbbe082c9 100644 --- a/src/wp-includes/class-wp-theme-json.php +++ b/src/wp-includes/class-wp-theme-json.php @@ -82,8 +82,8 @@ class WP_Theme_JSON { * - prevent_override => Disables override of default presets by theme presets. * The relationship between whether to override the defaults * and whether the defaults are enabled is inverse: - * - If defaults are enabled => theme presets should not be overriden - * - If defaults are disabled => theme presets should be overriden + * - If defaults are enabled => theme presets should not be overridden + * - If defaults are disabled => theme presets should be overridden * For example, a theme sets defaultPalette to false, * making the default palette hidden from the user. * In that case, we want all the theme presets to be present, diff --git a/tests/phpunit/tests/blocks/register.php b/tests/phpunit/tests/blocks/register.php index 1bdfd24201d74..24af74e5c3705 100644 --- a/tests/phpunit/tests/blocks/register.php +++ b/tests/phpunit/tests/blocks/register.php @@ -483,23 +483,23 @@ public function test_block_registers_with_args_override() { array( 'name' => 'tests/notice-with-overrides', 'title' => 'Overriden title', - 'style' => array( 'tests-notice-style-overriden' ), + 'style' => array( 'tests-notice-style-overridden' ), ) ); $this->assertInstanceOf( 'WP_Block_Type', $result, 'The block was not registered' ); $this->assertSame( 2, $result->api_version, 'The API version is incorrect' ); - $this->assertSame( 'tests/notice-with-overrides', $result->name, 'The block name was not overriden' ); - $this->assertSame( 'Overriden title', $result->title, 'The block title was not overriden' ); + $this->assertSame( 'tests/notice-with-overrides', $result->name, 'The block name was not overridden' ); + $this->assertSame( 'Overriden title', $result->title, 'The block title was not overridden' ); $this->assertSameSets( array( 'tests-notice-editor-script' ), $result->editor_script_handles, 'The block editor script is incorrect' ); $this->assertSameSets( - array( 'tests-notice-style-overriden' ), + array( 'tests-notice-style-overridden' ), $result->style_handles, - 'The block style was not overriden' + 'The block style was not overridden' ); $this->assertIsCallable( $result->render_callback ); } From d5dfea3b1247947fd01baca8ab1519af3ca0ac03 Mon Sep 17 00:00:00 2001 From: Ari Stathopoulos Date: Tue, 4 Jul 2023 11:36:42 +0300 Subject: [PATCH 23/31] fix failing test from merge conflict --- src/wp-includes/blocks.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wp-includes/blocks.php b/src/wp-includes/blocks.php index cd34e9059686c..75077a17890c1 100644 --- a/src/wp-includes/blocks.php +++ b/src/wp-includes/blocks.php @@ -334,7 +334,7 @@ function register_block_type_from_metadata( $file_or_folder, $args = array() ) { $is_core_block = str_starts_with( $file_or_folder, ABSPATH . WPINC ); $metadata_file_exists = file_exists( $metadata_file ); - if ( ! $is_core_block && ! $metadata_file_exists ) { + if ( ! $is_core_block && ! $metadata_file_exists && empty( $args['name'] ) ) { return false; } From d1d47bc3e236271c90738ed34bfafd7bd401521a Mon Sep 17 00:00:00 2001 From: Ari Stathopoulos Date: Thu, 6 Jul 2023 11:29:09 +0300 Subject: [PATCH 24/31] Add more tests --- tests/phpunit/tests/blocks/register.php | 38 +++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/tests/phpunit/tests/blocks/register.php b/tests/phpunit/tests/blocks/register.php index 1c018bbbab79c..ea88ce3b842be 100644 --- a/tests/phpunit/tests/blocks/register.php +++ b/tests/phpunit/tests/blocks/register.php @@ -642,6 +642,44 @@ public function test_block_registers_with_args_override() { $this->assertIsCallable( $result->render_callback ); } + public function data_register_block_registers_with_args_override_returns_false_when_name_is_missing() { + return array( + 'no block.json file and no name argument' => array( + 'file' => '', // No block.json file. + 'args' => array( + 'title' => 'Overriden title', + 'style' => array( 'tests-notice-style-overridden' ), + ), + ), + 'existing file and args not an array' => array( + // A file that exists but is empty. This will bypass the file_exists() check. + 'file' => DIR_TESTDATA . '/blocks/notice/block.js', + 'args' => false, + ), + 'existing file and args[name] missing' => array( + // A file that exists but is empty. This will bypass the file_exists() check. + 'file' => DIR_TESTDATA . '/blocks/notice/block.js', + 'args' => array( + 'title' => 'Overriden title', + 'style' => array( 'tests-notice-style-overridden' ), + ), + ), + ); + } + + /** + * Tests that when the `name` is missing, `register_block_type_from_metadata` + * will return `false`. + * + * @ticket 56865 + * @dataProvider data_register_block_registers_with_args_override_returns_false_when_name_is_missing + * + * @covers ::register_block_type_from_metadata + */ + public function test_block_registers_with_args_override_returns_false_when_name_is_missing( $file, $args ) { + $this->assertFalse( register_block_type_from_metadata( $file, $args ) ); + } + /** * Tests that the function returns the registered block when the `block.json` * is found in the fixtures directory. From 614a5225ce44d7189a5794c8fe69da5c49ebc599 Mon Sep 17 00:00:00 2001 From: Ari Stathopoulos Date: Mon, 10 Jul 2023 10:11:24 +0300 Subject: [PATCH 25/31] Update tests/phpunit/tests/blocks/register.php Co-authored-by: Colin Stewart <79332690+costdev@users.noreply.github.com> --- tests/phpunit/tests/blocks/register.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tests/phpunit/tests/blocks/register.php b/tests/phpunit/tests/blocks/register.php index ea88ce3b842be..4eddb4824b737 100644 --- a/tests/phpunit/tests/blocks/register.php +++ b/tests/phpunit/tests/blocks/register.php @@ -642,6 +642,11 @@ public function test_block_registers_with_args_override() { $this->assertIsCallable( $result->render_callback ); } + /** + * Data provider. + * + * @return array[] + */ public function data_register_block_registers_with_args_override_returns_false_when_name_is_missing() { return array( 'no block.json file and no name argument' => array( From 69c8372cf7d6acbbf8bf3d6bbd09721f6e625edd Mon Sep 17 00:00:00 2001 From: Ari Stathopoulos Date: Mon, 10 Jul 2023 10:12:55 +0300 Subject: [PATCH 26/31] Update tests/phpunit/tests/blocks/register.php Co-authored-by: Colin Stewart <79332690+costdev@users.noreply.github.com> --- tests/phpunit/tests/blocks/register.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/tests/phpunit/tests/blocks/register.php b/tests/phpunit/tests/blocks/register.php index 4eddb4824b737..2aa0bfbf3c682 100644 --- a/tests/phpunit/tests/blocks/register.php +++ b/tests/phpunit/tests/blocks/register.php @@ -677,9 +677,12 @@ public function data_register_block_registers_with_args_override_returns_false_w * will return `false`. * * @ticket 56865 + * @covers ::register_block_type_from_metadata + * * @dataProvider data_register_block_registers_with_args_override_returns_false_when_name_is_missing * - * @covers ::register_block_type_from_metadata + * @param string $file The metadata file. + * @param array $args Array of block type arguments. */ public function test_block_registers_with_args_override_returns_false_when_name_is_missing( $file, $args ) { $this->assertFalse( register_block_type_from_metadata( $file, $args ) ); From 3b5fb1563f3cbdb954113a8a02f248ca6b8fa7da Mon Sep 17 00:00:00 2001 From: Ari Stathopoulos Date: Mon, 10 Jul 2023 10:14:53 +0300 Subject: [PATCH 27/31] Move dataProvider function --- tests/phpunit/tests/blocks/register.php | 32 ++++++++++++------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/tests/phpunit/tests/blocks/register.php b/tests/phpunit/tests/blocks/register.php index 2aa0bfbf3c682..4902440eb8249 100644 --- a/tests/phpunit/tests/blocks/register.php +++ b/tests/phpunit/tests/blocks/register.php @@ -642,6 +642,22 @@ public function test_block_registers_with_args_override() { $this->assertIsCallable( $result->render_callback ); } + /** + * Tests that when the `name` is missing, `register_block_type_from_metadata` + * will return `false`. + * + * @ticket 56865 + * @covers ::register_block_type_from_metadata + * + * @dataProvider data_register_block_registers_with_args_override_returns_false_when_name_is_missing + * + * @param string $file The metadata file. + * @param array $args Array of block type arguments. + */ + public function test_block_registers_with_args_override_returns_false_when_name_is_missing( $file, $args ) { + $this->assertFalse( register_block_type_from_metadata( $file, $args ) ); + } + /** * Data provider. * @@ -672,22 +688,6 @@ public function data_register_block_registers_with_args_override_returns_false_w ); } - /** - * Tests that when the `name` is missing, `register_block_type_from_metadata` - * will return `false`. - * - * @ticket 56865 - * @covers ::register_block_type_from_metadata - * - * @dataProvider data_register_block_registers_with_args_override_returns_false_when_name_is_missing - * - * @param string $file The metadata file. - * @param array $args Array of block type arguments. - */ - public function test_block_registers_with_args_override_returns_false_when_name_is_missing( $file, $args ) { - $this->assertFalse( register_block_type_from_metadata( $file, $args ) ); - } - /** * Tests that the function returns the registered block when the `block.json` * is found in the fixtures directory. From 4a6136b7414f08ff2ee33e84121d4a4a33977422 Mon Sep 17 00:00:00 2001 From: Ari Stathopoulos Date: Mon, 10 Jul 2023 11:28:03 +0300 Subject: [PATCH 28/31] Update tests/phpunit/tests/blocks/register.php Co-authored-by: Mukesh Panchal --- tests/phpunit/tests/blocks/register.php | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/phpunit/tests/blocks/register.php b/tests/phpunit/tests/blocks/register.php index 4902440eb8249..1b191015eacc5 100644 --- a/tests/phpunit/tests/blocks/register.php +++ b/tests/phpunit/tests/blocks/register.php @@ -647,6 +647,7 @@ public function test_block_registers_with_args_override() { * will return `false`. * * @ticket 56865 + * * @covers ::register_block_type_from_metadata * * @dataProvider data_register_block_registers_with_args_override_returns_false_when_name_is_missing From 47586a993f204bc39bdf1a49f24399849e180c9f Mon Sep 17 00:00:00 2001 From: Ari Stathopoulos Date: Tue, 18 Jul 2023 14:10:50 +0300 Subject: [PATCH 29/31] Don't check if file exists for core blocks --- src/wp-includes/blocks.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/wp-includes/blocks.php b/src/wp-includes/blocks.php index 41fcd82d5fffd..79937e0c6fb0b 100644 --- a/src/wp-includes/blocks.php +++ b/src/wp-includes/blocks.php @@ -358,8 +358,9 @@ function register_block_type_from_metadata( $file_or_folder, $args = array() ) { trailingslashit( $file_or_folder ) . 'block.json' : $file_or_folder; - $is_core_block = str_starts_with( $file_or_folder, ABSPATH . WPINC ); - $metadata_file_exists = file_exists( $metadata_file ); + $is_core_block = str_starts_with( $file_or_folder, ABSPATH . WPINC ); + // If the block is not a core block, the metadata file must exist. + $metadata_file_exists = $is_core_block || file_exists( $metadata_file ); if ( ! $is_core_block && ! $metadata_file_exists && empty( $args['name'] ) ) { return false; } From 280992d422e3a878aed65a9aaa36d5cf18f979d5 Mon Sep 17 00:00:00 2001 From: Ari Stathopoulos Date: Wed, 19 Jul 2023 10:43:56 +0300 Subject: [PATCH 30/31] resolve rebase conflicts --- src/wp-includes/blocks.php | 30 ++---------------------------- 1 file changed, 2 insertions(+), 28 deletions(-) diff --git a/src/wp-includes/blocks.php b/src/wp-includes/blocks.php index 79937e0c6fb0b..8b6dfe94a8b83 100644 --- a/src/wp-includes/blocks.php +++ b/src/wp-includes/blocks.php @@ -457,9 +457,10 @@ function register_block_type_from_metadata( $file_or_folder, $args = array() ) { * @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 + $settings['render_callback'] = static function( $attributes, $content, $block ) use ( $template_path ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable ob_start(); require $template_path; return ob_get_clean(); @@ -540,33 +541,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 ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable - ob_start(); - require $template_path; - return ob_get_clean(); - }; - } - } - /** * Filters the settings determined from the block type metadata. * From a8983621c5c33c4b439f6f84f08feceb3d32e057 Mon Sep 17 00:00:00 2001 From: Ari Stathopoulos Date: Wed, 19 Jul 2023 11:49:35 +0300 Subject: [PATCH 31/31] Update src/wp-includes/blocks.php Co-authored-by: Jonny Harris --- src/wp-includes/blocks.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wp-includes/blocks.php b/src/wp-includes/blocks.php index 8b6dfe94a8b83..03a4c1571745e 100644 --- a/src/wp-includes/blocks.php +++ b/src/wp-includes/blocks.php @@ -361,7 +361,7 @@ function register_block_type_from_metadata( $file_or_folder, $args = array() ) { $is_core_block = str_starts_with( $file_or_folder, ABSPATH . WPINC ); // If the block is not a core block, the metadata file must exist. $metadata_file_exists = $is_core_block || file_exists( $metadata_file ); - if ( ! $is_core_block && ! $metadata_file_exists && empty( $args['name'] ) ) { + if ( ! $metadata_file_exists && empty( $args['name'] ) ) { return false; }