Make WordPress Core

Changeset 58068

Timestamp:
05/01/2024 03:40:02 PM (3 months ago)
Author:
swissspidy
Message:

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.

Location:
trunk
Files:
2 added
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/functions.wp-scripts.php

    r57046 r58068  
    212212 * @see WP_Scripts::localize()
    213213 * @link https://core.trac.wordpress.org/ticket/11520
    214  * @global WP_Scripts $wp_scripts The WP_Scripts object for printing scripts.
    215214 *
    216215 * @since 2.2.0
     
    225224 */
    226225function wp_localize_script( $handle, $object_name, $l10n ) {
    227     global $wp_scripts;
    228 
    229     if ( ! ( $wp_scripts instanceof WP_Scripts ) ) {
    230         _wp_scripts_maybe_doing_it_wrong( __FUNCTION__, $handle );
    231         return false;
    232     }
     226    $wp_scripts = wp_scripts();
    233227
    234228    return $wp_scripts->localize( $handle, $object_name, $l10n );
Note: See TracChangeset for help on using the changeset viewer.