Skip to content

Commit b4acf19

Browse files
committed
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. git-svn-id: https://develop.svn.wordpress.org/trunk@54323 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 1de72e4 commit b4acf19

File tree

1 file changed

+27
-20
lines changed

1 file changed

+27
-20
lines changed

src/wp-includes/blocks.php

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -208,43 +208,50 @@ function register_block_style_handle( $metadata, $field_name, $index = 0 ) {
208208
}
209209

210210
// Check whether styles should have a ".min" suffix or not.
211-
$suffix = SCRIPT_DEBUG ? '' : '.min';
212-
$style_uri = plugins_url( $style_path, $metadata['file'] );
211+
$suffix = SCRIPT_DEBUG ? '' : '.min';
213212
if ( $is_core_block ) {
214213
$style_path = "style$suffix.css";
215-
$style_uri = includes_url( 'blocks/' . str_replace( 'core/', '', $metadata['name'] ) . "/style$suffix.css" );
216214
}
217-
218215
$style_path_norm = wp_normalize_path( realpath( dirname( $metadata['file'] ) . '/' . $style_path ) );
219-
$is_theme_block = 0 === strpos( $style_path_norm, $theme_path_norm );
220-
221-
if ( $is_theme_block ) {
222-
$style_uri = get_theme_file_uri( str_replace( $theme_path_norm, '', $style_path_norm ) );
216+
$has_style_file = '' !== $style_path_norm;
217+
if ( $has_style_file ) {
218+
$style_uri = plugins_url( $style_path, $metadata['file'] );
219+
$is_theme_block = str_starts_with( $style_path_norm, $theme_path_norm );
220+
if ( $is_theme_block ) {
221+
$style_uri = get_theme_file_uri( str_replace( $theme_path_norm, '', $style_path_norm ) );
222+
} elseif ( $is_core_block ) {
223+
$style_uri = includes_url( 'blocks/' . str_replace( 'core/', '', $metadata['name'] ) . "/style$suffix.css" );
224+
}
225+
} else {
226+
$style_uri = false;
223227
}
224228

225-
$style_handle = generate_block_asset_handle( $metadata['name'], $field_name, $index );
226-
$has_style_file = false !== $style_path_norm;
227-
$version = ! $is_core_block && isset( $metadata['version'] ) ? $metadata['version'] : false;
228-
$style_uri = $has_style_file ? $style_uri : false;
229-
$result = wp_register_style(
229+
$style_handle = generate_block_asset_handle( $metadata['name'], $field_name, $index );
230+
$version = ! $is_core_block && isset( $metadata['version'] ) ? $metadata['version'] : false;
231+
$result = wp_register_style(
230232
$style_handle,
231233
$style_uri,
232234
array(),
233235
$version
234236
);
235-
if ( file_exists( str_replace( '.css', '-rtl.css', $style_path_norm ) ) ) {
236-
wp_style_add_data( $style_handle, 'rtl', 'replace' );
237+
if ( ! $result ) {
238+
return false;
237239
}
240+
238241
if ( $has_style_file ) {
242+
if ( file_exists( str_replace( '.css', '-rtl.css', $style_path_norm ) ) ) {
243+
wp_style_add_data( $style_handle, 'rtl', 'replace' );
244+
}
245+
239246
wp_style_add_data( $style_handle, 'path', $style_path_norm );
240-
}
241247

242-
$rtl_file = str_replace( "$suffix.css", "-rtl$suffix.css", $style_path_norm );
243-
if ( is_rtl() && file_exists( $rtl_file ) ) {
244-
wp_style_add_data( $style_handle, 'path', $rtl_file );
248+
$rtl_file = str_replace( "{$suffix}.css", "-rtl{$suffix}.css", $style_path_norm );
249+
if ( is_rtl() && file_exists( $rtl_file ) ) {
250+
wp_style_add_data( $style_handle, 'path', $rtl_file );
251+
}
245252
}
246253

247-
return $result ? $style_handle : false;
254+
return $style_handle;
248255
}
249256

250257
/**

0 commit comments

Comments
 (0)