Skip to content

Commit 2c0960c

Browse files
Copilotsirreal
andcommitted
Add script_data_{$handle} filter for classic scripts
Co-authored-by: sirreal <[email protected]>
1 parent 661399e commit 2c0960c

File tree

2 files changed

+152
-0
lines changed

2 files changed

+152
-0
lines changed

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -634,6 +634,23 @@ public function localize( $handle, $object_name, $l10n ) {
634634
}
635635
}
636636

637+
/**
638+
* Filters data associated with a given script.
639+
*
640+
* The dynamic portion of the hook name, `$handle`, refers to the script handle.
641+
*
642+
* This filter allows developers to modify the data passed to a script via
643+
* wp_localize_script() before it is output. This is analogous to the
644+
* `script_module_data_{$module_id}` filter for script modules.
645+
*
646+
* @since 6.8.0
647+
*
648+
* @param array|string $l10n The data to be localized.
649+
* @param string $object_name The JavaScript object name.
650+
* @param string $handle The script handle.
651+
*/
652+
$l10n = apply_filters( "script_data_{$handle}", $l10n, $object_name, $handle );
653+
637654
$script = "var $object_name = " . wp_json_encode( $l10n, JSON_HEX_TAG | JSON_UNESCAPED_SLASHES ) . ';';
638655

639656
if ( ! empty( $after ) ) {

tests/phpunit/tests/dependencies/scripts.php

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4122,4 +4122,139 @@ public function test_wp_scripts_doing_it_wrong_for_missing_dependencies() {
41224122
'Expected _doing_it_wrong() notice to indicate missing dependencies for enqueued script.'
41234123
);
41244124
}
4125+
4126+
/**
4127+
* Tests that the script_data_{$handle} filter allows modifying localized script data.
4128+
*
4129+
* @ticket TBD
4130+
* @covers WP_Scripts::localize
4131+
*/
4132+
public function test_script_data_filter_modifies_localized_data() {
4133+
wp_enqueue_script( 'test-script', '/test.js', array(), null );
4134+
wp_localize_script( 'test-script', 'testData', array( 'foo' => 'bar' ) );
4135+
4136+
add_filter(
4137+
'script_data_test-script',
4138+
function ( $l10n, $object_name, $handle ) {
4139+
$this->assertSame( 'testData', $object_name );
4140+
$this->assertSame( 'test-script', $handle );
4141+
$this->assertIsArray( $l10n );
4142+
$this->assertSame( 'bar', $l10n['foo'] );
4143+
$l10n['baz'] = 'qux';
4144+
return $l10n;
4145+
},
4146+
10,
4147+
3
4148+
);
4149+
4150+
$output = get_echo( 'wp_print_scripts' );
4151+
4152+
$this->assertStringContainsString( '"foo":"bar"', $output );
4153+
$this->assertStringContainsString( '"baz":"qux"', $output );
4154+
}
4155+
4156+
/**
4157+
* Tests that the script_data_{$handle} filter receives correct parameters.
4158+
*
4159+
* @ticket TBD
4160+
* @covers WP_Scripts::localize
4161+
*/
4162+
public function test_script_data_filter_receives_correct_parameters() {
4163+
wp_enqueue_script( 'test-handle', '/test.js', array(), null );
4164+
wp_localize_script( 'test-handle', 'myObject', array( 'key' => 'value' ) );
4165+
4166+
$filter_called = false;
4167+
add_filter(
4168+
'script_data_test-handle',
4169+
function ( $l10n, $object_name, $handle ) use ( &$filter_called ) {
4170+
$filter_called = true;
4171+
$this->assertSame( array( 'key' => 'value' ), $l10n );
4172+
$this->assertSame( 'myObject', $object_name );
4173+
$this->assertSame( 'test-handle', $handle );
4174+
return $l10n;
4175+
},
4176+
10,
4177+
3
4178+
);
4179+
4180+
get_echo( 'wp_print_scripts' );
4181+
4182+
$this->assertTrue( $filter_called, 'Filter should have been called' );
4183+
}
4184+
4185+
/**
4186+
* Tests that the script_data_{$handle} filter works with multiple localizations.
4187+
*
4188+
* @ticket TBD
4189+
* @covers WP_Scripts::localize
4190+
*/
4191+
public function test_script_data_filter_with_multiple_localizations() {
4192+
wp_enqueue_script( 'test-script', '/test.js', array(), null );
4193+
wp_localize_script( 'test-script', 'data1', array( 'a' => '1' ) );
4194+
wp_localize_script( 'test-script', 'data2', array( 'b' => '2' ) );
4195+
4196+
$filter_call_count = 0;
4197+
add_filter(
4198+
'script_data_test-script',
4199+
function ( $l10n ) use ( &$filter_call_count ) {
4200+
$filter_call_count++;
4201+
$l10n['modified'] = 'yes';
4202+
return $l10n;
4203+
}
4204+
);
4205+
4206+
$output = get_echo( 'wp_print_scripts' );
4207+
4208+
$this->assertSame( 2, $filter_call_count, 'Filter should be called twice for two localizations' );
4209+
$this->assertStringContainsString( '"modified":"yes"', $output );
4210+
}
4211+
4212+
/**
4213+
* Tests that the script_data_{$handle} filter can return the data unmodified.
4214+
*
4215+
* @ticket TBD
4216+
* @covers WP_Scripts::localize
4217+
*/
4218+
public function test_script_data_filter_returns_data_unmodified() {
4219+
wp_enqueue_script( 'test-script', '/test.js', array(), null );
4220+
wp_localize_script( 'test-script', 'testData', array( 'foo' => 'bar' ) );
4221+
4222+
add_filter(
4223+
'script_data_test-script',
4224+
function ( $l10n ) {
4225+
// Return data unmodified.
4226+
return $l10n;
4227+
}
4228+
);
4229+
4230+
$output = get_echo( 'wp_print_scripts' );
4231+
4232+
$this->assertStringContainsString( 'var testData = {"foo":"bar"};', $output );
4233+
}
4234+
4235+
/**
4236+
* Tests that the script_data_{$handle} filter works correctly with jquery handle remapping.
4237+
*
4238+
* @ticket TBD
4239+
* @covers WP_Scripts::localize
4240+
*/
4241+
public function test_script_data_filter_with_jquery_handle() {
4242+
wp_enqueue_script( 'jquery' );
4243+
wp_localize_script( 'jquery', 'jqueryData', array( 'test' => 'value' ) );
4244+
4245+
$filter_called = false;
4246+
add_filter(
4247+
'script_data_jquery-core',
4248+
function ( $l10n ) use ( &$filter_called ) {
4249+
$filter_called = true;
4250+
$l10n['filtered'] = 'true';
4251+
return $l10n;
4252+
}
4253+
);
4254+
4255+
$output = get_echo( 'wp_print_scripts' );
4256+
4257+
$this->assertTrue( $filter_called, 'Filter should be called for jquery-core handle' );
4258+
$this->assertStringContainsString( '"filtered":"true"', $output );
4259+
}
41254260
}

0 commit comments

Comments
 (0)