@@ -922,6 +922,21 @@ function wp_start_template_enhancement_output_buffer(): bool {
922922 * @return string Finalized output buffer.
923923 */
924924function wp_finalize_template_enhancement_output_buffer ( string $ output , int $ phase ): string {
925+ $ do_send_late_headers = static function ( string $ output ): void {
926+ /**
927+ * Fires at the last moment HTTP headers may be sent.
928+ *
929+ * This happens immediately before the template enhancement output buffer is flushed. This is in contrast with
930+ * the {@see 'send_headers'} action which fires after the initial headers have been sent before the template
931+ * has begun rendering, and thus does not depend on output buffering.
932+ *
933+ * @since 6.9.0
934+ *
935+ * @param string $output Output buffer.
936+ */
937+ do_action ( 'wp_send_late_headers ' , $ output );
938+ };
939+
925940 // When the output is being cleaned (e.g. pending template is replaced with error page), do not send it through the filter.
926941 if ( ( $ phase & PHP_OUTPUT_HANDLER_CLEAN ) !== 0 ) {
927942 return $ output ;
@@ -958,6 +973,7 @@ function wp_finalize_template_enhancement_output_buffer( string $output, int $ph
958973
959974 // If the content type is not HTML, short-circuit since it is not relevant for enhancement.
960975 if ( ! $ is_html_content_type ) {
976+ $ do_send_late_headers ( $ output );
961977 return $ output ;
962978 }
963979
@@ -980,16 +996,7 @@ function wp_finalize_template_enhancement_output_buffer( string $output, int $ph
980996 */
981997 $ filtered_output = (string ) apply_filters ( 'wp_template_enhancement_output_buffer ' , $ filtered_output , $ output );
982998
983- /**
984- * Fires at the last moment HTTP headers may be sent.
985- *
986- * This happens immediately after the template enhancement output buffer has been processed.
987- *
988- * @since 6.9.0
989- *
990- * @param string $filtered_output Filtered output buffer.
991- */
992- do_action ( 'wp_send_late_headers ' , $ filtered_output );
999+ $ do_send_late_headers ( $ filtered_output );
9931000
9941001 return $ filtered_output ;
9951002}
0 commit comments