@@ -965,7 +965,6 @@ function wp_finalize_template_enhancement_output_buffer( string $output, int $ph
965965
966966 $ filtered_output = $ output ;
967967
968- // TODO: Also capture exceptions?
969968 $ error_log = array ();
970969 $ display_errors = ini_get ( 'display_errors ' );
971970 if ( $ display_errors ) {
@@ -980,22 +979,41 @@ static function ( int $level, string $message, ?string $file = null, ?int $line
980979 );
981980 }
982981
983- /**
984- * Filters the template enhancement output buffer prior to sending to the client.
985- *
986- * This filter only applies the HTML output of an included template. This filter is a progressive enhancement
987- * intended for applications such as optimizing markup to improve frontend page load performance. Sites must not
988- * depend on this filter applying since they may opt to stream the responses instead. Callbacks for this filter are
989- * highly discouraged from using regular expressions to do any kind of replacement on the output. Use the HTML API
990- * (either `WP_HTML_Tag_Processor` or `WP_HTML_Processor`), or else use {@see DOM\HtmlDocument} as of PHP 8.4 which
991- * fully supports HTML5.
992- *
993- * @since 6.9.0
994- *
995- * @param string $filtered_output HTML template enhancement output buffer.
996- * @param string $output Original HTML template output buffer.
997- */
998- $ filtered_output = (string ) apply_filters ( 'wp_template_enhancement_output_buffer ' , $ filtered_output , $ output );
982+ try {
983+ /**
984+ * Filters the template enhancement output buffer prior to sending to the client.
985+ *
986+ * This filter only applies the HTML output of an included template. This filter is a progressive enhancement
987+ * intended for applications such as optimizing markup to improve frontend page load performance. Sites must not
988+ * depend on this filter applying since they may opt to stream the responses instead. Callbacks for this filter are
989+ * highly discouraged from using regular expressions to do any kind of replacement on the output. Use the HTML API
990+ * (either `WP_HTML_Tag_Processor` or `WP_HTML_Processor`), or else use {@see DOM\HtmlDocument} as of PHP 8.4 which
991+ * fully supports HTML5.
992+ *
993+ * @since 6.9.0
994+ *
995+ * @param string $filtered_output HTML template enhancement output buffer.
996+ * @param string $output Original HTML template output buffer.
997+ */
998+ $ filtered_output = (string ) apply_filters ( 'wp_template_enhancement_output_buffer ' , $ filtered_output , $ output );
999+ } catch ( Exception $ exception ) {
1000+ $ error_log [] = array (
1001+ 'level ' => E_USER_ERROR ,
1002+ 'message ' => $ exception ->getMessage (),
1003+ 'file ' => $ exception ->getFile (),
1004+ 'line ' => $ exception ->getLine (),
1005+ );
1006+
1007+ // Emit to the error log.
1008+ trigger_error (
1009+ sprintf (
1010+ /* translators: %s is wp_template_enhancement_output_buffer */
1011+ __ ( 'Exception thrown during %s filter: ' ) . $ exception ->getMessage (),
1012+ 'wp_template_enhancement_output_buffer '
1013+ ),
1014+ E_USER_WARNING
1015+ );
1016+ }
9991017
10001018 if ( $ display_errors ) {
10011019 foreach ( $ error_log as $ error ) {
@@ -1025,23 +1043,37 @@ static function ( int $level, string $message, ?string $file = null, ?int $line
10251043 }
10261044 }
10271045
1028- /**
1029- * Fires at the last moment HTTP headers may be sent.
1030- *
1031- * This happens immediately before the template enhancement output buffer is flushed. This is in contrast with
1032- * the {@see 'send_headers'} action which fires after the initial headers have been sent before the template
1033- * has begun rendering, and thus does not depend on output buffering. This action does not fire if the "template
1034- * enhancement output buffer" was not started. This output buffer is automatically started if this action is added
1035- * before {@see wp_start_template_enhancement_output_buffer()} runs at the {@see 'wp_before_include_template'}
1036- * action with priority 1000. Before this point, the output buffer will also be started automatically if there was a
1037- * {@see 'wp_template_enhancement_output_buffer'} filter added, or if the
1038- * {@see 'wp_should_output_buffer_template_for_enhancement'} filter is made to return `true`.
1039- *
1040- * @since 6.9.0
1041- *
1042- * @param string $output Output buffer.
1043- */
1044- do_action ( 'wp_send_late_headers ' , $ filtered_output );
1046+ try {
1047+ /**
1048+ * Fires at the last moment HTTP headers may be sent.
1049+ *
1050+ * This happens immediately before the template enhancement output buffer is flushed. This is in contrast with
1051+ * the {@see 'send_headers'} action which fires after the initial headers have been sent before the template
1052+ * has begun rendering, and thus does not depend on output buffering. This action does not fire if the "template
1053+ * enhancement output buffer" was not started. This output buffer is automatically started if this action is added
1054+ * before {@see wp_start_template_enhancement_output_buffer()} runs at the {@see 'wp_before_include_template'}
1055+ * action with priority 1000. Before this point, the output buffer will also be started automatically if there was a
1056+ * {@see 'wp_template_enhancement_output_buffer'} filter added, or if the
1057+ * {@see 'wp_should_output_buffer_template_for_enhancement'} filter is made to return `true`.
1058+ *
1059+ * @since 6.9.0
1060+ *
1061+ * @param string $output Output buffer.
1062+ */
1063+ do_action ( 'wp_send_late_headers ' , $ filtered_output );
1064+ } catch ( Exception $ exception ) {
1065+ // Emit to the error log.
1066+ trigger_error (
1067+ sprintf (
1068+ /* translators: %s is wp_send_late_headers */
1069+ __ ( 'Exception thrown during %s action: ' ) . $ exception ->getMessage (),
1070+ 'wp_send_late_headers '
1071+ ),
1072+ E_USER_WARNING
1073+ );
1074+
1075+ // TODO: Should this also append the error to $filtered output if $display_errors? But it could make a sent header incorrect.
1076+ }
10451077
10461078 if ( $ display_errors ) {
10471079 restore_error_handler ();
0 commit comments