Make WordPress Core


Ignore:
Timestamp:
09/14/2022 10:50:26 AM (19 months ago)
Author:
gziolo
Message:

Blocks: Allow registering multiple items for all supported asset types

Follow-up #54337, [52069]. Part of https://github.com/WordPress/gutenberg/issues/41236. More details in https://github.com/WordPress/gutenberg/issues/33542.

Allow passing more than one script per block for editorScript, script, and viewScript fields in the block.json metadata file. This aligns with the previously added changes for style and editorStyle fields.

This change impacts the WP_Block_Type class and the REST API endpoint for block types. To ensure backward compatibiliy old names were soft deprecated in favor of new fields that work with array values and have _handles suffix.

Props zieladam, dlh, timothyblynjacobs, aristath, bernhard-reiter.
Fixes #56408.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-block-type.php

    r54133 r54155  
    167167
    168168    /**
    169      * Block type editor only script handle.
    170      *
    171      * @since 5.0.0
    172      * @var string|null
    173      */
    174     public $editor_script = null;
    175 
    176     /**
    177      * Block type front end and editor script handle.
    178      *
    179      * @since 5.0.0
    180      * @var string|null
    181      */
    182     public $script = null;
    183 
    184     /**
    185      * Block type front end only script handle.
    186      *
    187      * @since 5.9.0
    188      * @var string|null
    189      */
    190     public $view_script = null;
    191 
    192     /**
    193      * Block type editor only style handle.
    194      *
    195      * @since 5.0.0
    196      * @var string|null
    197      */
    198     public $editor_style = null;
    199 
    200     /**
    201      * Block type front end and editor style handle.
    202      *
    203      * @since 5.0.0
    204      * @var string|null
    205      */
    206     public $style = null;
     169     * Block type editor only script handles.
     170     *
     171     * @since 6.1.0
     172     * @var string[]
     173     */
     174    public $editor_script_handles = array();
     175
     176    /**
     177     * Block type front end and editor script handles.
     178     *
     179     * @since 6.1.0
     180     * @var string[]
     181     */
     182    public $script_handles = array();
     183
     184    /**
     185     * Block type front end only script handles.
     186     *
     187     * @since 6.1.0
     188     * @var string[]
     189     */
     190    public $view_script_handles = array();
     191
     192    /**
     193     * Block type editor only style handles.
     194     *
     195     * @since 6.1.0
     196     * @var string[]
     197     */
     198    public $editor_style_handles = array();
     199
     200    /**
     201     * Block type front end and editor style handles.
     202     *
     203     * @since 6.1.0
     204     * @var string[]
     205     */
     206    public $style_handles = array();
     207
     208    /**
     209     * Deprecated block type properties for script and style handles.
     210     *
     211     * @since 6.1.0
     212     * @var string[]
     213     */
     214    private $deprecated_properties = array(
     215        'editor_script',
     216        'script',
     217        'view_script',
     218        'editor_style',
     219        'style',
     220    );
    207221
    208222    /**
     
    229243     * @since 5.9.0 Added the `view_script` property.
    230244     * @since 6.0.0 Added the `ancestor` property.
     245     * @since 6.1.0 Added the `editor_script_handles`, `script_handles`, `view_script_handles,
     246     *              `editor_style_handles`, and `style_handles` properties.
     247     *              Deprecated the `editor_script`, `script`, `view_script`, `editor_style`, and `style` properties.
    231248     *
    232249     * @see register_block_type()
     
    237254     *     however the ones described below are supported by default. Default empty array.
    238255     *
    239      *     @type string        $api_version      Block API version.
    240      *     @type string        $title            Human-readable block type label.
    241      *     @type string|null   $category         Block type category classification, used in
    242      *                                           search interfaces to arrange block types by category.
    243      *     @type string[]|null $parent           Setting parent lets a block require that it is only
    244      *                                           available when nested within the specified blocks.
    245      *     @type string[]|null $ancestor         Setting ancestor makes a block available only inside the specified
    246      *                                           block types at any position of the ancestor's block subtree.
    247      *     @type string|null   $icon             Block type icon.
    248      *     @type string        $description      A detailed block type description.
    249      *     @type string[]      $keywords         Additional keywords to produce block type as
    250      *                                           result in search interfaces.
    251      *     @type string|null   $textdomain       The translation textdomain.
    252      *     @type array[]       $styles           Alternative block styles.
    253      *     @type array[]       $variations       Block variations.
    254      *     @type array|null    $supports         Supported features.
    255      *     @type array|null    $example          Structured data for the block preview.
    256      *     @type callable|null $render_callback  Block type render callback.
    257      *     @type array|null    $attributes       Block type attributes property schemas.
    258      *     @type string[]      $uses_context     Context values inherited by blocks of this type.
    259      *     @type string[]|null $provides_context Context provided by blocks of this type.
    260      *     @type string|null   $editor_script    Block type editor only script handle.
    261      *     @type string|null   $script           Block type front end and editor script handle.
    262      *     @type string|null   $view_script      Block type front end only script handle.
    263      *     @type string|null   $editor_style     Block type editor only style handle.
    264      *     @type string|null   $style            Block type front end and editor style handle.
     256     *     @type string        $api_version              Block API version.
     257     *     @type string        $title                    Human-readable block type label.
     258     *     @type string|null   $category                 Block type category classification, used in
     259     *                                                   search interfaces to arrange block types by category.
     260     *     @type string[]|null $parent                   Setting parent lets a block require that it is only
     261     *                                                   available when nested within the specified blocks.
     262     *     @type string[]|null $ancestor                 Setting ancestor makes a block available only inside the specified
     263     *                                                   block types at any position of the ancestor's block subtree.
     264     *     @type string|null   $icon                     Block type icon.
     265     *     @type string        $description              A detailed block type description.
     266     *     @type string[]      $keywords                 Additional keywords to produce block type as
     267     *                                                   result in search interfaces.
     268     *     @type string|null   $textdomain               The translation textdomain.
     269     *     @type array[]       $styles                   Alternative block styles.
     270     *     @type array[]       $variations               Block variations.
     271     *     @type array|null    $supports                 Supported features.
     272     *     @type array|null    $example                  Structured data for the block preview.
     273     *     @type callable|null $render_callback          Block type render callback.
     274     *     @type array|null    $attributes               Block type attributes property schemas.
     275     *     @type string[]      $uses_context             Context values inherited by blocks of this type.
     276     *     @type string[]|null $provides_context         Context provided by blocks of this type.
     277     *     @type string[]      $editor_script_handles    Block type editor only script handles.
     278     *     @type string[]      $script_handles           Block type front end and editor script handles.
     279     *     @type string[]      $view_script_handles      Block type front end only script handles.
     280     *     @type string[]      $editor_style_handles     Block type editor only style handles.
     281     *     @type string[]      $style_handles            Block type front end and editor style handles.
    265282     * }
    266283     */
     
    269286
    270287        $this->set_props( $args );
     288    }
     289
     290    /**
     291     * Proxies getting values for deprecated properties for script and style handles for backward compatibility.
     292     * Gets the value for the corresponding new property if the first item in the array provided.
     293     *
     294     * @since 6.1.0
     295     *
     296     * @param string $name Deprecated property name.
     297     *
     298     * @return string|null|void The value read from the new property if the first item in the array provided,
     299     *                          null when value not found, or void when unknown property name provided.
     300     */
     301    public function __get( $name ) {
     302        if ( ! in_array( $name, $this->deprecated_properties ) ) {
     303            return;
     304        }
     305
     306        $new_name = $name . '_handles';
     307        return isset( $this->{$new_name }[0] ) ? $this->{$new_name }[0] : null;
     308    }
     309
     310    /**
     311     * Proxies checking for deprecated properties for script and style handles for backward compatibility.
     312     * Checks whether the corresponding new property has the first item in the array provided.
     313     *
     314     * @since 6.1.0
     315     *
     316     * @param string $name Deprecated property name.
     317     *
     318     * @return boolean Returns true when for the new property the first item in the array exists,
     319     *                     or false otherwise.
     320     */
     321    public function __isset( $name ) {
     322        if ( ! in_array( $name, $this->deprecated_properties ) ) {
     323            return false;
     324        }
     325
     326        $new_name = $name . '_handles';
     327        return isset( $this->{$new_name }[0] );
     328    }
     329
     330    /**
     331     * Proxies setting values for deprecated properties for script and style handles for backward compatibility.
     332     * Sets the value for the corresponding new property as the first item in the array.
     333     * It also allows setting custom properties for backward compatibility.
     334     *
     335     * @since 6.1.0
     336     *
     337     * @param string $name  Property name.
     338     * @param mixed  $value Property value.
     339     */
     340    public function __set( $name, $value ) {
     341        if ( ! in_array( $name, $this->deprecated_properties ) ) {
     342            $this->{$name} = $value;
     343            return;
     344        }
     345
     346        if ( ! is_string( $value ) ) {
     347            return;
     348        }
     349
     350        $new_name = $name . '_handles';
     351        $this->{$new_name }[0] = $value;
    271352    }
    272353
Note: See TracChangeset for help on using the changeset viewer.