diff --git a/src/wp-includes/template.php b/src/wp-includes/template.php index f799113e9c26e..f17307e19daa2 100644 --- a/src/wp-includes/template.php +++ b/src/wp-includes/template.php @@ -926,13 +926,24 @@ function wp_finalize_template_enhancement_output_buffer( string $output, int $ph $is_html_content_type = null; $html_content_types = array( 'text/html', 'application/xhtml+xml' ); foreach ( headers_list() as $header ) { - $header_parts = preg_split( '/\s*[:;]\s*/', strtolower( $header ) ); + $header_parts = explode( ':', strtolower( $header ), 2 ); if ( - is_array( $header_parts ) && - count( $header_parts ) >= 2 && + count( $header_parts ) === 2 && 'content-type' === $header_parts[0] ) { - $is_html_content_type = in_array( $header_parts[1], $html_content_types, true ); + /* + * This is looking for very specific content types, therefore it + * doesn’t need to fully parse the header’s value. Instead, it needs + * only assert that the content type is one of the static HTML types. + * + * Example: + * + * Content-Type: text/html; charset=utf8 + * Content-Type: text/html ;charset=latin4 + * Content-Type:application/xhtml+xml + */ + $media_type = trim( strtok( $header_parts[1], ';' ), " \t" ); + $is_html_content_type = in_array( $media_type, $html_content_types, true ); break; // PHP only sends the first Content-Type header in the list. } } diff --git a/tests/phpunit/tests/template.php b/tests/phpunit/tests/template.php index f7dcf015bff03..89136c00e3361 100644 --- a/tests/phpunit/tests/template.php +++ b/tests/phpunit/tests/template.php @@ -597,6 +597,9 @@ static function ( string $buffer ) use ( &$filter_args ): string { PHP_INT_MAX ); + $this->assertCount( 0, headers_list(), 'Expected no headers to have been sent during unit tests.' ); + ini_set( 'default_mimetype', 'text/html' ); // Since sending a header won't work. + $initial_ob_level = ob_get_level(); $this->assertTrue( wp_start_template_enhancement_output_buffer(), 'Expected wp_start_template_enhancement_output_buffer() to return true indicating the output buffer started.' ); $this->assertSame( 1, did_action( 'wp_template_enhancement_output_buffer_started' ), 'Expected the wp_template_enhancement_output_buffer_started action to have fired.' ); @@ -676,6 +679,9 @@ static function ( string $buffer ) use ( &$applied_filter ): string { } ); + $this->assertCount( 0, headers_list(), 'Expected no headers to have been sent during unit tests.' ); + ini_set( 'default_mimetype', 'text/html' ); // Since sending a header won't work. + $initial_ob_level = ob_get_level(); $this->assertTrue( wp_start_template_enhancement_output_buffer(), 'Expected wp_start_template_enhancement_output_buffer() to return true indicating the output buffer started.' ); $this->assertSame( 1, did_action( 'wp_template_enhancement_output_buffer_started' ), 'Expected the wp_template_enhancement_output_buffer_started action to have fired.' ); @@ -740,6 +746,9 @@ static function ( string $buffer ) use ( &$called_filter ): string { } ); + $this->assertCount( 0, headers_list(), 'Expected no headers to have been sent during unit tests.' ); + ini_set( 'default_mimetype', 'application/xhtml+xml' ); // Since sending a header won't work. + $initial_ob_level = ob_get_level(); $this->assertTrue( wp_start_template_enhancement_output_buffer(), 'Expected wp_start_template_enhancement_output_buffer() to return true indicating the output buffer started.' ); $this->assertSame( 1, did_action( 'wp_template_enhancement_output_buffer_started' ), 'Expected the wp_template_enhancement_output_buffer_started action to have fired.' ); @@ -749,15 +758,18 @@ static function ( string $buffer ) use ( &$called_filter ): string { + Unprocessed

Hello World!

+ '; ?> - + + Template Replaced @@ -799,7 +811,9 @@ public function test_wp_start_template_enhancement_output_buffer_for_json(): voi $this->assertSame( 1, did_action( 'wp_template_enhancement_output_buffer_started' ), 'Expected the wp_template_enhancement_output_buffer_started action to have fired.' ); $this->assertSame( $initial_ob_level + 1, ob_get_level(), 'Expected the output buffer level to have been incremented.' ); + $this->assertCount( 0, headers_list(), 'Expected no headers to have been sent during unit tests.' ); ini_set( 'default_mimetype', 'application/json' ); // Since sending a header won't work. + $json = wp_json_encode( array( 'success' => true,