@@ -35,12 +35,15 @@ function remove_block_asset_path_prefix( $asset_handle_or_path ) {
35
35
* and the field name provided.
36
36
*
37
37
* @since 5.5.0
38
+ * @since 6.1.0 Added `$index` parameter.
38
39
*
39
40
* @param string $block_name Name of the block.
40
41
* @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.
41
44
* @return string Generated asset name for the block's field.
42
45
*/
43
- function generate_block_asset_handle ( $ block_name , $ field_name ) {
46
+ function generate_block_asset_handle ( $ block_name , $ field_name, $ index = 0 ) {
44
47
if ( 0 === strpos ( $ block_name , 'core/ ' ) ) {
45
48
$ asset_handle = str_replace ( 'core/ ' , 'wp-block- ' , $ block_name );
46
49
if ( 0 === strpos ( $ field_name , 'editor ' ) ) {
@@ -49,6 +52,9 @@ function generate_block_asset_handle( $block_name, $field_name ) {
49
52
if ( 0 === strpos ( $ field_name , 'view ' ) ) {
50
53
$ asset_handle .= '-view ' ;
51
54
}
55
+ if ( $ index > 0 ) {
56
+ $ asset_handle .= '- ' . ( $ index + 1 );
57
+ }
52
58
return $ asset_handle ;
53
59
}
54
60
@@ -59,8 +65,12 @@ function generate_block_asset_handle( $block_name, $field_name ) {
59
65
'editorStyle ' => 'editor-style ' ,
60
66
'style ' => 'style ' ,
61
67
);
62
- return str_replace ( '/ ' , '- ' , $ block_name ) .
68
+ $ asset_handle = str_replace ( '/ ' , '- ' , $ block_name ) .
63
69
'- ' . $ field_mappings [ $ field_name ];
70
+ if ( $ index > 0 ) {
71
+ $ asset_handle .= '- ' . ( $ index + 1 );
72
+ }
73
+ return $ asset_handle ;
64
74
}
65
75
66
76
/**
@@ -70,23 +80,34 @@ function generate_block_asset_handle( $block_name, $field_name ) {
70
80
* generated handle name. It returns unprocessed script handle otherwise.
71
81
*
72
82
* @since 5.5.0
83
+ * @since 6.1.0 Added `$index` parameter.
73
84
*
74
85
* @param array $metadata Block metadata.
75
86
* @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.
76
89
* @return string|false Script handle provided directly or created through
77
90
* script's registration, or false on failure.
78
91
*/
79
- function register_block_script_handle ( $ metadata , $ field_name ) {
92
+ function register_block_script_handle ( $ metadata , $ field_name, $ index = 0 ) {
80
93
if ( empty ( $ metadata [ $ field_name ] ) ) {
81
94
return false ;
82
95
}
96
+
83
97
$ 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 );
85
106
if ( $ script_handle === $ script_path ) {
86
107
return $ script_handle ;
87
108
}
88
109
89
- $ script_handle = generate_block_asset_handle ( $ metadata ['name ' ], $ field_name );
110
+ $ script_handle = generate_block_asset_handle ( $ metadata ['name ' ], $ field_name, $ index );
90
111
$ script_asset_path = wp_normalize_path (
91
112
realpath (
92
113
dirname ( $ metadata ['file ' ] ) . '/ ' .
@@ -145,33 +166,49 @@ function register_block_script_handle( $metadata, $field_name ) {
145
166
* generated handle name. It returns unprocessed style handle otherwise.
146
167
*
147
168
* @since 5.5.0
169
+ * @since 6.1.0 Added `$index` parameter.
148
170
*
149
171
* @param array $metadata Block metadata.
150
172
* @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.
151
175
* @return string|false Style handle provided directly or created through
152
176
* style's registration, or false on failure.
153
177
*/
154
- function register_block_style_handle ( $ metadata , $ field_name ) {
178
+ function register_block_style_handle ( $ metadata , $ field_name, $ index = 0 ) {
155
179
if ( empty ( $ metadata [ $ field_name ] ) ) {
156
180
return false ;
157
181
}
182
+
158
183
$ wpinc_path_norm = wp_normalize_path ( realpath ( ABSPATH . WPINC ) );
159
184
$ theme_path_norm = wp_normalize_path ( get_theme_file_path () );
160
185
$ 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.
161
187
if ( $ is_core_block && ! wp_should_load_separate_core_block_assets () ) {
162
188
return false ;
163
189
}
164
190
165
- // Check whether styles should have a ".min" suffix or not.
166
- $ suffix = SCRIPT_DEBUG ? '' : '.min ' ;
167
-
168
191
$ 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
+ }
170
198
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 ) ) {
172
207
return $ style_handle ;
173
208
}
174
209
210
+ // Check whether styles should have a ".min" suffix or not.
211
+ $ suffix = SCRIPT_DEBUG ? '' : '.min ' ;
175
212
$ style_uri = plugins_url ( $ style_path , $ metadata ['file ' ] );
176
213
if ( $ is_core_block ) {
177
214
$ style_path = "style $ suffix.css " ;
@@ -185,9 +222,9 @@ function register_block_style_handle( $metadata, $field_name ) {
185
222
$ style_uri = get_theme_file_uri ( str_replace ( $ theme_path_norm , '' , $ style_path_norm ) );
186
223
}
187
224
188
- $ style_handle = generate_block_asset_handle ( $ metadata ['name ' ], $ field_name );
225
+ $ style_handle = generate_block_asset_handle ( $ metadata ['name ' ], $ field_name, $ index );
189
226
$ block_dir = dirname ( $ metadata ['file ' ] );
190
- $ style_file = realpath ( "$ block_dir/ $ style_path " );
227
+ $ style_file = wp_normalize_path ( realpath ( "$ block_dir/ $ style_path " ) );
191
228
$ has_style_file = false !== $ style_file ;
192
229
$ version = ! $ is_core_block && isset ( $ metadata ['version ' ] ) ? $ metadata ['version ' ] : false ;
193
230
$ style_uri = $ has_style_file ? $ style_uri : false ;
@@ -311,39 +348,69 @@ function register_block_type_from_metadata( $file_or_folder, $args = array() ) {
311
348
}
312
349
}
313
350
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
+ }
340
382
}
341
383
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
+ }
347
414
}
348
415
349
416
if ( ! empty ( $ metadata ['render ' ] ) ) {
@@ -1261,49 +1328,6 @@ function get_query_pagination_arrow( $block, $is_next ) {
1261
1328
return null ;
1262
1329
}
1263
1330
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
-
1307
1331
/**
1308
1332
* Helper function that constructs a comment query vars array from the passed
1309
1333
* block properties.
0 commit comments