Skip to content

Commit dd9bbdf

Browse files
committed
Opt to display errors which occur during finalize action
1 parent 2a359df commit dd9bbdf

File tree

2 files changed

+32
-28
lines changed

2 files changed

+32
-28
lines changed

src/wp-includes/template.php

Lines changed: 23 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1031,29 +1031,6 @@ static function ( int $level, string $message, ?string $file = null, ?int $line
10311031
$did_just_catch_exception = false;
10321032
}
10331033

1034-
if ( $display_errors ) {
1035-
foreach ( $error_log as $error ) {
1036-
switch ( $error['level'] ) {
1037-
case E_USER_NOTICE:
1038-
$type = 'Notice';
1039-
break;
1040-
case E_USER_DEPRECATED:
1041-
$type = 'Deprecated';
1042-
break;
1043-
case E_USER_WARNING:
1044-
$type = 'Warning';
1045-
break;
1046-
default:
1047-
$type = 'Error';
1048-
}
1049-
$format = "<br />\n<b>%s</b>: %s in <b>%s</b> on line <b>%d</b><br />";
1050-
if ( ! ini_get( 'html_errors' ) ) {
1051-
$format = strip_tags( $format );
1052-
}
1053-
$filtered_output .= sprintf( $format, $type, $error['message'], $error['file'], $error['line'] );
1054-
}
1055-
}
1056-
10571034
try {
10581035
/**
10591036
* Fires after the template enhancement output buffer has been finalized.
@@ -1074,11 +1051,6 @@ static function ( int $level, string $message, ?string $file = null, ?int $line
10741051
* added to the action must not attempt to start their own output buffers. Otherwise, PHP will raise a fatal
10751052
* error: "Cannot use output buffering in output buffering display handlers."
10761053
*
1077-
* If any errors are occur in callbacks for this action (e.g. deprecations, notices, warnings, exceptions),
1078-
* there will be no error message printed even if `display_errors` is enabled. This is because the output has
1079-
* already been finalized. The error will be emitted to the error log, however, as long as the error reporting
1080-
* level is configured.
1081-
*
10821054
* @since 6.9.0
10831055
*
10841056
* @param string $output Finalized output buffer.
@@ -1098,9 +1070,32 @@ static function ( int $level, string $message, ?string $file = null, ?int $line
10981070
$did_just_catch_exception = false;
10991071
}
11001072

1073+
// Append any errors to be displayed before returning flushing the buffer.
11011074
if ( $display_errors ) {
1075+
foreach ( $error_log as $error ) {
1076+
switch ( $error['level'] ) {
1077+
case E_USER_NOTICE:
1078+
$type = 'Notice';
1079+
break;
1080+
case E_USER_DEPRECATED:
1081+
$type = 'Deprecated';
1082+
break;
1083+
case E_USER_WARNING:
1084+
$type = 'Warning';
1085+
break;
1086+
default:
1087+
$type = 'Error';
1088+
}
1089+
$format = "<br />\n<b>%s</b>: %s in <b>%s</b> on line <b>%d</b><br />";
1090+
if ( ! ini_get( 'html_errors' ) ) {
1091+
$format = strip_tags( $format );
1092+
}
1093+
$filtered_output .= sprintf( $format, $type, $error['message'], $error['file'], $error['line'] );
1094+
}
1095+
11021096
ini_set( 'display_errors', 1 );
11031097
}
1098+
11041099
restore_error_handler();
11051100

11061101
return $filtered_output;

tests/phpunit/tests/template.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1011,6 +1011,7 @@ public function data_provider_to_test_wp_finalize_template_enhancement_output_bu
10111011
),
10121012
'expected_displayed_errors' => array(
10131013
'<b>Deprecated</b>: You are history during filter. in <b>__FILE__</b> on line <b>__LINE__</b>',
1014+
'<b>Deprecated</b>: You are history during action. in <b>__FILE__</b> on line <b>__LINE__</b>',
10141015
),
10151016
),
10161017
'notice' => array(
@@ -1028,6 +1029,7 @@ public function data_provider_to_test_wp_finalize_template_enhancement_output_bu
10281029
),
10291030
'expected_displayed_errors' => array(
10301031
'<b>Notice</b>: POSTED: No trespassing during filter. in <b>__FILE__</b> on line <b>__LINE__</b>',
1032+
'<b>Notice</b>: POSTED: No trespassing during action. in <b>__FILE__</b> on line <b>__LINE__</b>',
10311033
),
10321034
),
10331035
'warning' => array(
@@ -1045,6 +1047,7 @@ public function data_provider_to_test_wp_finalize_template_enhancement_output_bu
10451047
),
10461048
'expected_displayed_errors' => array(
10471049
'<b>Warning</b>: AVISO: Piso mojado durante filtro. in <b>__FILE__</b> on line <b>__LINE__</b>',
1050+
'<b>Warning</b>: AVISO: Piso mojado durante acción. in <b>__FILE__</b> on line <b>__LINE__</b>',
10481051
),
10491052
),
10501053
'error' => array(
@@ -1062,6 +1065,7 @@ public function data_provider_to_test_wp_finalize_template_enhancement_output_bu
10621065
),
10631066
'expected_displayed_errors' => array(
10641067
'<b>Error</b>: Uncaught exception "Exception" thrown: User error triggered: ERROR: Can this mistake be rectified during filter? in <b>__FILE__</b> on line <b>__LINE__</b>',
1068+
'<b>Error</b>: Uncaught exception "Exception" thrown: User error triggered: ERROR: Can this mistake be rectified during action? in <b>__FILE__</b> on line <b>__LINE__</b>',
10651069
),
10661070
),
10671071
'exception' => array(
@@ -1079,6 +1083,7 @@ public function data_provider_to_test_wp_finalize_template_enhancement_output_bu
10791083
),
10801084
'expected_displayed_errors' => array(
10811085
'<b>Error</b>: Uncaught exception "Exception" thrown: I take exception to this filter! in <b>__FILE__</b> on line <b>__LINE__</b>',
1086+
'<b>Error</b>: Uncaught exception "Exception" thrown: I take exception to this action! in <b>__FILE__</b> on line <b>__LINE__</b>',
10821087
),
10831088
),
10841089
'multiple_non_errors' => array(
@@ -1106,6 +1111,9 @@ public function data_provider_to_test_wp_finalize_template_enhancement_output_bu
11061111
'<b>Deprecated</b>: You are history during filter. in <b>__FILE__</b> on line <b>__LINE__</b>',
11071112
'<b>Notice</b>: POSTED: No trespassing during filter. in <b>__FILE__</b> on line <b>__LINE__</b>',
11081113
'<b>Warning</b>: AVISO: Piso mojado durante filtro. in <b>__FILE__</b> on line <b>__LINE__</b>',
1114+
'<b>Deprecated</b>: You are history during action. in <b>__FILE__</b> on line <b>__LINE__</b>',
1115+
'<b>Notice</b>: POSTED: No trespassing during action. in <b>__FILE__</b> on line <b>__LINE__</b>',
1116+
'<b>Warning</b>: AVISO: Piso mojado durante acción. in <b>__FILE__</b> on line <b>__LINE__</b>',
11091117
),
11101118
),
11111119
'deprecated_without_html' => array(
@@ -1142,6 +1150,7 @@ public function data_provider_to_test_wp_finalize_template_enhancement_output_bu
11421150
),
11431151
'expected_displayed_errors' => array(
11441152
'<b>Warning</b>: AVISO: Piso mojado durante filtro. in <b>__FILE__ : eval()\'d code</b> on line <b>__LINE__</b>',
1153+
'<b>Warning</b>: AVISO: Piso mojado durante acción. in <b>__FILE__ : eval()\'d code</b> on line <b>__LINE__</b>',
11451154
),
11461155
),
11471156
);

0 commit comments

Comments
 (0)