From b4acf193d1cac4b287492b762dfc2344b0942cbe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Greg=20Zi=C3=83=C2=B3=C3=85=E2=80=9Akowski?= Date: Tue, 27 Sep 2022 07:38:40 +0000 Subject: [PATCH] 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 --- src/wp-includes/blocks.php | 49 ++++++++++++++++++++++---------------- 1 file changed, 28 insertions(+), 21 deletions(-) diff --git a/src/wp-includes/blocks.php b/src/wp-includes/blocks.php index 584493fd5b..0d6d84fdef 100644 --- a/src/wp-includes/blocks.php +++ b/src/wp-includes/blocks.php @@ -208,43 +208,50 @@ function register_block_style_handle( $metadata, $field_name, $index = 0 ) { } // Check whether styles should have a ".min" suffix or not. - $suffix = SCRIPT_DEBUG ? '' : '.min'; - $style_uri = plugins_url( $style_path, $metadata['file'] ); + $suffix = SCRIPT_DEBUG ? '' : '.min'; if ( $is_core_block ) { $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 ) ); - $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 ) ); + $has_style_file = '' !== $style_path_norm; + if ( $has_style_file ) { + $style_uri = plugins_url( $style_path, $metadata['file'] ); + $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 ); - $has_style_file = false !== $style_path_norm; - $version = ! $is_core_block && isset( $metadata['version'] ) ? $metadata['version'] : false; - $style_uri = $has_style_file ? $style_uri : false; - $result = wp_register_style( + $style_handle = generate_block_asset_handle( $metadata['name'], $field_name, $index ); + $version = ! $is_core_block && isset( $metadata['version'] ) ? $metadata['version'] : false; + $result = wp_register_style( $style_handle, $style_uri, array(), $version ); - if ( file_exists( str_replace( '.css', '-rtl.css', $style_path_norm ) ) ) { - wp_style_add_data( $style_handle, 'rtl', 'replace' ); + if ( ! $result ) { + return false; } + 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 ); + + $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 ); - if ( is_rtl() && file_exists( $rtl_file ) ) { - wp_style_add_data( $style_handle, 'path', $rtl_file ); - } - - return $result ? $style_handle : false; + return $style_handle; } /**