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.

Thanks for the birthday wishes, Pascal!

Reviewed by Jorbin.
Merges [58068] to the 6.5 branch.

Props salcode, aslamdoctor, jorbin, swissspidy.
Fixes #60862.

Built from https://develop.svn.wordpress.org/branches/6.5@58078


git-svn-id: http://core.svn.wordpress.org/branches/6.5@57543 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Aaron Jorbin 2024-05-02 15:06:16 +00:00
parent def147d01e
commit 4546b73afb
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.5.3-alpha-58066';
$wp_version = '6.5.3-alpha-58078';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.