Make WordPress Core

Changeset 58078


Ignore:
Timestamp:
05/02/2024 03:04:04 PM (3 months ago)
Author:
jorbin
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.

Thanks for the birthday wishes, Pascal!

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

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

Location:
branches/6.5
Files:
2 edited
2 copied

Legend:

Unmodified
Added
Removed
  • branches/6.5

  • branches/6.5/src/wp-includes/functions.wp-scripts.php

    r57046 r58078  
    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.