mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2026-06-28 22:30:04 +00:00
Editor: Allow registration of blocks that include assets from within a theme
Fixes the issue when you register blocks with `block.json` in your theme. There is no longer an assets's URL error because it resolves correctly in relation to the theme where it is located. Props fabiankaegy, ocean90, whoisnegrello, audrasjb, peterwilsoncc, Fixes #54647, #55513. git-svn-id: https://develop.svn.wordpress.org/trunk@53091 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
@@ -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,
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -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"
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
/* Test CSS file - RTL version */
|
||||
@@ -0,0 +1 @@
|
||||
/* Test CSS file */
|
||||
@@ -0,0 +1,6 @@
|
||||
<?php
|
||||
|
||||
return array(
|
||||
'dependencies' => array( 'wp-element', 'wp-blocks' ),
|
||||
'version' => 'test',
|
||||
);
|
||||
@@ -0,0 +1 @@
|
||||
/* Test JavaScript file. */
|
||||
@@ -0,0 +1 @@
|
||||
/* Test CSS file - RTL version */
|
||||
@@ -0,0 +1 @@
|
||||
/* Test CSS file */
|
||||
@@ -0,0 +1,6 @@
|
||||
<?php
|
||||
|
||||
return array(
|
||||
'dependencies' => array( 'wp-element', 'wp-blocks' ),
|
||||
'version' => 'test',
|
||||
);
|
||||
@@ -0,0 +1 @@
|
||||
/* Test JavaScript file. */
|
||||
7
tests/phpunit/data/themedir1/block-theme/functions.php
Normal file
7
tests/phpunit/data/themedir1/block-theme/functions.php
Normal file
@@ -0,0 +1,7 @@
|
||||
<?php
|
||||
|
||||
add_action( 'init', 'register_theme_blocks' );
|
||||
|
||||
function register_theme_blocks() {
|
||||
register_block_type( __DIR__ . 'blocks/example-block' );
|
||||
}
|
||||
@@ -252,6 +252,23 @@ class Tests_Blocks_Register extends WP_UnitTestCase {
|
||||
$this->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.
|
||||
|
||||
Reference in New Issue
Block a user