Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wp_localize_script() on login_enqueue_scripts hook change in behavior #6334

Closed
wants to merge 14 commits into from
Closed
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();
swissspidy marked this conversation as resolved.
Show resolved Hide resolved

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,
)
)
);
}
}