mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2025-10-16 12:05:38 +00:00
Editor: Make asset file optional for block scripts
It is no longer a hard requirement that a *.asset.php file is present to register a script for block. Fixes #57234. Props joefusco, gziolo, spacedmonkey, colorful-tones. git-svn-id: https://develop.svn.wordpress.org/trunk@57559 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
parent
b3d834c132
commit
0ff0842fee
@ -124,12 +124,13 @@ function get_block_asset_url( $path ) {
|
||||
|
||||
/**
|
||||
* Finds a script handle for the selected block metadata field. It detects
|
||||
* when a path to file was provided and finds a corresponding asset file
|
||||
* with details necessary to register the script under automatically
|
||||
* when a path to file was provided and optionally finds a corresponding asset
|
||||
* file with details necessary to register the script under automatically
|
||||
* generated handle name. It returns unprocessed script handle otherwise.
|
||||
*
|
||||
* @since 5.5.0
|
||||
* @since 6.1.0 Added `$index` parameter.
|
||||
* @since 6.5.0 The asset file is optional.
|
||||
*
|
||||
* @param array $metadata Block metadata.
|
||||
* @param string $field_name Field name to pick from metadata.
|
||||
@ -163,21 +164,6 @@ function register_block_script_handle( $metadata, $field_name, $index = 0 ) {
|
||||
realpath( $script_asset_raw_path )
|
||||
);
|
||||
|
||||
if ( empty( $script_asset_path ) ) {
|
||||
_doing_it_wrong(
|
||||
__FUNCTION__,
|
||||
sprintf(
|
||||
/* translators: 1: Asset file location, 2: Field name, 3: Block name. */
|
||||
__( 'The asset file (%1$s) for the "%2$s" defined in "%3$s" block definition is missing.' ),
|
||||
$script_asset_raw_path,
|
||||
$field_name,
|
||||
$metadata['name']
|
||||
),
|
||||
'5.5.0'
|
||||
);
|
||||
return false;
|
||||
}
|
||||
|
||||
$script_path_norm = wp_normalize_path( realpath( $path . '/' . $script_path ) );
|
||||
$script_uri = get_block_asset_url( $script_path_norm );
|
||||
|
||||
@ -186,7 +172,8 @@ function register_block_script_handle( $metadata, $field_name, $index = 0 ) {
|
||||
$script_args['strategy'] = 'defer';
|
||||
}
|
||||
|
||||
$script_asset = require $script_asset_path;
|
||||
// Asset file for blocks is optional. See https://core.trac.wordpress.org/ticket/60460.
|
||||
$script_asset = ! empty( $script_asset_path ) ? require $script_asset_path : array();
|
||||
$script_dependencies = isset( $script_asset['dependencies'] ) ? $script_asset['dependencies'] : array();
|
||||
$result = wp_register_script(
|
||||
$script_handle,
|
||||
|
||||
@ -60,6 +60,12 @@ class Tests_Blocks_Register extends WP_UnitTestCase {
|
||||
}
|
||||
}
|
||||
|
||||
foreach ( wp_scripts()->registered as $script_handle => $script ) {
|
||||
if ( str_starts_with( $script_handle, 'unit-tests-' ) ) {
|
||||
wp_deregister_script( $script_handle );
|
||||
}
|
||||
}
|
||||
|
||||
parent::tear_down();
|
||||
}
|
||||
|
||||
@ -225,21 +231,6 @@ class Tests_Blocks_Register extends WP_UnitTestCase {
|
||||
$this->assertFalse( $result );
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedIncorrectUsage register_block_script_handle
|
||||
* @ticket 50263
|
||||
*/
|
||||
public function test_missing_asset_file_register_block_script_handle() {
|
||||
$metadata = array(
|
||||
'file' => __FILE__,
|
||||
'name' => 'unit-tests/test-block',
|
||||
'script' => 'file:./blocks/notice/missing-asset.js',
|
||||
);
|
||||
$result = register_block_script_handle( $metadata, 'script' );
|
||||
|
||||
$this->assertFalse( $result );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 50263
|
||||
*/
|
||||
@ -264,6 +255,21 @@ class Tests_Blocks_Register extends WP_UnitTestCase {
|
||||
$this->assertSame( 'test-script-handle-2', $result, 1 );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 50263
|
||||
* @ticket 60460
|
||||
*/
|
||||
public function test_missing_asset_file_register_block_script_handle_with_default_settings() {
|
||||
$metadata = array(
|
||||
'file' => __FILE__,
|
||||
'name' => 'unit-tests/test-block',
|
||||
'script' => 'file:./blocks/notice/missing-asset.js',
|
||||
);
|
||||
$result = register_block_script_handle( $metadata, 'script' );
|
||||
|
||||
$this->assertSame( 'unit-tests-test-block-script', $result );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 50263
|
||||
*/
|
||||
|
||||
Loading…
Reference in New Issue
Block a user