From 65cc4348efbb28b20361c858d42c9aadd8ae3097 Mon Sep 17 00:00:00 2001 From: Ben Dwyer Date: Fri, 10 Jun 2022 13:04:40 +0100 Subject: [PATCH] Try to refactor register_block_style_handle to handle arrays --- src/wp-includes/blocks.php | 39 ++++++++++++++++++++++++-------------- 1 file changed, 25 insertions(+), 14 deletions(-) diff --git a/src/wp-includes/blocks.php b/src/wp-includes/blocks.php index 9df70abaf4b6a..8566949e54714 100644 --- a/src/wp-includes/blocks.php +++ b/src/wp-includes/blocks.php @@ -152,12 +152,18 @@ function register_block_script_handle( $metadata, $field_name ) { * style's registration, or false on failure. */ function register_block_style_handle( $metadata, $field_name ) { - if ( empty( $metadata[ $field_name ] ) ) { + $style_data = $metadata[ $field_name ]; + if ( empty( $style_data ) ) { 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 ); + + $block_name = $metadata['name']; + $block_json_file_path = $metadata['file']; + $wpinc_path_norm = wp_normalize_path( realpath( ABSPATH . WPINC ) ); + $theme_path_norm = wp_normalize_path( get_theme_file_path() ); + $is_core_block = isset( $block_json_file_path ) && 0 === strpos( $block_json_file_path, $wpinc_path_norm ); + $version = ! $is_core_block && isset( $metadata['version'] ) ? $metadata['version'] : false; + if ( $is_core_block && ! wp_should_load_separate_core_block_assets() ) { return false; } @@ -165,31 +171,36 @@ function register_block_style_handle( $metadata, $field_name ) { // Check whether styles should have a ".min" suffix or not. $suffix = SCRIPT_DEBUG ? '' : '.min'; - $style_handle = $metadata[ $field_name ]; - $style_path = remove_block_asset_path_prefix( $metadata[ $field_name ] ); - if ( $style_handle === $style_path && ! $is_core_block ) { - return $style_handle; + if ( is_array( $style_data ) ) { + $style_path = remove_block_asset_path_prefix( $style_data[0] ); + //if ( $style_data[0] === $style_path && ! $is_core_block ) { + // return $style_data[0]; + //} + } else { + $style_path = remove_block_asset_path_prefix( $style_data ); + if ( $style_data === $style_path && ! $is_core_block ) { + return $style_data; + } } - $style_uri = plugins_url( $style_path, $metadata['file'] ); + $style_uri = plugins_url( $style_path, $block_json_file_path ); if ( $is_core_block ) { $style_path = "style$suffix.css"; - $style_uri = includes_url( 'blocks/' . str_replace( 'core/', '', $metadata['name'] ) . "/style$suffix.css" ); + $style_uri = includes_url( 'blocks/' . str_replace( 'core/', '', $block_name ) . "/style$suffix.css" ); } - $style_path_norm = wp_normalize_path( realpath( dirname( $metadata['file'] ) . '/' . $style_path ) ); + $style_path_norm = wp_normalize_path( realpath( dirname( $block_json_file_path ) . '/' . $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_handle = generate_block_asset_handle( $block_name, $field_name ); + $block_dir = dirname( $block_json_file_path ); $style_file = realpath( "$block_dir/$style_path" ); $has_style_file = false !== $style_file; - $version = ! $is_core_block && isset( $metadata['version'] ) ? $metadata['version'] : false; $style_uri = $has_style_file ? $style_uri : false; $result = wp_register_style( $style_handle,