diff --git a/src/wp-includes/functions.wp-scripts.php b/src/wp-includes/functions.wp-scripts.php index 1207502c44839..de134961f8f3a 100644 --- a/src/wp-includes/functions.wp-scripts.php +++ b/src/wp-includes/functions.wp-scripts.php @@ -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 * @@ -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 ); } diff --git a/tests/e2e/specs/login-localize-script.test.js b/tests/e2e/specs/login-localize-script.test.js new file mode 100644 index 0000000000000..ecaa48998329d --- /dev/null +++ b/tests/e2e/specs/login-localize-script.test.js @@ -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 = ` 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' ); + } ); +} ); diff --git a/tests/phpunit/tests/dependencies/wpLocalizeScript.php b/tests/phpunit/tests/dependencies/wpLocalizeScript.php new file mode 100644 index 0000000000000..ed79d3e7bd5db --- /dev/null +++ b/tests/phpunit/tests/dependencies/wpLocalizeScript.php @@ -0,0 +1,41 @@ +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, + ) + ) + ); + } +}