Make WordPress Core


Ignore:
Timestamp:
09/14/2022 10:50:26 AM (2 years 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/blocks.php

    r54138 r54155  
    3636 *
    3737 * @since 5.5.0
     38 * @since 6.1.0 Added `$index` parameter.
    3839 *
    3940 * @param string $block_name Name of the block.
    4041 * @param string $field_name Name of the metadata field.
     42 * @param int    $index      Optional. Index of the asset when multiple items passed.
     43 *                           Default 0.
    4144 * @return string Generated asset name for the block's field.
    4245 */
    43 function generate_block_asset_handle( $block_name, $field_name ) {
     46function generate_block_asset_handle( $block_name, $field_name, $index = 0 ) {
    4447    if ( 0 === strpos( $block_name, 'core/' ) ) {
    4548        $asset_handle = str_replace( 'core/', 'wp-block-', $block_name );
     
    4952        if ( 0 === strpos( $field_name, 'view' ) ) {
    5053            $asset_handle .= '-view';
     54        }
     55        if ( $index > 0 ) {
     56            $asset_handle .= '-' . ( $index + 1 );
    5157        }
    5258        return $asset_handle;
     
    6066        'style'        => 'style',
    6167    );
    62     return str_replace( '/', '-', $block_name ) .
     68    $asset_handle   = str_replace( '/', '-', $block_name ) .
    6369        '-' . $field_mappings[ $field_name ];
     70    if ( $index > 0 ) {
     71        $asset_handle .= '-' . ( $index + 1 );
     72    }
     73    return $asset_handle;
    6474}
    6575
     
    7181 *
    7282 * @since 5.5.0
     83 * @since 6.1.0 Added `$index` parameter.
    7384 *
    7485 * @param array  $metadata   Block metadata.
    7586 * @param string $field_name Field name to pick from metadata.
     87 * @param int    $index      Optional. Index of the script to register when multiple items passed.
     88 *                           Default 0.
    7689 * @return string|false Script handle provided directly or created through
    7790 *                      script's registration, or false on failure.
    7891 */
    79 function register_block_script_handle( $metadata, $field_name ) {
     92function register_block_script_handle( $metadata, $field_name, $index = 0 ) {
    8093    if ( empty( $metadata[ $field_name ] ) ) {
    8194        return false;
    8295    }
     96
    8397    $script_handle = $metadata[ $field_name ];
    84     $script_path   = remove_block_asset_path_prefix( $metadata[ $field_name ] );
     98    if ( is_array( $script_handle ) ) {
     99        if ( empty( $script_handle[ $index ] ) ) {
     100            return false;
     101        }
     102        $script_handle = $script_handle[ $index ];
     103    }
     104
     105    $script_path = remove_block_asset_path_prefix( $script_handle );
    85106    if ( $script_handle === $script_path ) {
    86107        return $script_handle;
    87108    }
    88109
    89     $script_handle     = generate_block_asset_handle( $metadata['name'], $field_name );
     110    $script_handle     = generate_block_asset_handle( $metadata['name'], $field_name, $index );
    90111    $script_asset_path = wp_normalize_path(
    91112        realpath(
     
    146167 *
    147168 * @since 5.5.0
     169 * @since 6.1.0 Added `$index` parameter.
    148170 *
    149171 * @param array  $metadata   Block metadata.
    150172 * @param string $field_name Field name to pick from metadata.
     173 * @param int    $index      Optional. Index of the style to register when multiple items passed.
     174 *                           Default 0.
    151175 * @return string|false Style handle provided directly or created through
    152176 *                      style's registration, or false on failure.
    153177 */
    154 function register_block_style_handle( $metadata, $field_name ) {
     178function register_block_style_handle( $metadata, $field_name, $index = 0 ) {
    155179    if ( empty( $metadata[ $field_name ] ) ) {
    156180        return false;
    157181    }
     182
    158183    $wpinc_path_norm = wp_normalize_path( realpath( ABSPATH . WPINC ) );
    159184    $theme_path_norm = wp_normalize_path( get_theme_file_path() );
    160185    $is_core_block   = isset( $metadata['file'] ) && 0 === strpos( $metadata['file'], $wpinc_path_norm );
     186    // Skip registering individual styles for each core block when a bundled version provided.
    161187    if ( $is_core_block && ! wp_should_load_separate_core_block_assets() ) {
    162188        return false;
    163189    }
    164190
     191    $style_handle = $metadata[ $field_name ];
     192    if ( is_array( $style_handle ) ) {
     193        if ( empty( $style_handle[ $index ] ) ) {
     194            return false;
     195        }
     196        $style_handle = $style_handle[ $index ];
     197    }
     198
     199    $style_path      = remove_block_asset_path_prefix( $style_handle );
     200    $is_style_handle = $style_handle === $style_path;
     201    // Allow only passing style handles for core blocks.
     202    if ( $is_core_block && ! $is_style_handle ) {
     203        return false;
     204    }
     205    // Return the style handle unless it's the first item for every core block that requires special treatment.
     206    if ( $is_style_handle && ! ( $is_core_block && 0 === $index ) ) {
     207        return $style_handle;
     208    }
     209
    165210    // Check whether styles should have a ".min" suffix or not.
    166     $suffix = SCRIPT_DEBUG ? '' : '.min';
    167 
    168     $style_handle = $metadata[ $field_name ];
    169     $style_path   = remove_block_asset_path_prefix( $metadata[ $field_name ] );
    170 
    171     if ( $style_handle === $style_path && ! $is_core_block ) {
    172         return $style_handle;
    173     }
    174 
     211    $suffix    = SCRIPT_DEBUG ? '' : '.min';
    175212    $style_uri = plugins_url( $style_path, $metadata['file'] );
    176213    if ( $is_core_block ) {
     
    186223    }
    187224
    188     $style_handle   = generate_block_asset_handle( $metadata['name'], $field_name );
     225    $style_handle   = generate_block_asset_handle( $metadata['name'], $field_name, $index );
    189226    $block_dir      = dirname( $metadata['file'] );
    190     $style_file     = realpath( "$block_dir/$style_path" );
     227    $style_file     = wp_normalize_path( realpath( "$block_dir/$style_path" ) );
    191228    $has_style_file = false !== $style_file;
    192229    $version        = ! $is_core_block && isset( $metadata['version'] ) ? $metadata['version'] : false;
     
    312349    }
    313350
    314     if ( ! empty( $metadata['editorScript'] ) ) {
    315         $settings['editor_script'] = register_block_script_handle(
    316             $metadata,
    317             'editorScript'
    318         );
    319     }
    320 
    321     if ( ! empty( $metadata['script'] ) ) {
    322         $settings['script'] = register_block_script_handle(
    323             $metadata,
    324             'script'
    325         );
    326     }
    327 
    328     if ( ! empty( $metadata['viewScript'] ) ) {
    329         $settings['view_script'] = register_block_script_handle(
    330             $metadata,
    331             'viewScript'
    332         );
    333     }
    334 
    335     if ( ! empty( $metadata['editorStyle'] ) ) {
    336         $settings['editor_style'] = register_block_style_handle(
    337             $metadata,
    338             'editorStyle'
    339         );
    340     }
    341 
    342     if ( ! empty( $metadata['style'] ) ) {
    343         $settings['style'] = register_block_style_handle(
    344             $metadata,
    345             'style'
    346         );
     351    $script_fields = array(
     352        'editorScript' => 'editor_script_handles',
     353        'script'       => 'script_handles',
     354        'viewScript'   => 'view_script_handles',
     355    );
     356    foreach ( $script_fields as $metadata_field_name => $settings_field_name ) {
     357        if ( ! empty( $metadata[ $metadata_field_name ] ) ) {
     358            $scripts           = $metadata[ $metadata_field_name ];
     359            $processed_scripts = array();
     360            if ( is_array( $scripts ) ) {
     361                for ( $index = 0; $index < count( $scripts ); $index++ ) {
     362                    $result = register_block_script_handle(
     363                        $metadata,
     364                        $metadata_field_name,
     365                        $index
     366                    );
     367                    if ( $result ) {
     368                        $processed_scripts[] = $result;
     369                    }
     370                }
     371            } else {
     372                $result = register_block_script_handle(
     373                    $metadata,
     374                    $metadata_field_name
     375                );
     376                if ( $result ) {
     377                    $processed_scripts[] = $result;
     378                }
     379            }
     380            $settings[ $settings_field_name ] = $processed_scripts;
     381        }
     382    }
     383
     384    $style_fields = array(
     385        'editorStyle' => 'editor_style_handles',
     386        'style'       => 'style_handles',
     387    );
     388    foreach ( $style_fields as $metadata_field_name => $settings_field_name ) {
     389        if ( ! empty( $metadata[ $metadata_field_name ] ) ) {
     390            $styles           = $metadata[ $metadata_field_name ];
     391            $processed_styles = array();
     392            if ( is_array( $styles ) ) {
     393                for ( $index = 0; $index < count( $styles ); $index++ ) {
     394                    $result = register_block_style_handle(
     395                        $metadata,
     396                        $metadata_field_name,
     397                        $index
     398                    );
     399                    if ( $result ) {
     400                        $processed_styles[] = $result;
     401                    }
     402                }
     403            } else {
     404                $result = register_block_style_handle(
     405                    $metadata,
     406                    $metadata_field_name
     407                );
     408                if ( $result ) {
     409                    $processed_styles[] = $result;
     410                }
     411            }
     412            $settings[ $settings_field_name ] = $processed_styles;
     413        }
    347414    }
    348415
     
    12631330
    12641331/**
    1265  * Allows multiple block styles.
    1266  *
    1267  * @since 5.9.0
    1268  *
    1269  * @param array $metadata Metadata for registering a block type.
    1270  * @return array Metadata for registering a block type.
    1271  */
    1272 function _wp_multiple_block_styles( $metadata ) {
    1273     foreach ( array( 'style', 'editorStyle' ) as $key ) {
    1274         if ( ! empty( $metadata[ $key ] ) && is_array( $metadata[ $key ] ) ) {
    1275             $default_style = array_shift( $metadata[ $key ] );
    1276             foreach ( $metadata[ $key ] as $handle ) {
    1277                 $args = array( 'handle' => $handle );
    1278                 if ( 0 === strpos( $handle, 'file:' ) && isset( $metadata['file'] ) ) {
    1279                     $style_path      = remove_block_asset_path_prefix( $handle );
    1280                     $theme_path_norm = wp_normalize_path( get_theme_file_path() );
    1281                     $style_path_norm = wp_normalize_path( realpath( dirname( $metadata['file'] ) . '/' . $style_path ) );
    1282                     $is_theme_block  = isset( $metadata['file'] ) && 0 === strpos( $metadata['file'], $theme_path_norm );
    1283 
    1284                     $style_uri = plugins_url( $style_path, $metadata['file'] );
    1285 
    1286                     if ( $is_theme_block ) {
    1287                         $style_uri = get_theme_file_uri( str_replace( $theme_path_norm, '', $style_path_norm ) );
    1288                     }
    1289 
    1290                     $args = array(
    1291                         'handle' => sanitize_key( "{$metadata['name']}-{$style_path}" ),
    1292                         'src'    => $style_uri,
    1293                     );
    1294                 }
    1295 
    1296                 wp_enqueue_block_style( $metadata['name'], $args );
    1297             }
    1298 
    1299             // Only return the 1st item in the array.
    1300             $metadata[ $key ] = $default_style;
    1301         }
    1302     }
    1303     return $metadata;
    1304 }
    1305 add_filter( 'block_type_metadata', '_wp_multiple_block_styles' );
    1306 
    1307 /**
    13081332 * Helper function that constructs a comment query vars array from the passed
    13091333 * block properties.
Note: See TracChangeset for help on using the changeset viewer.