diff --git a/src/wp-includes/class-wp-script-modules.php b/src/wp-includes/class-wp-script-modules.php index dbfa038f8cbe2..b08d323dd0a25 100644 --- a/src/wp-includes/class-wp-script-modules.php +++ b/src/wp-includes/class-wp-script-modules.php @@ -265,12 +265,21 @@ public function print_import_map() { * @return array Array with an `imports` key mapping to an array of script module identifiers and their respective * URLs, including the version query. */ - private function get_import_map(): array { + public function get_import_map(): array { $imports = array(); foreach ( $this->get_dependencies( array_keys( $this->get_marked_for_enqueue() ) ) as $id => $script_module ) { $imports[ $id ] = $this->get_src( $id ); } - return array( 'imports' => $imports ); + $import_map = array( 'imports' => $imports ); + + /** + * Filters the import map before it is returned. + * + * @since 6.8.0 + * + * @param array $import_map The import map array containing script module identifiers and their URLs. + */ + return apply_filters( 'script_modules_import_map', $import_map ); } /** @@ -280,7 +289,7 @@ private function get_import_map(): array { * * @return array[] Script modules marked for enqueue, keyed by script module identifier. */ - private function get_marked_for_enqueue(): array { + public function get_marked_for_enqueue(): array { $enqueued = array(); foreach ( $this->registered as $id => $script_module ) { if ( true === $script_module['enqueue'] ) { @@ -503,4 +512,29 @@ public function print_a11y_script_module_html() { . '
' . ''; } + + /** + * Checks if a script module is marked for enqueue. + * + * @since 6.8.0 + * + * @param string $id The script module identifier. + * @return bool True if the script module is marked for enqueue, false otherwise. + */ + public function is_marked_for_enqueue( string $id ): bool { + return isset( $this->registered[ $id ] ) && true === $this->registered[ $id ]['enqueue']; + } + + /** + * Checks if a script module will be included in the import map. + * + * @since 6.8.0 + * + * @param string $id The script module identifier. + * @return bool True if the script module will be included in the import map, false otherwise. + */ + public function is_marked_for_import( string $id ): bool { + $import_map = $this->get_import_map(); + return isset( $import_map['imports'][ $id ] ); + } } diff --git a/tests/phpunit/tests/script-modules/wpScriptModules.php b/tests/phpunit/tests/script-modules/wpScriptModules.php index 85f9599f0dac3..628106f1f0066 100644 --- a/tests/phpunit/tests/script-modules/wpScriptModules.php +++ b/tests/phpunit/tests/script-modules/wpScriptModules.php @@ -906,4 +906,39 @@ public static function data_invalid_script_module_data(): array { 'string' => array( 'string' ), ); } + + /** + * Tests that is_marked_for_enqueue() correctly identifies enqueued modules. + * + * @ticket 60597 + * + * @covers ::is_marked_for_enqueue + */ + public function test_is_marked_for_enqueue() { + $this->script_modules->register( 'foo', '/foo.js' ); + $this->script_modules->register( 'bar', '/bar.js' ); + $this->script_modules->enqueue( 'foo' ); + + $this->assertTrue( $this->script_modules->is_marked_for_enqueue( 'foo' ) ); + $this->assertFalse( $this->script_modules->is_marked_for_enqueue( 'bar' ) ); + $this->assertFalse( $this->script_modules->is_marked_for_enqueue( 'nonexistent' ) ); + } + + /** + * Tests that is_marked_for_import() correctly identifies modules in the import map. + * + * @ticket 60597 + * + * @covers ::is_marked_for_import + */ + public function test_is_marked_for_import() { + $this->script_modules->register( 'foo', '/foo.js' ); + $this->script_modules->register( 'bar', '/bar.js', array( 'foo' ) ); + $this->script_modules->register( 'baz', '/baz.js', array( 'bar' ) ); + $this->script_modules->enqueue( 'baz' ); + + $this->assertTrue( $this->script_modules->is_marked_for_import( 'foo' ) ); + $this->assertTrue( $this->script_modules->is_marked_for_import( 'bar' ) ); + $this->assertFalse( $this->script_modules->is_marked_for_import( 'nonexistent' ) ); + } }