Skip to content

Commit 3381f05

Browse files
committed
Blocks: Allow registering multiple items for all supported asset types
Follow-up #54337, [52069]. Part of WordPress/gutenberg#41236. More details in WordPress/gutenberg#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. git-svn-id: https://develop.svn.wordpress.org/trunk@54155 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 73c668d commit 3381f05

File tree

10 files changed

+544
-261
lines changed

10 files changed

+544
-261
lines changed

src/wp-includes/block-editor.php

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -324,17 +324,16 @@ function _wp_get_iframed_editor_assets() {
324324
$block_registry = WP_Block_Type_Registry::get_instance();
325325

326326
foreach ( $block_registry->get_all_registered() as $block_type ) {
327-
if ( ! empty( $block_type->style ) ) {
328-
$style_handles[] = $block_type->style;
329-
}
330-
331-
if ( ! empty( $block_type->editor_style ) ) {
332-
$style_handles[] = $block_type->editor_style;
333-
}
327+
$style_handles = array_merge(
328+
$style_handles,
329+
$block_type->style_handles,
330+
$block_type->editor_style_handles
331+
);
334332

335-
if ( ! empty( $block_type->script ) ) {
336-
$script_handles[] = $block_type->script;
337-
}
333+
$script_handles = array_merge(
334+
$script_handles,
335+
$block_type->script_handles
336+
);
338337
}
339338

340339
$style_handles = array_unique( $style_handles );

src/wp-includes/blocks.php

Lines changed: 111 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,15 @@ function remove_block_asset_path_prefix( $asset_handle_or_path ) {
3535
* and the field name provided.
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 ) {
46+
function 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 );
4649
if ( 0 === strpos( $field_name, 'editor' ) ) {
@@ -49,6 +52,9 @@ function generate_block_asset_handle( $block_name, $field_name ) {
4952
if ( 0 === strpos( $field_name, 'view' ) ) {
5053
$asset_handle .= '-view';
5154
}
55+
if ( $index > 0 ) {
56+
$asset_handle .= '-' . ( $index + 1 );
57+
}
5258
return $asset_handle;
5359
}
5460

@@ -59,8 +65,12 @@ function generate_block_asset_handle( $block_name, $field_name ) {
5965
'editorStyle' => 'editor-style',
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

6676
/**
@@ -70,23 +80,34 @@ function generate_block_asset_handle( $block_name, $field_name ) {
7080
* generated handle name. It returns unprocessed script handle otherwise.
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 ) {
92+
function 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(
92113
dirname( $metadata['file'] ) . '/' .
@@ -145,33 +166,49 @@ function register_block_script_handle( $metadata, $field_name ) {
145166
* generated handle name. It returns unprocessed style handle otherwise.
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 ) {
178+
function 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

165-
// Check whether styles should have a ".min" suffix or not.
166-
$suffix = SCRIPT_DEBUG ? '' : '.min';
167-
168191
$style_handle = $metadata[ $field_name ];
169-
$style_path = remove_block_asset_path_prefix( $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+
}
170198

171-
if ( $style_handle === $style_path && ! $is_core_block ) {
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 ) ) {
172207
return $style_handle;
173208
}
174209

210+
// Check whether styles should have a ".min" suffix or not.
211+
$suffix = SCRIPT_DEBUG ? '' : '.min';
175212
$style_uri = plugins_url( $style_path, $metadata['file'] );
176213
if ( $is_core_block ) {
177214
$style_path = "style$suffix.css";
@@ -185,9 +222,9 @@ function register_block_style_handle( $metadata, $field_name ) {
185222
$style_uri = get_theme_file_uri( str_replace( $theme_path_norm, '', $style_path_norm ) );
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;
193230
$style_uri = $has_style_file ? $style_uri : false;
@@ -311,39 +348,69 @@ function register_block_type_from_metadata( $file_or_folder, $args = array() ) {
311348
}
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-
);
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+
}
340382
}
341383

342-
if ( ! empty( $metadata['style'] ) ) {
343-
$settings['style'] = register_block_style_handle(
344-
$metadata,
345-
'style'
346-
);
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

349416
if ( ! empty( $metadata['render'] ) ) {
@@ -1261,49 +1328,6 @@ function get_query_pagination_arrow( $block, $is_next ) {
12611328
return null;
12621329
}
12631330

1264-
/**
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-
13071331
/**
13081332
* Helper function that constructs a comment query vars array from the passed
13091333
* block properties.

0 commit comments

Comments
 (0)