diff --git a/lib/experimental/fonts/class-wp-font-face.php b/lib/experimental/fonts/class-wp-font-face.php index db65d3452aa69..482bf4d42396d 100644 --- a/lib/experimental/fonts/class-wp-font-face.php +++ b/lib/experimental/fonts/class-wp-font-face.php @@ -118,6 +118,11 @@ function_exists( 'current_theme_supports' ) && ! current_theme_supports( 'html5' public function generate_and_print( array $fonts ) { $fonts = $this->validate_fonts( $fonts ); + // Bail out if there are no fonts are given to process. + if ( empty( $fonts ) ) { + return; + } + printf( $this->get_style_element(), $this->get_css( $fonts ) diff --git a/phpunit/fonts/wpFontFace/generateAndPrint-test.php b/phpunit/fonts/wpFontFace/generateAndPrint-test.php index b5ddeeb705a52..302395d8fead6 100644 --- a/phpunit/fonts/wpFontFace/generateAndPrint-test.php +++ b/phpunit/fonts/wpFontFace/generateAndPrint-test.php @@ -32,6 +32,14 @@ class Tests_Fonts_WPFontFace_GenerateAndPrint extends WP_UnitTestCase { use WP_Font_Face_Tests_Datasets; + public function test_should_not_generate_and_print_when_no_fonts() { + $font_face = new WP_Font_Face(); + $fonts = array(); + + $this->expectOutputString( '' ); + $font_face->generate_and_print( $fonts ); + } + /** * @dataProvider data_should_print_given_fonts * diff --git a/phpunit/fonts/wpPrintFontFaces-test.php b/phpunit/fonts/wpPrintFontFaces-test.php index 5ae336de8a0e4..aa44dd4f71b0d 100644 --- a/phpunit/fonts/wpPrintFontFaces-test.php +++ b/phpunit/fonts/wpPrintFontFaces-test.php @@ -38,6 +38,13 @@ public static function set_up_before_class() { parent::set_up_before_class(); } + public function test_should_not_print_when_no_fonts() { + switch_theme( 'block-theme' ); + + $this->expectOutputString( '' ); + wp_print_font_faces(); + } + /** * @dataProvider data_should_print_given_fonts *