Blocks: Fix 404 error for core styles with no file

[54155] broke loading of style.css files, namely it was enqueuing style.css files that don't exist on the frontend, which lead to 404 HTTO errors. All these style.css files don't exist for core blocks as they should be registered style handlers without a file path.

Follow-up to [54155].
Props tobiasbg, nendeb55.
Fixes #56408, #56614.


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


git-svn-id: http://core.svn.wordpress.org/trunk@53882 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
gziolo 2022-09-27 07:40:11 +00:00
parent 8364c68daa
commit 8cb2e8aa60
2 changed files with 29 additions and 22 deletions

View File

@ -208,43 +208,50 @@ function register_block_style_handle( $metadata, $field_name, $index = 0 ) {
} }
// Check whether styles should have a ".min" suffix or not. // Check whether styles should have a ".min" suffix or not.
$suffix = SCRIPT_DEBUG ? '' : '.min'; $suffix = SCRIPT_DEBUG ? '' : '.min';
$style_uri = plugins_url( $style_path, $metadata['file'] );
if ( $is_core_block ) { if ( $is_core_block ) {
$style_path = "style$suffix.css"; $style_path = "style$suffix.css";
$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 ) ); $style_path_norm = wp_normalize_path( realpath( dirname( $metadata['file'] ) . '/' . $style_path ) );
$is_theme_block = 0 === strpos( $style_path_norm, $theme_path_norm ); $has_style_file = '' !== $style_path_norm;
if ( $has_style_file ) {
if ( $is_theme_block ) { $style_uri = plugins_url( $style_path, $metadata['file'] );
$style_uri = get_theme_file_uri( str_replace( $theme_path_norm, '', $style_path_norm ) ); $is_theme_block = str_starts_with( $style_path_norm, $theme_path_norm );
if ( $is_theme_block ) {
$style_uri = get_theme_file_uri( str_replace( $theme_path_norm, '', $style_path_norm ) );
} elseif ( $is_core_block ) {
$style_uri = includes_url( 'blocks/' . str_replace( 'core/', '', $metadata['name'] ) . "/style$suffix.css" );
}
} else {
$style_uri = false;
} }
$style_handle = generate_block_asset_handle( $metadata['name'], $field_name, $index ); $style_handle = generate_block_asset_handle( $metadata['name'], $field_name, $index );
$has_style_file = false !== $style_path_norm; $version = ! $is_core_block && isset( $metadata['version'] ) ? $metadata['version'] : false;
$version = ! $is_core_block && isset( $metadata['version'] ) ? $metadata['version'] : false; $result = wp_register_style(
$style_uri = $has_style_file ? $style_uri : false;
$result = wp_register_style(
$style_handle, $style_handle,
$style_uri, $style_uri,
array(), array(),
$version $version
); );
if ( file_exists( str_replace( '.css', '-rtl.css', $style_path_norm ) ) ) { if ( ! $result ) {
wp_style_add_data( $style_handle, 'rtl', 'replace' ); return false;
} }
if ( $has_style_file ) { if ( $has_style_file ) {
if ( file_exists( str_replace( '.css', '-rtl.css', $style_path_norm ) ) ) {
wp_style_add_data( $style_handle, 'rtl', 'replace' );
}
wp_style_add_data( $style_handle, 'path', $style_path_norm ); wp_style_add_data( $style_handle, 'path', $style_path_norm );
$rtl_file = str_replace( "{$suffix}.css", "-rtl{$suffix}.css", $style_path_norm );
if ( is_rtl() && file_exists( $rtl_file ) ) {
wp_style_add_data( $style_handle, 'path', $rtl_file );
}
} }
$rtl_file = str_replace( "$suffix.css", "-rtl$suffix.css", $style_path_norm ); return $style_handle;
if ( is_rtl() && file_exists( $rtl_file ) ) {
wp_style_add_data( $style_handle, 'path', $rtl_file );
}
return $result ? $style_handle : false;
} }
/** /**

View File

@ -16,7 +16,7 @@
* *
* @global string $wp_version * @global string $wp_version
*/ */
$wp_version = '6.1-beta1-54322'; $wp_version = '6.1-beta1-54323';
/** /**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema. * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.