Skip to content

Commit 4cfdf59

Browse files
committed
Blocks: Allow reading the script handle from asset files
1 parent 482843a commit 4cfdf59

4 files changed

Lines changed: 110 additions & 42 deletions

File tree

‎src/wp-includes/blocks.php‎

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ function get_block_asset_url( $path ) {
129129
*
130130
* @since 5.5.0
131131
* @since 6.1.0 Added `$index` parameter.
132+
* @since 6.5.0 Added ability for reading a custom script handle from the block asset file.
132133
*
133134
* @param array $metadata Block metadata.
134135
* @param string $field_name Field name to pick from metadata.
@@ -142,22 +143,21 @@ function register_block_script_handle( $metadata, $field_name, $index = 0 ) {
142143
return false;
143144
}
144145

145-
$script_handle = $metadata[ $field_name ];
146-
if ( is_array( $script_handle ) ) {
147-
if ( empty( $script_handle[ $index ] ) ) {
146+
$script_handle_or_path = $metadata[ $field_name ];
147+
if ( is_array( $script_handle_or_path ) ) {
148+
if ( empty( $script_handle_or_path[ $index ] ) ) {
148149
return false;
149150
}
150-
$script_handle = $script_handle[ $index ];
151+
$script_handle_or_path = $script_handle_or_path[ $index ];
151152
}
152153

153-
$script_path = remove_block_asset_path_prefix( $script_handle );
154-
if ( $script_handle === $script_path ) {
155-
return $script_handle;
154+
$script_path = remove_block_asset_path_prefix( $script_handle_or_path );
155+
if ( $script_handle_or_path === $script_path ) {
156+
return $script_handle_or_path;
156157
}
157158

158159
$path = dirname( $metadata['file'] );
159160
$script_asset_raw_path = $path . '/' . substr_replace( $script_path, '.asset.php', - strlen( '.js' ) );
160-
$script_handle = generate_block_asset_handle( $metadata['name'], $field_name, $index );
161161
$script_asset_path = wp_normalize_path(
162162
realpath( $script_asset_raw_path )
163163
);
@@ -177,6 +177,14 @@ function register_block_script_handle( $metadata, $field_name, $index = 0 ) {
177177
return false;
178178
}
179179

180+
$script_asset = require $script_asset_path;
181+
$script_handle = isset( $script_asset['handle'] ) ?
182+
$script_asset['handle'] :
183+
generate_block_asset_handle( $metadata['name'], $field_name, $index );
184+
if ( wp_script_is( $script_handle, 'registered' ) ) {
185+
return $script_handle;
186+
}
187+
180188
$script_path_norm = wp_normalize_path( realpath( $path . '/' . $script_path ) );
181189
$script_uri = get_block_asset_url( $script_path_norm );
182190

@@ -185,13 +193,13 @@ function register_block_script_handle( $metadata, $field_name, $index = 0 ) {
185193
$script_args['strategy'] = 'defer';
186194
}
187195

188-
$script_asset = require $script_asset_path;
189196
$script_dependencies = isset( $script_asset['dependencies'] ) ? $script_asset['dependencies'] : array();
197+
$block_version = ! $is_core_block && isset( $metadata['version'] ) ? $metadata['version'] : false;
190198
$result = wp_register_script(
191199
$script_handle,
192200
$script_uri,
193201
$script_dependencies,
194-
isset( $script_asset['version'] ) ? $script_asset['version'] : false,
202+
isset( $script_asset['version'] ) ? $script_asset['version'] : $block_version,
195203
$script_args
196204
);
197205
if ( ! $result ) {
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
return array(
4+
'handle' => 'tests-my-shared-script',
5+
'dependencies' => array(),
6+
'version' => 'test',
7+
);
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/* Another test JavaScript file. */

‎tests/phpunit/tests/blocks/register.php‎

Lines changed: 84 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,18 @@ public function render_stub() {}
5252
* @since 5.0.0
5353
*/
5454
public function tear_down() {
55-
$registry = WP_Block_Type_Registry::get_instance();
55+
// Removes test block types registered by test cases.
56+
$block_types = WP_Block_Type_Registry::get_instance()->get_all_registered();
57+
foreach ( $block_types as $block_type ) {
58+
$block_name = $block_type->name;
59+
if ( str_starts_with( $block_name, 'tests/' ) ) {
60+
unregister_block_type( $block_name );
61+
}
62+
}
5663

57-
foreach ( array( 'core/test-static', 'core/test-dynamic', 'tests/notice' ) as $block_name ) {
58-
if ( $registry->is_registered( $block_name ) ) {
59-
$registry->unregister( $block_name );
64+
foreach ( wp_scripts()->registered as $script_handle ) {
65+
if ( str_starts_with( $script_handle, 'tests-' ) ) {
66+
wp_deregister_script( $script_handle );
6067
}
6168
}
6269

@@ -134,26 +141,26 @@ public function test_removes_block_asset_path_prefix_and_current_directory() {
134141
* @ticket 50263
135142
*/
136143
public function test_generate_block_asset_handle() {
137-
$block_name = 'unit-tests/my-block';
144+
$block_name = 'tests/my-block';
138145

139146
$this->assertSame(
140-
'unit-tests-my-block-editor-script',
147+
'tests-my-block-editor-script',
141148
generate_block_asset_handle( $block_name, 'editorScript' )
142149
);
143150
$this->assertSame(
144-
'unit-tests-my-block-script',
151+
'tests-my-block-script',
145152
generate_block_asset_handle( $block_name, 'script', 0 )
146153
);
147154
$this->assertSame(
148-
'unit-tests-my-block-view-script-100',
155+
'tests-my-block-view-script-100',
149156
generate_block_asset_handle( $block_name, 'viewScript', 99 )
150157
);
151158
$this->assertSame(
152-
'unit-tests-my-block-editor-style-2',
159+
'tests-my-block-editor-style-2',
153160
generate_block_asset_handle( $block_name, 'editorStyle', 1 )
154161
);
155162
$this->assertSame(
156-
'unit-tests-my-block-style',
163+
'tests-my-block-style',
157164
generate_block_asset_handle( $block_name, 'style' )
158165
);
159166
}
@@ -226,7 +233,7 @@ public function test_wrong_array_index_do_not_register_block_script_handle() {
226233
public function test_missing_asset_file_register_block_script_handle() {
227234
$metadata = array(
228235
'file' => __FILE__,
229-
'name' => 'unit-tests/test-block',
236+
'name' => 'tests/sample-block',
230237
'script' => 'file:./blocks/notice/missing-asset.js',
231238
);
232239
$result = register_block_script_handle( $metadata, 'script' );
@@ -264,14 +271,14 @@ public function test_handles_passed_register_block_script_handles() {
264271
public function test_success_register_block_script_handle() {
265272
$metadata = array(
266273
'file' => DIR_TESTDATA . '/blocks/notice/block.json',
267-
'name' => 'unit-tests/test-block',
274+
'name' => 'tests/sample-block',
268275
'script' => 'file:./block.js',
269276
);
270277
$result = register_block_script_handle( $metadata, 'script' );
271278

272-
$this->assertSame( 'unit-tests-test-block-script', $result );
279+
$this->assertSame( 'tests-sample-block-script', $result );
273280

274-
// Test the behavior directly within the unit test
281+
// Test the behavior directly within the unit test.
275282
$this->assertFalse(
276283
strpos(
277284
wp_normalize_path( realpath( dirname( $metadata['file'] ) . '/' . $metadata['script'] ) ),
@@ -287,6 +294,51 @@ public function test_success_register_block_script_handle() {
287294
);
288295
}
289296

297+
/**
298+
* @ticket TBD...
299+
*/
300+
public function test_success_register_block_script_handle_with_custom_handle_name() {
301+
$custom_script_handle = 'tests-my-shared-script';
302+
$metadata = array(
303+
'file' => DIR_TESTDATA . '/blocks/notice/block.json',
304+
'name' => 'tests/sample-block',
305+
'script' => 'file:./shared-script.js',
306+
);
307+
$result = register_block_script_handle( $metadata, 'script' );
308+
309+
$this->assertSame( $custom_script_handle, $result );
310+
$this->assertStringEndsWith(
311+
'shared-script.js',
312+
wp_scripts()->registered[ $custom_script_handle ]->src
313+
);
314+
}
315+
316+
/**
317+
* @ticket TBD...
318+
*/
319+
public function test_reuse_registered_block_script_handle_with_custom_handle_name() {
320+
$custom_script_handle = 'tests-my-shared-script';
321+
$custom_script_src = 'https://example.com/foo.js';
322+
wp_register_script( $custom_script_handle, $custom_script_src );
323+
324+
$this->assertTrue(
325+
wp_script_is( $custom_script_handle, 'registered' )
326+
);
327+
328+
$metadata = array(
329+
'file' => DIR_TESTDATA . '/blocks/notice/block.json',
330+
'name' => 'tests/sample-block',
331+
'script' => 'file:./shared-script.js',
332+
);
333+
$result = register_block_script_handle( $metadata, 'script' );
334+
335+
$this->assertSame( $custom_script_handle, $result );
336+
$this->assertSame(
337+
$custom_script_src,
338+
wp_scripts()->registered[ $custom_script_handle ]->src
339+
);
340+
}
341+
290342
/**
291343
* @ticket 55513
292344
*/
@@ -440,21 +492,21 @@ public function test_handles_passed_register_block_style_handles() {
440492
public function test_success_register_block_style_handle() {
441493
$metadata = array(
442494
'file' => DIR_TESTDATA . '/blocks/notice/block.json',
443-
'name' => 'unit-tests/test-block',
495+
'name' => 'tests/sample-block',
444496
'style' => 'file:./block.css',
445497
);
446498
$result = register_block_style_handle( $metadata, 'style' );
447499

448-
$this->assertSame( 'unit-tests-test-block-style', $result );
449-
$this->assertFalse( wp_styles()->get_data( 'unit-tests-test-block-style', 'rtl' ) );
500+
$this->assertSame( 'tests-sample-block-style', $result );
501+
$this->assertFalse( wp_styles()->get_data( 'tests-sample-block-style', 'rtl' ) );
450502

451-
// @ticket 50328
503+
// @ticket 50328.
452504
$this->assertSame(
453505
wp_normalize_path( realpath( DIR_TESTDATA . '/blocks/notice/block.css' ) ),
454-
wp_normalize_path( wp_styles()->get_data( 'unit-tests-test-block-style', 'path' ) )
506+
wp_normalize_path( wp_styles()->get_data( 'tests-sample-block-style', 'path' ) )
455507
);
456508

457-
// Test the behavior directly within the unit test
509+
// Test the behavior directly within the unit test.
458510
$this->assertFalse(
459511
strpos(
460512
wp_normalize_path( realpath( dirname( $metadata['file'] ) . '/' . $metadata['style'] ) ),
@@ -483,22 +535,22 @@ public function test_register_block_style_handle_should_load_rtl_stylesheets_for
483535

484536
$metadata = array(
485537
'file' => DIR_TESTDATA . '/blocks/notice/block.json',
486-
'name' => 'unit-tests/test-block-rtl',
538+
'name' => 'tests/sample-block-rtl',
487539
'style' => 'file:./block.css',
488540
);
489541

490542
$orig_text_dir = $wp_locale->text_direction;
491543
$wp_locale->text_direction = 'rtl';
492544

493545
$handle = register_block_style_handle( $metadata, 'style' );
494-
$extra_rtl = wp_styles()->get_data( 'unit-tests-test-block-rtl-style', 'rtl' );
495-
$extra_suffix = wp_styles()->get_data( 'unit-tests-test-block-rtl-style', 'suffix' );
496-
$extra_path = wp_normalize_path( wp_styles()->get_data( 'unit-tests-test-block-rtl-style', 'path' ) );
546+
$extra_rtl = wp_styles()->get_data( 'tests-sample-block-rtl-style', 'rtl' );
547+
$extra_suffix = wp_styles()->get_data( 'tests-sample-block-rtl-style', 'suffix' );
548+
$extra_path = wp_normalize_path( wp_styles()->get_data( 'tests-sample-block-rtl-style', 'path' ) );
497549

498550
$wp_locale->text_direction = $orig_text_dir;
499551

500552
$this->assertSame(
501-
'unit-tests-test-block-rtl-style',
553+
'tests-sample-block-rtl-style',
502554
$handle,
503555
'The handle did not match the expected handle.'
504556
);
@@ -528,13 +580,13 @@ public function test_register_block_style_handle_should_load_rtl_stylesheets_for
528580
public function test_register_nonexistent_stylesheet() {
529581
$metadata = array(
530582
'file' => DIR_TESTDATA . '/blocks/notice/block.json',
531-
'name' => 'unit-tests/test-block-nonexistent-stylesheet',
583+
'name' => 'tests/sample-block-nonexistent-stylesheet',
532584
'style' => 'file:./nonexistent.css',
533585
);
534586
register_block_style_handle( $metadata, 'style' );
535587

536588
global $wp_styles;
537-
$this->assertFalse( $wp_styles->registered['unit-tests-test-block-nonexistent-stylesheet-style']->src );
589+
$this->assertFalse( $wp_styles->registered['tests-sample-block-nonexistent-stylesheet-style']->src );
538590
}
539591

540592
/**
@@ -638,13 +690,13 @@ public function test_block_registers_with_metadata_fixture() {
638690
$result->provides_context
639691
);
640692
$this->assertSameSets( array( 'groupId' ), $result->uses_context );
641-
// @ticket 57585
693+
// @ticket 57585.
642694
$this->assertSame(
643695
array( 'root' => '.wp-block-notice' ),
644696
$result->selectors,
645697
'Block type should contain selectors from metadata.'
646698
);
647-
// @ticket 59346
699+
// @ticket 59346.
648700
$this->assertSameSets(
649701
array(
650702
'tests/before' => 'before',
@@ -716,13 +768,13 @@ public function test_block_registers_with_metadata_fixture() {
716768
$result->style_handles
717769
);
718770

719-
// @ticket 50328
771+
// @ticket 50328.
720772
$this->assertSame(
721773
wp_normalize_path( realpath( DIR_TESTDATA . '/blocks/notice/block.css' ) ),
722-
wp_normalize_path( wp_styles()->get_data( 'unit-tests-test-block-style', 'path' ) )
774+
wp_normalize_path( wp_styles()->get_data( 'tests-sample-block-style', 'path' ) )
723775
);
724776

725-
// @ticket 53148
777+
// @ticket 53148.
726778
$this->assertIsCallable( $result->render_callback );
727779
}
728780

0 commit comments

Comments
 (0)