Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 13 additions & 13 deletions src/wp-includes/class-wp-script-modules.php
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,7 @@ public function print_script_module_translations(): void {
public function add_hooks() {
$is_block_theme = wp_is_block_theme();
$position = $is_block_theme ? 'wp_head' : 'wp_footer';
add_action( $position, array( $this, 'print_import_map' ) );
add_action( $position, 'wp_print_script_module_import_map' );
if ( $is_block_theme ) {
/*
* Modules can only be printed in the head for block themes because only with
Expand All @@ -442,28 +442,28 @@ public function add_hooks() {
* template rendering, thus the import map must be printed in the footer,
* followed by all enqueued modules.
*/
add_action( 'wp_head', array( $this, 'print_head_enqueued_script_modules' ) );
add_action( 'wp_head', 'wp_print_head_script_modules' );
}
add_action( 'wp_footer', array( $this, 'print_enqueued_script_modules' ) );
add_action( $position, array( $this, 'print_script_module_preloads' ) );
add_action( 'wp_footer', 'wp_print_script_modules' );
add_action( $position, 'wp_print_script_module_preloads' );

add_action( 'admin_print_footer_scripts', array( $this, 'print_import_map' ), 9 );
add_action( 'admin_print_footer_scripts', array( $this, 'print_enqueued_script_modules' ) );
add_action( 'admin_print_footer_scripts', array( $this, 'print_script_module_preloads' ) );
add_action( 'admin_print_footer_scripts', 'wp_print_script_module_import_map', 9 );
add_action( 'admin_print_footer_scripts', 'wp_print_script_modules' );
add_action( 'admin_print_footer_scripts', 'wp_print_script_module_preloads' );

/*
* Print translations after classic scripts like wp-i18n are loaded (at
* priority 10 via _wp_footer_scripts), but before the script modules
* execute. Script modules with type="module" are deferred by default,
* so inline translation scripts at priority 11 will execute before them.
*/
add_action( 'wp_footer', array( $this, 'print_script_module_translations' ), 21 );
add_action( 'admin_print_footer_scripts', array( $this, 'print_script_module_translations' ), 11 );
add_action( 'wp_footer', 'wp_print_script_module_translations', 21 );
add_action( 'admin_print_footer_scripts', 'wp_print_script_module_translations', 11 );

add_action( 'wp_footer', array( $this, 'print_script_module_data' ) );
add_action( 'admin_print_footer_scripts', array( $this, 'print_script_module_data' ) );
add_action( 'wp_footer', array( $this, 'print_a11y_script_module_html' ), 20 );
add_action( 'admin_print_footer_scripts', array( $this, 'print_a11y_script_module_html' ), 20 );
add_action( 'wp_footer', 'wp_print_script_module_data' );
add_action( 'admin_print_footer_scripts', 'wp_print_script_module_data' );
add_action( 'wp_footer', 'wp_print_a11y_script_module_html', 20 );
add_action( 'admin_print_footer_scripts', 'wp_print_a11y_script_module_html', 20 );
}

/**
Expand Down
77 changes: 77 additions & 0 deletions src/wp-includes/script-modules.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,83 @@ function wp_set_script_module_translations( string $id, string $domain = 'defaul
return wp_script_modules()->set_translations( $id, $domain, $path );
}

/**
* Prints the import map using a script tag with a type="importmap" attribute.
*
* @since 7.2.0
*
* @see WP_Script_Modules::print_import_map()
*/
function wp_print_script_module_import_map(): void {
wp_script_modules()->print_import_map();
}

/**
* Prints the enqueued script modules in head.
*
* @since 7.2.0
*
* @see WP_Script_Modules::print_head_enqueued_script_modules()
*/
function wp_print_head_script_modules(): void {
wp_script_modules()->print_head_enqueued_script_modules();
}

/**
* Prints the enqueued script modules in footer.
*
* @since 7.2.0
*
* @see WP_Script_Modules::print_enqueued_script_modules()
*/
function wp_print_script_modules(): void {
wp_script_modules()->print_enqueued_script_modules();
}

/**
* Prints the static dependencies of the enqueued script modules using link tags with rel="modulepreload" attributes.
*
* @since 7.2.0
*
* @see WP_Script_Modules::print_script_module_preloads()
*/
function wp_print_script_module_preloads(): void {
wp_script_modules()->print_script_module_preloads();
}

/**
* Prints the translations of the script modules that will be printed on the page.
*
* @since 7.2.0
*
* @see WP_Script_Modules::print_script_module_translations()
*/
function wp_print_script_module_translations(): void {
wp_script_modules()->print_script_module_translations();
}

/**
* Prints the data associated with the script modules that will be printed on the page.
*
* @since 7.2.0
*
* @see WP_Script_Modules::print_script_module_data()
*/
function wp_print_script_module_data(): void {
wp_script_modules()->print_script_module_data();
}

/**
* Prints the HTML regions used by the `@wordpress/a11y` script module.
*
* @since 7.2.0
*
* @see WP_Script_Modules::print_a11y_script_module_html()
*/
function wp_print_a11y_script_module_html(): void {
wp_script_modules()->print_a11y_script_module_html();
}

/**
* Registers all the default WordPress Script Modules.
*
Expand Down
31 changes: 31 additions & 0 deletions tests/phpunit/tests/script-modules/wpScriptModules.php
Original file line number Diff line number Diff line change
Expand Up @@ -2924,4 +2924,35 @@ static function ( $translations, $file, $handle, $domain ) use ( &$seen_domain )
$this->assertSame( 'my-plugin', $seen_domain, 'load_script_module_textdomain() should be called with the overridden domain.' );
$this->assertStringContainsString( 'Hola', $output, 'Output should contain the translated string loaded under the overridden domain.' );
}

/**
* Tests that the hooks added by add_hooks() print the script modules of the
* current global instance, not the instance that added the hooks.
*
* @ticket 66100
* @covers WP_Script_Modules::add_hooks
*/
public function test_add_hooks_callbacks_use_the_current_global_instance() {
global $wp_script_modules;

// Add the hooks the way wp-settings.php does, with the instance that is the global at that moment.
wp_script_modules()->add_hooks();

// Replace the global instance after the hooks have been added.
$wp_script_modules = new WP_Script_Modules();

wp_enqueue_script_module( 'test-hooks-global', '/test-hooks-global.js' );

$output = get_echo(
static function () {
do_action( 'admin_print_footer_scripts' );
}
);

$this->assertStringContainsString(
'test-hooks-global',
$output,
'The print callbacks added by add_hooks() must read the current global WP_Script_Modules instance.'
);
}
}
Loading