Skip to content

Commit 816ff68

Browse files
committed
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. git-svn-id: https://develop.svn.wordpress.org/trunk@58068 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 6974b99 commit 816ff68

3 files changed

Lines changed: 95 additions & 7 deletions

File tree

src/wp-includes/functions.wp-scripts.php

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,6 @@ function wp_register_script( $handle, $src, $deps = array(), $ver = false, $args
211211
*
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
217216
*
@@ -224,12 +223,7 @@ function wp_register_script( $handle, $src, $deps = array(), $ver = false, $args
224223
* @return bool True if the script was successfully localized, false otherwise.
225224
*/
226225
function 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 );
235229
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/**
2+
* External dependencies
3+
*/
4+
import { existsSync, mkdirSync, writeFileSync, unlinkSync } from 'node:fs';
5+
import { join } from 'node:path';
6+
7+
/**
8+
* WordPress dependencies
9+
*/
10+
import { test, expect } from '@wordpress/e2e-test-utils-playwright';
11+
12+
test.describe( 'Localize Script on wp-login.php', () => {
13+
const muPlugins = join(
14+
process.cwd(),
15+
process.env.LOCAL_DIR ?? 'src',
16+
'wp-content/mu-plugins'
17+
);
18+
const muPluginFile = join( muPlugins, 'login-test.php' );
19+
20+
test.beforeAll( async () => {
21+
const muPluginCode = `<?php
22+
add_action(
23+
'login_enqueue_scripts',
24+
function() {
25+
wp_localize_script(
26+
'wp-util',
27+
'testData',
28+
[
29+
'answerToTheUltimateQuestionOfLifeTheUniverseAndEverything' => 42,
30+
]
31+
);
32+
}
33+
);`;
34+
35+
if ( ! existsSync( muPlugins ) ) {
36+
mkdirSync( muPlugins, { recursive: true } );
37+
}
38+
writeFileSync( muPluginFile, muPluginCode );
39+
} );
40+
41+
test.afterAll( async () => {
42+
unlinkSync( muPluginFile );
43+
} );
44+
45+
test( 'should localize script', async ( { page } ) => {
46+
await page.goto( '/wp-login.php' );
47+
await page.waitForSelector( '#login' );
48+
const testData = await page.evaluate( () => window.testData );
49+
expect(
50+
testData.answerToTheUltimateQuestionOfLifeTheUniverseAndEverything
51+
).toBe( '42' );
52+
} );
53+
} );
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
/**
3+
* @group dependencies
4+
* @group scripts
5+
*/
6+
class Tests_Dependencies_LocalizeScript extends WP_UnitTestCase {
7+
/**
8+
* @var WP_Scripts
9+
*/
10+
protected $old_wp_scripts;
11+
12+
public function set_up() {
13+
parent::set_up();
14+
15+
$this->old_wp_scripts = $GLOBALS['wp_scripts'] ?? null;
16+
$GLOBALS['wp_scripts'] = null;
17+
}
18+
19+
public function tear_down() {
20+
$GLOBALS['wp_scripts'] = $this->old_wp_scripts;
21+
parent::tear_down();
22+
}
23+
24+
/**
25+
* Verifies that wp_localize_script() works if global has not been initialized yet.
26+
*
27+
* @ticket 60862
28+
* @covers ::wp_localize_script
29+
*/
30+
public function test_wp_localize_script_works_before_enqueue_script() {
31+
$this->assertTrue(
32+
wp_localize_script(
33+
'wp-util',
34+
'salcodeExample',
35+
array(
36+
'answerToTheUltimateQuestionOfLifeTheUniverseAndEverything' => 42,
37+
)
38+
)
39+
);
40+
}
41+
}

0 commit comments

Comments
 (0)