Script Loader: Ensure `wp_localize_script()` works when called early.

Before, `wp_localize_script()` did not work when the `$wp_scripts` global was not already set (for example because of a script registration happening elsewhere) and even emitted a warning in that case. Due to side effects such as block registration early in the load process, this usually never happened. However, the absence of these side effects in 6.5 caused the `wp_localize_script()` to no longer work in places such as the `login_enqueue_scripts`.

By calling `wp_scripts()` in `wp_localize_script()`, the `$wp_scripts` global is automatically set if needed, restoring previous behavior. Adds both a PHP unit test and an e2e test to verify this use case. Hat tip: jorbin.

Happy birthday, Aaron!

Props salcode, aslamdoctor, jorbin, swissspidy.
Fixes #60862.
Built from https://develop.svn.wordpress.org/trunk@58068


git-svn-id: http://core.svn.wordpress.org/trunk@57533 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Pascal Birchler 2024-05-01 15:42:11 +00:00
parent 8aa7eb7e16
commit 5d6024cefb
2 changed files with 2 additions and 8 deletions

View File

@ -211,7 +211,6 @@ function wp_register_script( $handle, $src, $deps = array(), $ver = false, $args
*
* @see WP_Scripts::localize()
* @link https://core.trac.wordpress.org/ticket/11520
* @global WP_Scripts $wp_scripts The WP_Scripts object for printing scripts.
*
* @since 2.2.0
*
@ -224,12 +223,7 @@ function wp_register_script( $handle, $src, $deps = array(), $ver = false, $args
* @return bool True if the script was successfully localized, false otherwise.
*/
function wp_localize_script( $handle, $object_name, $l10n ) {
global $wp_scripts;
if ( ! ( $wp_scripts instanceof WP_Scripts ) ) {
_wp_scripts_maybe_doing_it_wrong( __FUNCTION__, $handle );
return false;
}
$wp_scripts = wp_scripts();
return $wp_scripts->localize( $handle, $object_name, $l10n );
}

View File

@ -16,7 +16,7 @@
*
* @global string $wp_version
*/
$wp_version = '6.6-alpha-58067';
$wp_version = '6.6-alpha-58068';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.