Skip to content

Commit

Permalink
Allow list of enqueued module data to be exposed
Browse files Browse the repository at this point in the history
  • Loading branch information
Sukhendu2002 committed Jan 1, 2025
1 parent de14028 commit 3526c28
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 3 deletions.
40 changes: 37 additions & 3 deletions src/wp-includes/class-wp-script-modules.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}

/**
Expand All @@ -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'] ) {
Expand Down Expand Up @@ -503,4 +512,29 @@ public function print_a11y_script_module_html() {
. '<div id="a11y-speak-polite" class="a11y-speak-region" aria-live="polite" aria-relevant="additions text" aria-atomic="true"></div>'
. '</div>';
}

/**
* 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 ] );
}
}
35 changes: 35 additions & 0 deletions tests/phpunit/tests/script-modules/wpScriptModules.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' ) );
}
}

0 comments on commit 3526c28

Please sign in to comment.