Skip to content

Commit f9c245c

Browse files
committed
Make get_src return nullable
1 parent 3224849 commit f9c245c

File tree

1 file changed

+20
-6
lines changed

1 file changed

+20
-6
lines changed

src/wp-includes/class-wp-script-modules.php

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -231,10 +231,15 @@ public function add_hooks() {
231231
*/
232232
public function print_enqueued_script_modules() {
233233
foreach ( $this->get_marked_for_enqueue() as $id => $script_module ) {
234+
$src = $this->get_src( $id );
235+
if ( null === $src ) {
236+
continue;
237+
}
238+
234239
wp_print_script_tag(
235240
array(
236241
'type' => 'module',
237-
'src' => $this->get_src( $id ),
242+
'src' => $src,
238243
'id' => $id . '-js-module',
239244
)
240245
);
@@ -251,11 +256,16 @@ public function print_enqueued_script_modules() {
251256
*/
252257
public function print_script_module_preloads() {
253258
foreach ( $this->get_dependencies( array_keys( $this->get_marked_for_enqueue() ), array( 'static' ) ) as $id => $script_module ) {
259+
$src = $this->get_src( $id );
260+
if ( null === $src ) {
261+
continue;
262+
}
263+
254264
// Don't preload if it's marked for enqueue.
255265
if ( true !== $script_module['enqueue'] ) {
256266
echo sprintf(
257267
'<link rel="modulepreload" href="%s" id="%s">',
258-
esc_url( $this->get_src( $id ) ),
268+
esc_url( $src ),
259269
esc_attr( $id . '-js-modulepreload' )
260270
);
261271
}
@@ -293,7 +303,11 @@ private function get_import_map(): array {
293303
$imports = array();
294304
$script_module_ids = array_unique( array_keys( $this->exposed ) + array_keys( $this->get_marked_for_enqueue() ) );
295305
foreach ( $this->get_dependencies( $script_module_ids ) as $id => $script_module ) {
296-
$imports[ $id ] = $this->get_src( $id );
306+
$src = $this->get_src( $id );
307+
if ( null === $src ) {
308+
continue;
309+
}
310+
$imports[ $id ] = $src;
297311
}
298312
return array( 'imports' => $imports );
299313
}
@@ -360,11 +374,11 @@ function ( $dependency_script_modules, $id ) use ( $import_types ) {
360374
* @since 6.5.0
361375
*
362376
* @param string $id The script module identifier.
363-
* @return string The script module src with a version if relevant.
377+
* @return string|null The script module src with a version if relevant.
364378
*/
365-
private function get_src( string $id ): string {
379+
private function get_src( string $id ): ?string {
366380
if ( ! isset( $this->registered[ $id ] ) ) {
367-
return '';
381+
return null;
368382
}
369383

370384
$script_module = $this->registered[ $id ];

0 commit comments

Comments
 (0)