Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Editor: Make asset registration .asset.php optional for blocks #5799

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 2 additions & 16 deletions src/wp-includes/blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,21 +162,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 );

Expand All @@ -185,7 +170,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,
Expand Down
36 changes: 21 additions & 15 deletions tests/phpunit/tests/blocks/register.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ public function tear_down() {
}
}

foreach ( wp_scripts()->registered as $script_handle => $script ) {
if ( str_starts_with( $script_handle, 'unit-tests-' ) ) {
wp_deregister_script( $script_handle );
}
}

parent::tear_down();
}

Expand Down Expand Up @@ -219,21 +225,6 @@ public function test_wrong_array_index_do_not_register_block_script_handle() {
$this->assertFalse( $result );
}

/**
* @expectedIncorrectUsage register_block_script_handle
josephfusco marked this conversation as resolved.
Show resolved Hide resolved
* @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
*/
Expand All @@ -258,6 +249,21 @@ public function test_handles_passed_register_block_script_handles() {
$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
*/
Expand Down
Loading