diff --git a/src/wp-includes/blocks.php b/src/wp-includes/blocks.php index 7c3509daac..45e36fc1dc 100644 --- a/src/wp-includes/blocks.php +++ b/src/wp-includes/blocks.php @@ -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, ); } diff --git a/tests/phpunit/data/themedir1/block-theme/blocks/example-block/block.json b/tests/phpunit/data/themedir1/block-theme/blocks/example-block/block.json new file mode 100644 index 0000000000..419a332b58 --- /dev/null +++ b/tests/phpunit/data/themedir1/block-theme/blocks/example-block/block.json @@ -0,0 +1,8 @@ +{ + "apiVersion": 2, + "title": "Example Theme Block", + "name": "block-theme/example-block", + "description": "Custom block registered from within a theme", + "editorScript": "file:./index.js", + "style": "file:./style.css" +} diff --git a/tests/phpunit/data/themedir1/block-theme/blocks/example-block/editor-style-rtl.css b/tests/phpunit/data/themedir1/block-theme/blocks/example-block/editor-style-rtl.css new file mode 100644 index 0000000000..2572f27aaf --- /dev/null +++ b/tests/phpunit/data/themedir1/block-theme/blocks/example-block/editor-style-rtl.css @@ -0,0 +1 @@ +/* Test CSS file - RTL version */ diff --git a/tests/phpunit/data/themedir1/block-theme/blocks/example-block/editor-style.css b/tests/phpunit/data/themedir1/block-theme/blocks/example-block/editor-style.css new file mode 100644 index 0000000000..5bbe1134f7 --- /dev/null +++ b/tests/phpunit/data/themedir1/block-theme/blocks/example-block/editor-style.css @@ -0,0 +1 @@ +/* Test CSS file */ diff --git a/tests/phpunit/data/themedir1/block-theme/blocks/example-block/index.asset.php b/tests/phpunit/data/themedir1/block-theme/blocks/example-block/index.asset.php new file mode 100644 index 0000000000..0314b6de8d --- /dev/null +++ b/tests/phpunit/data/themedir1/block-theme/blocks/example-block/index.asset.php @@ -0,0 +1,6 @@ + array( 'wp-element', 'wp-blocks' ), + 'version' => 'test', +); diff --git a/tests/phpunit/data/themedir1/block-theme/blocks/example-block/index.js b/tests/phpunit/data/themedir1/block-theme/blocks/example-block/index.js new file mode 100644 index 0000000000..0bdf0f5ad9 --- /dev/null +++ b/tests/phpunit/data/themedir1/block-theme/blocks/example-block/index.js @@ -0,0 +1 @@ +/* Test JavaScript file. */ diff --git a/tests/phpunit/data/themedir1/block-theme/blocks/example-block/style-rtl.css b/tests/phpunit/data/themedir1/block-theme/blocks/example-block/style-rtl.css new file mode 100644 index 0000000000..2572f27aaf --- /dev/null +++ b/tests/phpunit/data/themedir1/block-theme/blocks/example-block/style-rtl.css @@ -0,0 +1 @@ +/* Test CSS file - RTL version */ diff --git a/tests/phpunit/data/themedir1/block-theme/blocks/example-block/style.css b/tests/phpunit/data/themedir1/block-theme/blocks/example-block/style.css new file mode 100644 index 0000000000..5bbe1134f7 --- /dev/null +++ b/tests/phpunit/data/themedir1/block-theme/blocks/example-block/style.css @@ -0,0 +1 @@ +/* Test CSS file */ diff --git a/tests/phpunit/data/themedir1/block-theme/blocks/example-block/view.asset.php b/tests/phpunit/data/themedir1/block-theme/blocks/example-block/view.asset.php new file mode 100644 index 0000000000..0314b6de8d --- /dev/null +++ b/tests/phpunit/data/themedir1/block-theme/blocks/example-block/view.asset.php @@ -0,0 +1,6 @@ + array( 'wp-element', 'wp-blocks' ), + 'version' => 'test', +); diff --git a/tests/phpunit/data/themedir1/block-theme/blocks/example-block/view.js b/tests/phpunit/data/themedir1/block-theme/blocks/example-block/view.js new file mode 100644 index 0000000000..0bdf0f5ad9 --- /dev/null +++ b/tests/phpunit/data/themedir1/block-theme/blocks/example-block/view.js @@ -0,0 +1 @@ +/* Test JavaScript file. */ diff --git a/tests/phpunit/data/themedir1/block-theme/functions.php b/tests/phpunit/data/themedir1/block-theme/functions.php new file mode 100644 index 0000000000..1a378da4b3 --- /dev/null +++ b/tests/phpunit/data/themedir1/block-theme/functions.php @@ -0,0 +1,7 @@ +assertSame( 'unit-tests-test-block-script', $result ); } + /** + * @ticket 55513 + */ + public function test_success_register_block_script_handle_in_theme() { + switch_theme( 'block-theme' ); + + $metadata = array( + 'file' => wp_normalize_path( get_theme_file_path( 'blocks/example-block/block.json' ) ), + 'name' => 'block-theme/example-block', + 'viewScript' => 'file:./view.js', + ); + $result = register_block_script_handle( $metadata, 'viewScript' ); + + $expected_script_handle = 'block-theme-example-block-view-script'; + $this->assertSame( $expected_script_handle, $result ); + } + /** * @ticket 50263 */ @@ -305,6 +322,24 @@ class Tests_Blocks_Register extends WP_UnitTestCase { ); } + /** + * @ticket 55513 + */ + public function test_success_register_block_style_handle_in_theme() { + switch_theme( 'block-theme' ); + + $metadata = array( + 'file' => wp_normalize_path( get_theme_file_path( 'blocks/example-block/block.json' ) ), + 'name' => 'block-theme/example-block', + 'editorStyle' => 'file:./editor-style.css', + ); + $result = register_block_style_handle( $metadata, 'editorStyle' ); + + $expected_style_handle = 'block-theme-example-block-editor-style'; + $this->assertSame( $expected_style_handle, $result ); + $this->assertSame( 'replace', wp_styles()->get_data( $expected_style_handle, 'rtl' ) ); + } + /** * Tests that the function returns false when the `block.json` is not found * in the WordPress core.