Editor: Fix broken asset URLs when using WP outside of the regular directory

Both CSS and JS URLs were totally broken for some of the newer blocks (e.g. the navigation block in the 2022 theme) when WP core is outside of the current directory using a different prefix.

Props gziolo, SergeyBiryukov, pgpagely, Mamaduka.
Merges [52939] to the 5.9 branch.
Fixes #55311.


git-svn-id: https://develop.svn.wordpress.org/branches/5.9@53019 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Jb Audras 2022-03-29 15:41:42 +00:00
parent 99e04b155c
commit 8cbd8c0c7b

View File

@ -103,7 +103,7 @@ function register_block_script_handle( $metadata, $field_name ) {
return false;
}
// Path needs to be normalized to work in Windows env.
$wpinc_path_norm = wp_normalize_path( ABSPATH . WPINC );
$wpinc_path_norm = wp_normalize_path( realpath( ABSPATH . WPINC ) );
$script_path_norm = wp_normalize_path( realpath( dirname( $metadata['file'] ) . '/' . $script_path ) );
$is_core_block = isset( $metadata['file'] ) && 0 === strpos( $metadata['file'], $wpinc_path_norm );
@ -145,7 +145,7 @@ function register_block_style_handle( $metadata, $field_name ) {
if ( empty( $metadata[ $field_name ] ) ) {
return false;
}
$wpinc_path_norm = wp_normalize_path( ABSPATH . WPINC );
$wpinc_path_norm = wp_normalize_path( realpath( ABSPATH . WPINC ) );
$is_core_block = isset( $metadata['file'] ) && 0 === strpos( $metadata['file'], $wpinc_path_norm );
if ( $is_core_block && ! wp_should_load_separate_core_block_assets() ) {
return false;
@ -239,7 +239,7 @@ function register_block_type_from_metadata( $file_or_folder, $args = array() ) {
if ( ! is_array( $metadata ) || empty( $metadata['name'] ) ) {
return false;
}
$metadata['file'] = wp_normalize_path( $metadata_file );
$metadata['file'] = wp_normalize_path( realpath( $metadata_file ) );
/**
* Filters the metadata provided for registering a block type.