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 5 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
23 changes: 11 additions & 12 deletions src/wp-includes/blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,18 +162,8 @@ function register_block_script_handle( $metadata, $field_name, $index = 0 ) {
realpath( $script_asset_raw_path )
);

// Ensure graceful handling of scenarios where *.asset.php is not present or accessible.
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;
josephfusco marked this conversation as resolved.
Show resolved Hide resolved
}

Expand All @@ -185,7 +175,16 @@ function register_block_script_handle( $metadata, $field_name, $index = 0 ) {
$script_args['strategy'] = 'defer';
}

$script_asset = require $script_asset_path;
if ( file_exists( $script_asset_path ) ) {
josephfusco marked this conversation as resolved.
Show resolved Hide resolved
$script_asset = require $script_asset_path;
} else {
// Ensure graceful fallback for missing or inaccessible *.asset.php by setting default dependencies and version.
$script_asset = array(
josephfusco marked this conversation as resolved.
Show resolved Hide resolved
'dependencies' => array(),
'version' => false,
);
}

$script_dependencies = isset( $script_asset['dependencies'] ) ? $script_asset['dependencies'] : array();
$result = wp_register_script(
$script_handle,
Expand Down
1 change: 1 addition & 0 deletions tests/phpunit/data/blocks/no-asset-php/block-rtl.css
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/* Test CSS file - RTL version */
1 change: 1 addition & 0 deletions tests/phpunit/data/blocks/no-asset-php/block.css
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/* Test CSS file */
1 change: 1 addition & 0 deletions tests/phpunit/data/blocks/no-asset-php/block.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/* Test JavaScript file. */
73 changes: 73 additions & 0 deletions tests/phpunit/data/blocks/no-asset-php/block.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
{
"apiVersion": 2,
"name": "tests/notice",
"title": "Notice",
"category": "common",
"parent": [
"tests/group"
],
"ancestor": [
"tests/section"
],
"providesContext": {
"tests/message": "message"
},
"usesContext": [
"groupId"
],
"icon": "star",
"description": "Shows warning, error or success notices…",
"keywords": [
"alert",
"message"
],
"textdomain": "notice",
"attributes": {
"message": {
"type": "string"
}
},
"selectors": {
"root": ".wp-block-notice"
},
"blockHooks": {
"tests/before": "before",
"tests/after": "after",
"tests/first-child": "firstChild",
"tests/last-child": "lastChild"
},
"supports": {
"align": true,
"lightBlockWrapper": true
},
"styles": [
{
"name": "default",
"label": "Default",
"isDefault": true
},
{
"name": "other",
"label": "Other"
}
],
"variations": [
{
"name": "error",
"title": "Error",
"description": "Shows error.",
"keywords": [ "failure" ]
}
],
"example": {
"attributes": {
"message": "This is a notice!"
}
},
"editorScript": "tests-notice-editor-script",
"script": "tests-notice-script",
"viewScript": [ "tests-notice-view-script", "tests-notice-view-script-2" ],
"editorStyle": "tests-notice-editor-style",
"style": [ "tests-notice-style", "tests-notice-style-2" ],
"render": "file:./render.php"
}
1 change: 1 addition & 0 deletions tests/phpunit/data/blocks/no-asset-php/render.php
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<p <?php echo get_block_wrapper_attributes(); ?>><?php echo esc_html( $attributes['message'] ); ?></p>
47 changes: 46 additions & 1 deletion tests/phpunit/tests/blocks/register.php
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,6 @@ public function test_wrong_array_index_do_not_register_block_script_handle() {
}

/**
* @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() {
Expand All @@ -234,6 +233,52 @@ public function test_missing_asset_file_register_block_script_handle() {
$this->assertFalse( $result );
}

/**
* @ticket 57234
*/
public function test_missing_asset_php_for_traditionally_registered_block() {
$metadata = array(
'file' => DIR_TESTDATA . '/blocks/notice/block.json',
'name' => 'unit-test/traditional-block',
'script' => 'file:./blocks/notice/block.js',
);
$result = register_block_script_handle( $metadata, 'script' );

// Assert that the result is false due to missing .asset.php
$this->assertFalse( $result );
josephfusco marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* @ticket 57234
*/
public function test_inaccessible_asset_php_path() {
$metadata = array(
'file' => DIR_TESTDATA . '/blocks/notice/block.json',
'name' => 'unit-test/inaccessible-asset',
'script' => 'file:./blocks/notice/inaccessible-asset.php',
);
$result = register_block_script_handle( $metadata, 'script' );

// Assert that the result is false due to inaccessible .asset.php path
$this->assertFalse( $result );
}

/**
* @ticket 57234
*/
public function test_missing_asset_php_for_non_traditional_block() {
$metadata = array(
'file' => DIR_TESTDATA . '/blocks/no-asset-php/block.json',
'name' => 'unit-test/non-traditional-block',
// Assuming non-traditional registration doesn't use the script field
// or has a different mechanism that doesn't involve .asset.php
);
$result = register_block_script_handle( $metadata, 'script' );

// Assert that the result is false as expected in non-traditional scenarios
$this->assertFalse( $result );
}

/**
* @ticket 50263
*/
Expand Down
Loading