Skip to content

Commit b450826

Browse files
westonrutersirrealgemini-code-assist[bot]
committed
Validate module_dependencies in WP_Scripts::add_data()
Ensures that the 'module_dependencies' value is an array of strings. Non-array values trigger a _doing_it_wrong() notice and return false, while non-string items within an array are stripped with a notice. Co-authored-by: Jon Surrell <[email protected]> Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
1 parent babe954 commit b450826

File tree

2 files changed

+91
-0
lines changed

2 files changed

+91
-0
lines changed

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

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -920,6 +920,45 @@ public function add_data( $handle, $key, $value ) {
920920
);
921921
return false;
922922
}
923+
} elseif ( 'module_dependencies' === $key ) {
924+
if ( ! is_array( $value ) ) {
925+
_doing_it_wrong(
926+
__METHOD__,
927+
sprintf(
928+
/* translators: 1: 'module_dependencies', 2: Script handle. */
929+
__( 'The value for "%1$s" must be an array for the "%2$s" script.' ),
930+
'module_dependencies',
931+
$handle
932+
),
933+
'7.0.0'
934+
);
935+
return false;
936+
}
937+
938+
$sanitized_value = array();
939+
$has_invalid_ids = false;
940+
foreach ( $value as $id ) {
941+
if ( ! is_string( $id ) ) {
942+
$has_invalid_ids = true;
943+
} else {
944+
$sanitized_value[] = $id;
945+
}
946+
}
947+
948+
if ( $has_invalid_ids ) {
949+
_doing_it_wrong(
950+
__METHOD__,
951+
sprintf(
952+
/* translators: 1: Script handle, 2: 'module_dependencies' */
953+
__( 'The script handle "%1$s" has one or more of its script module dependencies ("%2$s") which are not strings.' ),
954+
$handle,
955+
'module_dependencies'
956+
),
957+
'7.0.0'
958+
);
959+
}
960+
961+
$value = $sanitized_value;
923962
}
924963
return parent::add_data( $handle, $key, $value );
925964
}

tests/phpunit/tests/dependencies/scripts.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1277,6 +1277,58 @@ public function test_invalid_fetchpriority_on_alias() {
12771277
$this->assertArrayNotHasKey( 'fetchpriority', wp_scripts()->registered['alias']->extra );
12781278
}
12791279

1280+
/**
1281+
* Tests validation of module_dependencies in WP_Scripts::add_data().
1282+
*
1283+
* @ticket 61500
1284+
*
1285+
* @covers WP_Scripts::add_data
1286+
*
1287+
* @dataProvider data_add_data_module_dependencies_validation
1288+
*
1289+
* @param mixed $data Data to add.
1290+
* @param string $message Expected error message.
1291+
* @param bool $expected Expected return value.
1292+
* @param array|null $stored Expected stored value.
1293+
*/
1294+
public function test_add_data_module_dependencies_validation( $data, string $message, bool $expected, ?array $stored ) {
1295+
wp_register_script( 'test-script', '/test.js' );
1296+
1297+
$expected_incorrect_usage = 'WP_Scripts::add_data';
1298+
$this->setExpectedIncorrectUsage( $expected_incorrect_usage );
1299+
1300+
$this->assertSame( $expected, wp_scripts()->add_data( 'test-script', 'module_dependencies', $data ) );
1301+
$this->assertStringContainsString( $message, $this->caught_doing_it_wrong[ $expected_incorrect_usage ] );
1302+
1303+
if ( null === $stored ) {
1304+
$this->assertFalse( wp_scripts()->get_data( 'test-script', 'module_dependencies' ) );
1305+
} else {
1306+
$this->assertSame( $stored, wp_scripts()->get_data( 'test-script', 'module_dependencies' ) );
1307+
}
1308+
}
1309+
1310+
/**
1311+
* Data provider.
1312+
*
1313+
* @return array<string, array{data: mixed, message: string, expected: bool, stored: string[]|null}>
1314+
*/
1315+
public function data_add_data_module_dependencies_validation(): array {
1316+
return array(
1317+
'non-array' => array(
1318+
'data' => 'not-an-array',
1319+
'message' => 'The value for "module_dependencies" must be an array',
1320+
'expected' => false,
1321+
'stored' => null,
1322+
),
1323+
'bad-items' => array(
1324+
'data' => array( 'valid', 123, true, array() ),
1325+
'message' => 'has script module dependencies ("module_dependencies") that are not strings and have been removed',
1326+
'expected' => true,
1327+
'stored' => array( 'valid' ),
1328+
),
1329+
);
1330+
}
1331+
12801332
/**
12811333
* Data provider.
12821334
*

0 commit comments

Comments
 (0)