diff --git a/src/wp-includes/class-wp-styles.php b/src/wp-includes/class-wp-styles.php index 9b210b2df9d30..6d47bc15d56fd 100644 --- a/src/wp-includes/class-wp-styles.php +++ b/src/wp-includes/class-wp-styles.php @@ -183,12 +183,11 @@ public function do_item( $handle, $group = false ) { $inline_style = $this->print_inline_style( $handle, false ); if ( $inline_style ) { - $inline_style_tag = sprintf( - "\n", - esc_attr( $handle ), - $this->type_attr, - $inline_style - ); + $processor = new WP_HTML_Tag_Processor( "type_attr}>\n" ); + $processor->next_tag(); + $processor->set_attribute( 'id', "{$handle}-inline-css" ); + $processor->set_modifiable_text( "\n{$inline_style}\n" ); + $inline_style_tag = $processor->get_updated_html(); } else { $inline_style_tag = ''; } @@ -364,12 +363,11 @@ public function print_inline_style( $handle, $display = true ) { return $output; } - printf( - "\n", - esc_attr( $handle ), - $this->type_attr, - $output - ); + $processor = new WP_HTML_Tag_Processor( "type_attr}>\n" ); + $processor->next_tag(); + $processor->set_attribute( 'id', "{$handle}-inline-css" ); + $processor->set_modifiable_text( "\n{$output}\n" ); + echo $processor->get_updated_html(); return true; } diff --git a/src/wp-includes/fonts/class-wp-font-face.php b/src/wp-includes/fonts/class-wp-font-face.php index 07cd3d6de9002..fd66825a0b4e5 100644 --- a/src/wp-includes/fonts/class-wp-font-face.php +++ b/src/wp-includes/fonts/class-wp-font-face.php @@ -118,7 +118,13 @@ public function generate_and_print( array $fonts ) { return; } - printf( $this->get_style_element(), $css ); + $processor = new WP_HTML_Tag_Processor( "\n" ); + $processor->next_tag(); + foreach ( $this->style_tag_attrs as $name => $value ) { + $processor->set_attribute( $name, $value ); + } + $processor->set_modifiable_text( "\n{$css}\n" ); + echo $processor->get_updated_html(); } /** @@ -219,34 +225,6 @@ private function validate_font_face_declarations( array $font_face ) { return $font_face; } - /** - * Gets the style element for wrapping the `@font-face` CSS. - * - * @since 6.4.0 - * - * @return string The style element. - */ - private function get_style_element() { - $attributes = $this->generate_style_element_attributes(); - - return "\n"; - } - - /** - * Gets the defined \n"; + $processor = new WP_HTML_Tag_Processor( "" ); + $processor->next_tag(); + $style_tag_contents = "\n{$wp_styles->print_code}\n" + . sprintf( "/*# sourceURL=%s */\n", rawurlencode( $concat_source_url ) ); + $processor->set_modifiable_text( $style_tag_contents ); + echo $processor->get_updated_html(); } } @@ -3217,7 +3219,10 @@ function wp_enqueue_block_support_styles( $style, $priority = 10 ) { add_action( $action_hook_name, static function () use ( $style ) { - echo "\n"; + $processor = new WP_HTML_Tag_Processor( "\n" ); + $processor->next_tag(); + $processor->set_modifiable_text( $style ); + echo $processor->get_updated_html(); }, $priority ); diff --git a/src/wp-includes/theme.php b/src/wp-includes/theme.php index 558bea6ae9e02..b8d35a255d35c 100644 --- a/src/wp-includes/theme.php +++ b/src/wp-includes/theme.php @@ -1953,11 +1953,13 @@ function _custom_background_cb() { $style .= $image . $position . $size . $repeat . $attachment; } - ?> - id="custom-background-css"> -body.custom-background { } - - " ); + $processor->next_tag(); + + $style_tag_content = 'body.custom-background { ' . trim( $style ) . ' }'; + $processor->set_modifiable_text( "\n{$style_tag_content}\n" ); + echo $processor->get_updated_html(); } /** @@ -1967,17 +1969,18 @@ function _custom_background_cb() { */ function wp_custom_css_cb() { $styles = wp_get_custom_css(); - if ( $styles || is_customize_preview() ) : - $type_attr = current_theme_supports( 'html5', 'style' ) ? '' : ' type="text/css"'; - ?> - id="wp-custom-css"> - - - ' ); + $processor->next_tag(); + if ( ! current_theme_supports( 'html5', 'style' ) ) { + $processor->set_attribute( 'type', 'text/css' ); + } + $processor->set_attribute( 'id', 'wp-custom-css' ); + $processor->set_modifiable_text( "\n{$styles}\n" ); + echo $processor->get_updated_html(); } /** diff --git a/tests/phpunit/tests/fonts/font-face/wpFontFace/generateAndPrint.php b/tests/phpunit/tests/fonts/font-face/wpFontFace/generateAndPrint.php index d10ea500e8707..1f6b07b3cfb0c 100644 --- a/tests/phpunit/tests/fonts/font-face/wpFontFace/generateAndPrint.php +++ b/tests/phpunit/tests/fonts/font-face/wpFontFace/generateAndPrint.php @@ -34,7 +34,11 @@ public function test_should_generate_and_print_given_fonts( array $fonts, $expec $style_element = "\n"; $expected_output = sprintf( $style_element, $expected ); - $this->expectOutputString( $expected_output ); - $font_face->generate_and_print( $fonts ); + $output = get_echo( + function () use ( $font_face, $fonts ) { + $font_face->generate_and_print( $fonts ); + } + ); + $this->assertEqualHTML( $expected_output, $output ); } } diff --git a/tests/phpunit/tests/fonts/font-face/wpPrintFontFaces.php b/tests/phpunit/tests/fonts/font-face/wpPrintFontFaces.php index 2fa64559c2049..1d6ef663c5f22 100644 --- a/tests/phpunit/tests/fonts/font-face/wpPrintFontFaces.php +++ b/tests/phpunit/tests/fonts/font-face/wpPrintFontFaces.php @@ -37,8 +37,12 @@ public function test_should_not_print_when_no_fonts() { public function test_should_print_given_fonts( array $fonts, $expected ) { $expected_output = $this->get_expected_styles_output( $expected ); - $this->expectOutputString( $expected_output ); - wp_print_font_faces( $fonts ); + $output = get_echo( + function () use ( $fonts ) { + wp_print_font_faces( $fonts ); + } + ); + $this->assertEqualHTML( $expected_output, $output ); } public function test_should_escape_tags() { @@ -60,9 +64,13 @@ public function test_should_escape_tags() { CSS; - $this->expectOutputString( $expected_output ); - wp_print_font_faces( $fonts ); + $output = get_echo( + function () use ( $fonts ) { + wp_print_font_faces( $fonts ); + } + ); + $this->assertEqualHTML( $expected_output, $output ); } public function test_should_print_fonts_in_merged_data() { @@ -71,8 +79,8 @@ public function test_should_print_fonts_in_merged_data() { $expected = $this->get_expected_fonts_for_fonts_block_theme( 'font_face_styles' ); $expected_output = $this->get_expected_styles_output( $expected ); - $this->expectOutputString( $expected_output ); - wp_print_font_faces(); + $output = get_echo( 'wp_print_font_faces' ); + $this->assertEqualHTML( $expected_output, $output ); } private function get_expected_styles_output( $styles ) { diff --git a/tests/phpunit/tests/fonts/font-face/wpPrintFontFacesFromStyleVariations.php b/tests/phpunit/tests/fonts/font-face/wpPrintFontFacesFromStyleVariations.php index a59ba882a4e86..5dd6304fd2f7b 100644 --- a/tests/phpunit/tests/fonts/font-face/wpPrintFontFacesFromStyleVariations.php +++ b/tests/phpunit/tests/fonts/font-face/wpPrintFontFacesFromStyleVariations.php @@ -43,8 +43,8 @@ public function test_should_print_fonts_in_style_variations() { $expected = $this->get_custom_style_variations( 'expected_styles' ); $expected_output = $this->get_expected_styles_output( $expected ); - $this->expectOutputString( $expected_output ); - wp_print_font_faces_from_style_variations(); + $output = get_echo( 'wp_print_font_faces_from_style_variations' ); + $this->assertEqualHTML( $expected_output, $output ); } private function get_expected_styles_output( $styles ) {