Make WordPress Core


Ignore:
Timestamp:
02/08/2024 08:16:59 AM (15 months ago)
Author:
gziolo
Message:

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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/blocks.php

    r57550 r57559  
    125125/**
    126126 * Finds a script handle for the selected block metadata field. It detects
    127  * when a path to file was provided and finds a corresponding asset file
    128  * with details necessary to register the script under automatically
     127 * when a path to file was provided and optionally finds a corresponding asset
     128 * file with details necessary to register the script under automatically
    129129 * generated handle name. It returns unprocessed script handle otherwise.
    130130 *
    131131 * @since 5.5.0
    132132 * @since 6.1.0 Added `$index` parameter.
     133 * @since 6.5.0 The asset file is optional.
    133134 *
    134135 * @param array  $metadata   Block metadata.
     
    164165    );
    165166
    166     if ( empty( $script_asset_path ) ) {
    167         _doing_it_wrong(
    168             __FUNCTION__,
    169             sprintf(
    170                 /* translators: 1: Asset file location, 2: Field name, 3: Block name.  */
    171                 __( 'The asset file (%1$s) for the "%2$s" defined in "%3$s" block definition is missing.' ),
    172                 $script_asset_raw_path,
    173                 $field_name,
    174                 $metadata['name']
    175             ),
    176             '5.5.0'
    177         );
    178         return false;
    179     }
    180 
    181167    $script_path_norm = wp_normalize_path( realpath( $path . '/' . $script_path ) );
    182168    $script_uri       = get_block_asset_url( $script_path_norm );
     
    187173    }
    188174
    189     $script_asset        = require $script_asset_path;
     175    // Asset file for blocks is optional. See https://core.trac.wordpress.org/ticket/60460.
     176    $script_asset        = ! empty( $script_asset_path ) ? require $script_asset_path : array();
    190177    $script_dependencies = isset( $script_asset['dependencies'] ) ? $script_asset['dependencies'] : array();
    191178    $result              = wp_register_script(
Note: See TracChangeset for help on using the changeset viewer.