Editor: Allow registration of blocks that include assets from within a theme

Fixes the issue when you register blocks with `block.json` in your theme. There is no longer an assets's URL error because it resolves correctly in relation to the theme where it is located.

Props fabiankaegy, ocean90, whoisnegrello, audrasjb, peterwilsoncc, 
Fixes #54647, #55513.


Built from https://develop.svn.wordpress.org/trunk@53091


git-svn-id: http://core.svn.wordpress.org/trunk@52680 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
gziolo 2022-04-07 11:59:05 +00:00
parent 030980579d
commit 0ae89f5171
2 changed files with 32 additions and 6 deletions

View File

@ -108,12 +108,20 @@ function register_block_script_handle( $metadata, $field_name ) {
}
// Path needs to be normalized to work in Windows env.
$wpinc_path_norm = wp_normalize_path( realpath( ABSPATH . WPINC ) );
$theme_path_norm = wp_normalize_path( get_theme_file_path() );
$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 );
$is_theme_block = 0 === strpos( $script_path_norm, $theme_path_norm );
$script_uri;
if ( $is_core_block ) {
$script_uri = includes_url( str_replace( $wpinc_path_norm, '', $script_path_norm ) );
} elseif ( $is_theme_block ) {
$script_uri = get_theme_file_uri( str_replace( $theme_path_norm, '', $script_path_norm ) );
} else {
$script_uri = plugins_url( $script_path, $metadata['file'] );
}
$script_uri = $is_core_block ?
includes_url( str_replace( $wpinc_path_norm, '', $script_path_norm ) ) :
plugins_url( $script_path, $metadata['file'] );
$script_asset = require $script_asset_path;
$script_dependencies = isset( $script_asset['dependencies'] ) ? $script_asset['dependencies'] : array();
$result = wp_register_script(
@ -150,6 +158,7 @@ function register_block_style_handle( $metadata, $field_name ) {
return false;
}
$wpinc_path_norm = wp_normalize_path( realpath( ABSPATH . WPINC ) );
$theme_path_norm = wp_normalize_path( get_theme_file_path() );
$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;
@ -171,6 +180,13 @@ function register_block_style_handle( $metadata, $field_name ) {
$style_uri = includes_url( 'blocks/' . str_replace( 'core/', '', $metadata['name'] ) . "/style$suffix.css" );
}
$style_path_norm = wp_normalize_path( realpath( dirname( $metadata['file'] ) . '/' . $style_path ) );
$is_theme_block = 0 === strpos( $style_path_norm, $theme_path_norm );
if ( $is_theme_block ) {
$style_uri = get_theme_file_uri( str_replace( $theme_path_norm, '', $style_path_norm ) );
}
$style_handle = generate_block_asset_handle( $metadata['name'], $field_name );
$block_dir = dirname( $metadata['file'] );
$style_file = realpath( "$block_dir/$style_path" );
@ -1315,10 +1331,20 @@ function _wp_multiple_block_styles( $metadata ) {
foreach ( $metadata[ $key ] as $handle ) {
$args = array( 'handle' => $handle );
if ( 0 === strpos( $handle, 'file:' ) && isset( $metadata['file'] ) ) {
$style_path = remove_block_asset_path_prefix( $handle );
$style_path = remove_block_asset_path_prefix( $handle );
$theme_path_norm = wp_normalize_path( get_theme_file_path() );
$style_path_norm = wp_normalize_path( realpath( dirname( $metadata['file'] ) . '/' . $style_path ) );
$is_theme_block = isset( $metadata['file'] ) && 0 === strpos( $metadata['file'], $theme_path_norm );
$style_uri = plugins_url( $style_path, $metadata['file'] );
if ( $is_theme_block ) {
$style_uri = get_theme_file_uri( str_replace( $theme_path_norm, '', $style_path_norm ) );
}
$args = array(
'handle' => sanitize_key( "{$metadata['name']}-{$style_path}" ),
'src' => plugins_url( $style_path, $metadata['file'] ),
'src' => $style_uri,
);
}

View File

@ -16,7 +16,7 @@
*
* @global string $wp_version
*/
$wp_version = '6.0-alpha-53090';
$wp_version = '6.0-alpha-53091';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.