Editor: Sanitize nested array in theme.json properly.

WP_Theme_JSON sanitization is now able to sanitize data contained on indexed arrays. 
So certain data from theme.json, for example, settings.typography.fontFamilies which is a JSON array will be sanitized.

Props mmaattiiaass, mukesh27.
Fixes #60360.

git-svn-id: https://develop.svn.wordpress.org/trunk@57496 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Riad Benguella
2024-01-31 10:53:48 +00:00
parent 4472385b88
commit b999860bb6
2 changed files with 261 additions and 12 deletions

View File

@@ -4978,4 +4978,139 @@ class Tests_Theme_wpThemeJson extends WP_UnitTestCase {
$actual
);
}
/**
* Tests that invalid properties are removed from the theme.json inside indexed arrays as settings.typography.fontFamilies.
*
* @ticket 60360
*/
public function test_sanitize_indexed_arrays() {
$theme_json = new WP_Theme_JSON(
array(
'version' => '2',
'badKey2' => 'I am Evil!',
'settings' => array(
'badKey3' => 'I am Evil!',
'typography' => array(
'badKey4' => 'I am Evil!',
'fontFamilies' => array(
'custom' => array(
array(
'badKey4' => 'I am Evil!',
'name' => 'Arial',
'slug' => 'arial',
'fontFamily' => 'Arial, sans-serif',
),
),
'theme' => array(
array(
'badKey5' => 'I am Evil!',
'name' => 'Piazzolla',
'slug' => 'piazzolla',
'fontFamily' => 'Piazzolla',
'fontFace' => array(
array(
'badKey6' => 'I am Evil!',
'fontFamily' => 'Piazzolla',
'fontStyle' => 'italic',
'fontWeight' => '400',
'src' => 'https://example.com/font.ttf',
),
array(
'badKey7' => 'I am Evil!',
'fontFamily' => 'Piazzolla',
'fontStyle' => 'italic',
'fontWeight' => '400',
'src' => 'https://example.com/font.ttf',
),
),
),
array(
'badKey8' => 'I am Evil!',
'name' => 'Inter',
'slug' => 'Inter',
'fontFamily' => 'Inter',
'fontFace' => array(
array(
'badKey9' => 'I am Evil!',
'fontFamily' => 'Inter',
'fontStyle' => 'italic',
'fontWeight' => '400',
'src' => 'https://example.com/font.ttf',
),
array(
'badKey10' => 'I am Evil!',
'fontFamily' => 'Inter',
'fontStyle' => 'italic',
'fontWeight' => '400',
'src' => 'https://example.com/font.ttf',
),
),
),
),
),
),
),
)
);
$expected_sanitized = array(
'version' => '2',
'settings' => array(
'typography' => array(
'fontFamilies' => array(
'custom' => array(
array(
'name' => 'Arial',
'slug' => 'arial',
'fontFamily' => 'Arial, sans-serif',
),
),
'theme' => array(
array(
'name' => 'Piazzolla',
'slug' => 'piazzolla',
'fontFamily' => 'Piazzolla',
'fontFace' => array(
array(
'fontFamily' => 'Piazzolla',
'fontStyle' => 'italic',
'fontWeight' => '400',
'src' => 'https://example.com/font.ttf',
),
array(
'fontFamily' => 'Piazzolla',
'fontStyle' => 'italic',
'fontWeight' => '400',
'src' => 'https://example.com/font.ttf',
),
),
),
array(
'name' => 'Inter',
'slug' => 'Inter',
'fontFamily' => 'Inter',
'fontFace' => array(
array(
'fontFamily' => 'Inter',
'fontStyle' => 'italic',
'fontWeight' => '400',
'src' => 'https://example.com/font.ttf',
),
array(
'fontFamily' => 'Inter',
'fontStyle' => 'italic',
'fontWeight' => '400',
'src' => 'https://example.com/font.ttf',
),
),
),
),
),
),
),
);
$sanitized_theme_json = $theme_json->get_raw_data();
$this->assertSameSetsWithIndex( $expected_sanitized, $sanitized_theme_json, 'Sanitized theme.json does not match' );
}
}