Skip to content

Commit

Permalink
Script Loader: Ensure wp_localize_script() works when called early.
Browse files Browse the repository at this point in the history
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.


git-svn-id: https://develop.svn.wordpress.org/branches/6.5@58078 602fd350-edb4-49c9-b593-d223f7449a82
  • Loading branch information
aaronjorbin committed May 2, 2024
1 parent 4340ceb commit 9a960d4
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 7 deletions.
8 changes: 1 addition & 7 deletions src/wp-includes/functions.wp-scripts.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand All @@ -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 );
}
Expand Down
53 changes: 53 additions & 0 deletions tests/e2e/specs/login-localize-script.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/**
* External dependencies
*/
import { existsSync, mkdirSync, writeFileSync, unlinkSync } from 'node:fs';
import { join } from 'node:path';

/**
* WordPress dependencies
*/
import { test, expect } from '@wordpress/e2e-test-utils-playwright';

test.describe( 'Localize Script on wp-login.php', () => {
const muPlugins = join(
process.cwd(),
process.env.LOCAL_DIR ?? 'src',
'wp-content/mu-plugins'
);
const muPluginFile = join( muPlugins, 'login-test.php' );

test.beforeAll( async () => {
const muPluginCode = `<?php
add_action(
'login_enqueue_scripts',
function() {
wp_localize_script(
'wp-util',
'testData',
[
'answerToTheUltimateQuestionOfLifeTheUniverseAndEverything' => 42,
]
);
}
);`;

if ( ! existsSync( muPlugins ) ) {
mkdirSync( muPlugins, { recursive: true } );
}
writeFileSync( muPluginFile, muPluginCode );
} );

test.afterAll( async () => {
unlinkSync( muPluginFile );
} );

test( 'should localize script', async ( { page } ) => {
await page.goto( '/wp-login.php' );
await page.waitForSelector( '#login' );
const testData = await page.evaluate( () => window.testData );
expect(
testData.answerToTheUltimateQuestionOfLifeTheUniverseAndEverything
).toBe( '42' );
} );
} );
41 changes: 41 additions & 0 deletions tests/phpunit/tests/dependencies/wpLocalizeScript.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php
/**
* @group dependencies
* @group scripts
*/
class Tests_Dependencies_LocalizeScript extends WP_UnitTestCase {
/**
* @var WP_Scripts
*/
protected $old_wp_scripts;

public function set_up() {
parent::set_up();

$this->old_wp_scripts = $GLOBALS['wp_scripts'] ?? null;
$GLOBALS['wp_scripts'] = null;
}

public function tear_down() {
$GLOBALS['wp_scripts'] = $this->old_wp_scripts;
parent::tear_down();
}

/**
* Verifies that wp_localize_script() works if global has not been initialized yet.
*
* @ticket 60862
* @covers ::wp_localize_script
*/
public function test_wp_localize_script_works_before_enqueue_script() {
$this->assertTrue(
wp_localize_script(
'wp-util',
'salcodeExample',
array(
'answerToTheUltimateQuestionOfLifeTheUniverseAndEverything' => 42,
)
)
);
}
}

0 comments on commit 9a960d4

Please sign in to comment.