Skip to content

Commit

Permalink
Fix bug in flattening function and expand test case.
Browse files Browse the repository at this point in the history
  • Loading branch information
jffng committed Nov 14, 2023
1 parent a6502ee commit 222fa33
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 15 deletions.
33 changes: 18 additions & 15 deletions lib/class-wp-theme-json-gutenberg.php
Original file line number Diff line number Diff line change
Expand Up @@ -822,15 +822,14 @@ protected static function sanitize( $input, $valid_block_names, $valid_element_n
// For settings.typography.fontFamilies, make the $schema have all indexes present in the $input.
if ( isset( $input['settings']['typography']['fontFamilies'] ) ) {
// Do not handle the cases where the sanitization is called before font families are merged, since it will be handled again later.
if ( isset( $input['settings']['typography']['fontFamilies']['theme'] ) || isset( $input['settings']['typography']['fontFamilies']['custom'] ) ) {
return $output;
}
foreach ( $input['settings']['typography']['fontFamilies'] as $font_family_key => $value ) {
$schema['settings']['typography']['fontFamilies'][ $font_family_key ] = static::VALID_SETTINGS['typography']['fontFamilies'][0];
// Do the same for fontFace.
if ( isset( $input['settings']['typography']['fontFamilies'][ $font_family_key ]['fontFace'] ) ) {
foreach ( $input['settings']['typography']['fontFamilies'][ $font_family_key ]['fontFace'] as $font_face_key => $value2 ) {
$schema['settings']['typography']['fontFamilies'][ $font_family_key ]['fontFace'][ $font_face_key ] = static::VALID_SETTINGS['typography']['fontFamilies'][0]['fontFace'][0];
if ( ! isset( $input['settings']['typography']['fontFamilies']['theme'] ) || ! isset( $input['settings']['typography']['fontFamilies']['custom'] ) ) {
foreach ( $input['settings']['typography']['fontFamilies'] as $font_family_key => $value ) {
$schema['settings']['typography']['fontFamilies'][ $font_family_key ] = static::VALID_SETTINGS['typography']['fontFamilies'][0];
// Do the same for fontFace.
if ( isset( $input['settings']['typography']['fontFamilies'][ $font_family_key ]['fontFace'] ) ) {
foreach ( $input['settings']['typography']['fontFamilies'][ $font_family_key ]['fontFace'] as $font_face_key => $value2 ) {
$schema['settings']['typography']['fontFamilies'][ $font_family_key ]['fontFace'][ $font_face_key ] = static::VALID_SETTINGS['typography']['fontFamilies'][0]['fontFace'][0];
}
}
}
}
Expand Down Expand Up @@ -3318,16 +3317,20 @@ public function get_data() {
$items = array();
if ( isset( $preset['theme'] ) ) {
foreach ( $preset['theme'] as $item ) {
$slug = $item['slug'];
unset( $item['slug'] );
$items[ $slug ] = $item;
if ( is_array( $item ) ){
$slug = $item['slug'];
unset( $item['slug'] );
$items[ $slug ] = $item;
}
}
}
if ( isset( $preset['custom'] ) ) {
foreach ( $preset['custom'] as $item ) {
$slug = $item['slug'];
unset( $item['slug'] );
$items[ $slug ] = $item;
if ( is_array( $item ) ){
$slug = $item['slug'];
unset( $item['slug'] );
$items[ $slug ] = $item;
}
}
}
$flattened_preset = array();
Expand Down
28 changes: 28 additions & 0 deletions phpunit/class-wp-theme-json-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -904,6 +904,21 @@ public function test_sanitize_invalid_typography_settings() {
'typography' => array(
'fontFamilies' => array(
'badKey' => 'I am invalid',
'theme' => array(
'name' => 'Inter',
'slug' => 'inter',
'badKey' => 'I am invalid',
'fontFamily' => 'Inter',
'fontFace' => array(
array(
'anotherKey' => 'I am invalid',
'fontFamily' => 'Inter',
'fontStyle' => 'normal',
'fontWeight' => '400',
'src' => 'https://example.com/inter.ttf',
),
),
),
array(
'name' => 'Piazzolla',
'slug' => 'piazzolla',
Expand Down Expand Up @@ -932,6 +947,19 @@ public function test_sanitize_invalid_typography_settings() {
'settings' => array(
'typography' => array(
'fontFamilies' => array(
array(
'fontFace' => array(
array(
'fontFamily' => 'Inter',
'fontStyle' => 'normal',
'fontWeight' => '400',
'src' => 'https://example.com/inter.ttf',
),
),
'fontFamily' => 'Inter',
'name' => 'Inter',
'slug' => 'inter',
),
array(
'fontFace' => array(
array(
Expand Down

0 comments on commit 222fa33

Please sign in to comment.