From dcfea21e2652884b90b01b39926a54a59ea64390 Mon Sep 17 00:00:00 2001 From: Felix Arntz Date: Wed, 5 Mar 2025 22:17:23 +0000 Subject: [PATCH] Editor: Fix block type and block metadata collection registration issues on Windows due to lack of path normalization. Props flixos90, gziolo, joemcgill. Fixes #63027. Built from https://develop.svn.wordpress.org/trunk@59938 git-svn-id: http://core.svn.wordpress.org/trunk@59280 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/blocks.php | 4 +++- .../class-wp-block-metadata-registry.php | 21 +++++++++++-------- wp-includes/version.php | 2 +- 3 files changed, 16 insertions(+), 11 deletions(-) diff --git a/wp-includes/blocks.php b/wp-includes/blocks.php index 4d3ad3d4bd..31f1b22469 100644 --- a/wp-includes/blocks.php +++ b/wp-includes/blocks.php @@ -445,11 +445,13 @@ function register_block_type_from_metadata( $file_or_folder, $args = array() ) { * Using a static variable ensures that the metadata is only read once per request. */ + $file_or_folder = wp_normalize_path( $file_or_folder ); + $metadata_file = ( ! str_ends_with( $file_or_folder, 'block.json' ) ) ? trailingslashit( $file_or_folder ) . 'block.json' : $file_or_folder; - $is_core_block = str_starts_with( $file_or_folder, ABSPATH . WPINC ); + $is_core_block = str_starts_with( $file_or_folder, wp_normalize_path( ABSPATH . WPINC ) ); $metadata_file_exists = $is_core_block || file_exists( $metadata_file ); $registry_metadata = WP_Block_Metadata_Registry::get_metadata( $file_or_folder ); diff --git a/wp-includes/class-wp-block-metadata-registry.php b/wp-includes/class-wp-block-metadata-registry.php index e7e71afcd1..cba8193b7e 100644 --- a/wp-includes/class-wp-block-metadata-registry.php +++ b/wp-includes/class-wp-block-metadata-registry.php @@ -82,7 +82,7 @@ class WP_Block_Metadata_Registry { * @return bool True if the collection was registered successfully, false otherwise. */ public static function register_collection( $path, $manifest ) { - $path = wp_normalize_path( rtrim( $path, '/' ) ); + $path = rtrim( wp_normalize_path( $path ), '/' ); $collection_roots = self::get_default_collection_roots(); @@ -112,7 +112,7 @@ class WP_Block_Metadata_Registry { $collection_roots = array_unique( array_map( static function ( $allowed_root ) { - return rtrim( $allowed_root, '/' ); + return rtrim( wp_normalize_path( $allowed_root ), '/' ); }, $collection_roots ) @@ -161,6 +161,8 @@ class WP_Block_Metadata_Registry { * @return array|null The block metadata for the block, or null if not found. */ public static function get_metadata( $file_or_folder ) { + $file_or_folder = wp_normalize_path( $file_or_folder ); + $path = self::find_collection_path( $file_or_folder ); if ( ! $path ) { return null; @@ -194,7 +196,7 @@ class WP_Block_Metadata_Registry { * @return string[] List of block metadata file paths, or an empty array if the given `$path` is invalid. */ public static function get_collection_block_metadata_files( $path ) { - $path = wp_normalize_path( rtrim( $path, '/' ) ); + $path = rtrim( wp_normalize_path( $path ), '/' ); if ( ! isset( self::$collections[ $path ] ) ) { _doing_it_wrong( @@ -213,6 +215,7 @@ class WP_Block_Metadata_Registry { } return array_map( + // No normalization necessary since `$path` is already normalized and `$block_name` is just a folder name. static function ( $block_name ) use ( $path ) { return "{$path}/{$block_name}/block.json"; }, @@ -225,8 +228,8 @@ class WP_Block_Metadata_Registry { * * @since 6.7.0 * - * @param string $file_or_folder The path to the file or folder. - * @return string|null The collection path if found, or null if not found. + * @param string $file_or_folder The normalized path to the file or folder. + * @return string|null The normalized collection path if found, or null if not found. */ private static function find_collection_path( $file_or_folder ) { if ( empty( $file_or_folder ) ) { @@ -234,7 +237,7 @@ class WP_Block_Metadata_Registry { } // Check the last matched collection first, since block registration usually happens in batches per plugin or theme. - $path = wp_normalize_path( rtrim( $file_or_folder, '/' ) ); + $path = rtrim( $file_or_folder, '/' ); if ( self::$last_matched_collection && str_starts_with( $path, self::$last_matched_collection ) ) { return self::$last_matched_collection; } @@ -279,7 +282,7 @@ class WP_Block_Metadata_Registry { * * @since 6.7.0 * - * @param string $path The file or folder path to determine the block identifier from. + * @param string $path The normalized file or folder path to determine the block identifier from. * @return string The block identifier, or an empty string if the path is empty. */ private static function default_identifier_callback( $path ) { @@ -302,8 +305,8 @@ class WP_Block_Metadata_Registry { * * @since 6.7.2 * - * @param string $path Block metadata collection path, without trailing slash. - * @param string[] $collection_roots List of collection root paths, without trailing slashes. + * @param string $path Normalized block metadata collection path, without trailing slash. + * @param string[] $collection_roots List of normalized collection root paths, without trailing slashes. * @return bool True if the path is allowed, false otherwise. */ private static function is_valid_collection_path( $path, $collection_roots ) { diff --git a/wp-includes/version.php b/wp-includes/version.php index e518564ca2..fb94dec7f8 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -16,7 +16,7 @@ * * @global string $wp_version */ -$wp_version = '6.8-beta1-59937'; +$wp_version = '6.8-beta1-59938'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.