Make WordPress Core

Opened 2 years ago

Last modified 3 days ago

#61910 new defect (bug)

register_block_type_from_metadata fails to register style attribute when using $args

Reported by: mreishus Owned by:
Priority: normal Milestone: Awaiting Review
Component: General Version:
Severity: normal Keywords: has-patch has-unit-tests
Cc: Focuses:

Description

Summary

The register_block_type_from_metadata function fails to properly register a block's style attribute when the block is registered using the $args parameter instead of reading from a block.json file.

Steps to Reproduce

  1. Call register_block_type_from_metadata and pass the block configuration in the $args array instead of reading from block.json. The first argument should be an empty string to specify a block.json file is not used.
  2. Include a style attribute in $args specifying an array of style handles.
  3. Observe a PHP warning about an undefined index 'name' when it attempts to call register_block_style_handle.

Expected Result

The block should register successfully with the specified style attribute. No PHP warnings should be generated.

Actual Result

A PHP warning is emitted:

Warning: Undefined array key "name" in .../wp-includes/blocks.php on line 294

The style attribute is not properly registered for the block.

Relevant Code

The issue lies in this part of register_block_type_from_metadata:

foreach ( $style_fields as $metadata_field_name => $settings_field_name ) {
    if ( ! empty( $settings[ $metadata_field_name ] ) ) {
        $metadata[ $metadata_field_name ] = $settings[ $metadata_field_name ];
    }
    if ( ! empty( $metadata[ $metadata_field_name ] ) ) {
        $styles           = $metadata[ $metadata_field_name ];
        $processed_styles = array();
        if ( is_array( $styles ) ) {
            for ( $index = 0; $index < count( $styles ); $index++ ) {
                $result = register_block_style_handle(
                    $metadata,
                    $metadata_field_name, 
                    $index
                );
                if ( $result ) {
                    $processed_styles[] = $result;
                }
            }
        } else {
            $result = register_block_style_handle(
                $metadata,
                $metadata_field_name
            );
            if ( $result ) {
                $processed_styles[] = $result;
            }
        }
        $settings[ $settings_field_name ] = $processed_styles;
    }
}

When calling register_block_style_handle, it passes the $metadata array. However, when registering via $args, the block name is not present in $metadata. It only exists in $args/$settings at this point.

Unit Test

Here is a failing unit test that demonstrates the issue: (see attached)

The test currently fails due to the PHP warning.

Related

#56865, [57026] - Original change allowing register_block_type_from_metadata() to be used without a block.json file.

Attachments (1)

register-block-style-attribute.diff​ (1.7 KB ) - added by mreishus 2 years ago.
Failing Unit Test

Download all attachments as: .zip

Change History (2)

This ticket was mentioned in ​PR #15630 on ​WordPress/wordpress-develop by ​@salvatorecorsi.


3 days ago
#1

  • Keywords has-patch has-unit-tests added

register_block_type_from_metadata() accepts a block name in $args when the metadata does not contain one. It currently copies that name into the metadata only after registering assets, so the asset helpers can read an undefined name before they can generate handles from the supplied name.

Use the accepted name before calling those helpers, while preserving an existing metadata name. This also preserves the current behavior when $args['name'] overrides a name already present in block.json.

The tests cover the reported style-handle case and a metadata file with file-backed JavaScript and CSS whose name comes from $args. Related PR #14065 changes style .asset.php support and avoids name generation for directly supplied handles; this patch supplies the missing name earlier for the file-backed asset paths as well.

Testing

Create a block.json with a relative style and editorScript, omit its name, and register it with register_block_type_from_metadata( __DIR__, array( 'name' => 'example/notice' ) ). Both assets should register under the supplied block name without a PHP warning.

The two new tests fail on the unchanged source with undefined name warnings and pass with this change. The complete registration test class passes in single-site and multisite (73 tests each). PHP syntax, coding standards, compatibility and whitespace checks pass.

The broader native blocks group ran 720 tests with two Windows path-format failures, reproduced on unchanged source. Full PHPStan reports three Windows theme-documentation-reference errors, also reproduced unchanged. No Docker or browser E2E run was performed; this change does not modify generated JavaScript assets.

Use of AI Tools

AI assistance: Yes
Tool(s): Codex
Used for: Investigating the ticket and related work, implementing the fallback and regression tests, running validation, and independent automated code review. This contribution has not received a human review from the account owner.

Note: See TracTickets for help on using tickets.