You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
General: Add wp_send_late_headers action which fires right before the template enhancement output buffer is flushed.
This adds a (missing) `wp_send_late_headers` action which fires right after the `wp_template_enhancement_output_buffer` filters have applied and right before the output buffer is flushed. The filtered output buffer is passed as an argument to the action so that plugins may do things like send an `ETag` header which is calculated from the content. This action eliminates the need for plugins to hack the `wp_template_enhancement_output_buffer` filter with a high priority to send a late response header. This action compliments the `send_headers` action which is commonly used to send HTTP headers before the template is rendered. Furthermore:
* The template enhancement output buffer is now enabled by default if there is a callback added to either the `wp_template_enhancement_output_buffer` filter or the `wp_send_late_headers` action.
* The `wp_start_template_enhancement_output_buffer()` callback for the `wp_before_include_template` action is increased from the default of 10 to 1000. This goes with the previous point, so that plugins can add those filters and actions during the `wp_before_include_template` action without having to worry about adding them too late, that is, after `wp_start_template_enhancement_output_buffer()` has run.
* The `wp_send_late_headers` action fires regardless of whether the buffered response is HTML.
Developed in #10381
Follow-up to [60936].
Props westonruter, peterwilsoncc, johnbillion.
See #43258.
Fixes #64126.
git-svn-id: https://develop.svn.wordpress.org/trunk@61088 602fd350-edb4-49c9-b593-d223f7449a82
add_action( 'wp_before_include_template', 'wp_start_template_enhancement_output_buffer', 1000 ); // Late priority to let `wp_template_enhancement_output_buffer` filters and `wp_send_late_headers` actions be registered.
$this->assertCount( 1, $action_args, 'Expected the wp_send_late_headers action to have been passed only one argument.' );
740
+
$this->assertSame( $processed_output, $action_args[0], 'Expected the arg passed to wp_send_late_headers to be the same as the processed output buffer.' );
719
741
}
720
742
721
743
/**
722
744
* Tests that wp_start_template_enhancement_output_buffer() starts the expected output buffer but ending with cleaning prevents any processing.
@@ -743,6 +770,14 @@ static function ( string $buffer ) use ( &$applied_filter ): string {
743
770
}
744
771
);
745
772
773
+
$mock_action_callback = newMockAction();
774
+
add_filter(
775
+
'wp_send_late_headers',
776
+
array( $mock_action_callback, 'action' ),
777
+
10,
778
+
PHP_INT_MAX
779
+
);
780
+
746
781
$this->assertCount( 0, headers_list(), 'Expected no headers to have been sent during unit tests.' );
747
782
ini_set( 'default_mimetype', 'text/html' ); // Since sending a header won't work.
748
783
@@ -774,34 +809,41 @@ static function ( string $buffer ) use ( &$applied_filter ): string {
774
809
775
810
$this->assertSame( $initial_ob_level, ob_get_level(), 'Expected the output buffer to be back at the initial level.' );
776
811
777
-
$this->assertFalse( $applied_filter, 'Expected the wp_template_enhancement_output_buffer filter to not have applied.' );
778
-
$this->assertSame( 0, did_action( 'wp_final_template_output_buffer' ), 'Expected the wp_final_template_output_buffer action to not have fired.' );
812
+
$this->assertSame( 0, $mock_filter_callback->get_call_count(), 'Expected the wp_template_enhancement_output_buffer filter to not have applied.' );
779
813
780
814
// Obtain the output via the wrapper output buffer.
781
815
$output = ob_get_clean();
782
816
$this->assertIsString( $output, 'Expected ob_get_clean() to return a string.' );
783
817
$this->assertStringNotContainsString( '<title>Unprocessed</title>', $output, 'Expected output buffer to not have string since the template was overridden.' );
784
818
$this->assertStringNotContainsString( '<title>Processed</title>', $output, 'Expected output buffer to not have string since the filter did not apply.' );
785
819
$this->assertStringContainsString( '<title>Output Buffer Not Processed</title>', $output, 'Expected output buffer to have string since the output buffer was ended with cleaning.' );
820
+
821
+
$this->assertSame( 0, did_action( 'wp_send_late_headers' ), 'Expected the wp_send_late_headers action to not have fired.' );
822
+
$this->assertSame( 0, $mock_action_callback->get_call_count(), 'Expected wp_send_late_headers action callback to have been called once.' );
786
823
}
787
824
788
825
/**
789
826
* Tests that wp_start_template_enhancement_output_buffer() starts the expected output buffer and cleaning allows the template to be replaced.
$this->assertTrue( wp_start_template_enhancement_output_buffer(), 'Expected wp_start_template_enhancement_output_buffer() to return true indicating the output buffer started.' );
875
941
$this->assertSame( 1, did_action( 'wp_template_enhancement_output_buffer_started' ), 'Expected the wp_template_enhancement_output_buffer_started action to have fired.' );
@@ -903,6 +969,12 @@ public function test_wp_start_template_enhancement_output_buffer_for_json(): voi
903
969
$output = ob_get_clean();
904
970
$this->assertIsString( $output, 'Expected ob_get_clean() to return a string.' );
905
971
$this->assertSame( $json, $output, 'Expected output to not be processed.' );
972
+
973
+
$this->assertSame( 1, did_action( 'wp_send_late_headers' ), 'Expected the wp_send_late_headers action to have fired even though the wp_template_enhancement_output_buffer filter did not apply.' );
974
+
$this->assertSame( 1, $mock_action_callback->get_call_count(), 'Expected wp_send_late_headers action callback to have been called once.' );
0 commit comments