From 222fa331fc1596cf47e9d5d74e6924e60eb93f06 Mon Sep 17 00:00:00 2001 From: Jeff Ong Date: Mon, 2 Oct 2023 13:52:07 -0400 Subject: [PATCH] Fix bug in flattening function and expand test case. --- lib/class-wp-theme-json-gutenberg.php | 33 +++++++++++++++------------ phpunit/class-wp-theme-json-test.php | 28 +++++++++++++++++++++++ 2 files changed, 46 insertions(+), 15 deletions(-) diff --git a/lib/class-wp-theme-json-gutenberg.php b/lib/class-wp-theme-json-gutenberg.php index b9d75816ccc5cf..e2d4ccc1c5b120 100644 --- a/lib/class-wp-theme-json-gutenberg.php +++ b/lib/class-wp-theme-json-gutenberg.php @@ -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]; + } } } } @@ -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(); diff --git a/phpunit/class-wp-theme-json-test.php b/phpunit/class-wp-theme-json-test.php index 2837182b856337..e5cf057de5700c 100644 --- a/phpunit/class-wp-theme-json-test.php +++ b/phpunit/class-wp-theme-json-test.php @@ -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', @@ -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(