Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions src/wp-includes/template.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
}
}
Expand Down
16 changes: 15 additions & 1 deletion tests/phpunit/tests/template.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.' );
Expand Down Expand Up @@ -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.' );
Expand Down Expand Up @@ -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.' );
Expand All @@ -749,15 +758,18 @@ static function ( string $buffer ) use ( &$called_filter ): string {
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Unprocessed</title>
</head>
<body>
<h1>Hello World!</h1>
<!-- ... -->
<?php ob_clean(); // Clean the buffer started by wp_start_template_enhancement_output_buffer(), allowing the following document to replace the above.. ?>
<?php echo '<?xml version="1.0" encoding="UTF-8"?>'; ?>
<!DOCTYPE html>
<html lang="en">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta charset="utf-8" />
<title>Template Replaced</title>
</head>
<body>
Expand Down Expand Up @@ -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,
Expand Down
Loading