From 45cacbb26723b8c71036ccf5e612f9e0709ca154 Mon Sep 17 00:00:00 2001 From: Jon Surrell Date: Thu, 19 Dec 2024 18:15:57 +0100 Subject: [PATCH] Remove a loop from importmap checking --- src/wp-includes/class-wp-script-modules.php | 27 ++++++++++----------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/src/wp-includes/class-wp-script-modules.php b/src/wp-includes/class-wp-script-modules.php index 08bf9d06183a3..e7b9ad2482280 100644 --- a/src/wp-includes/class-wp-script-modules.php +++ b/src/wp-includes/class-wp-script-modules.php @@ -280,7 +280,10 @@ public function print_import_map() { */ private function get_import_map(): array { global $wp_scripts; - $classic_script_module_dependencies = array(); + + $imports = array(); + + $classic_script_dependencies = array(); if ( $wp_scripts instanceof WP_Scripts ) { foreach ( $wp_scripts->registered as $dependency ) { $handle = $dependency->handle; @@ -297,19 +300,18 @@ private function get_import_map(): array { if ( ! $module_deps ) { continue; } - array_push( $classic_script_module_dependencies, ...$module_deps ); + foreach ( $module_deps as $id ) { + $src = $this->get_src( $id ); + if ( null === $src ) { + continue; + } + $imports[ $id ] = $src; + $classic_script_dependencies[] = $id; + } } } - $imports = array(); - foreach ( $this->get_dependencies( array_merge( $classic_script_module_dependencies, array_keys( $this->get_marked_for_enqueue() ) ) ) as $id => $script_module ) { - $src = $this->get_src( $id ); - if ( null === $src ) { - continue; - } - $imports[ $id ] = $src; - } - foreach ( $classic_script_module_dependencies as $id ) { + foreach ( $this->get_dependencies( array_merge( $classic_script_dependencies, array_keys( $this->get_marked_for_enqueue() ) ) ) as $id => $script_module ) { $src = $this->get_src( $id ); if ( null === $src ) { continue; @@ -356,9 +358,6 @@ private function get_dependencies( array $ids, array $import_types = array( 'sta return array_reduce( $ids, function ( $dependency_script_modules, $id ) use ( $import_types ) { - if ( ! isset( $this->registered[ $id ]['dependencies'] ) ) { - return $dependency_script_modules; - } $dependencies = array(); foreach ( $this->registered[ $id ]['dependencies'] as $dependency ) { if (