Skip to content

Commit 2fe5542

Browse files
committed
Improve handling of html_errors and add support for error_prepend_string/error_append_string
1 parent dd9bbdf commit 2fe5542

File tree

2 files changed

+38
-25
lines changed

2 files changed

+38
-25
lines changed

src/wp-includes/template.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1086,11 +1086,18 @@ static function ( int $level, string $message, ?string $file = null, ?int $line
10861086
default:
10871087
$type = 'Error';
10881088
}
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 );
1089+
1090+
if ( ini_get( 'html_errors' ) ) {
1091+
/*
1092+
* Adapted from PHP internals: <https://github.com/php/php-src/blob/a979e9f897a90a580e883b1f39ce5673686ffc67/main/main.c#L1478>.
1093+
* The self-closing tags are a vestige of the XHTML past!
1094+
*/
1095+
$format = "%s<br />\n<b>%s</b>: %s in <b>%s</b> on line <b>%s</b><br />\n%s";
1096+
} else {
1097+
// Adapted from PHP internals: <https://github.com/php/php-src/blob/a979e9f897a90a580e883b1f39ce5673686ffc67/main/main.c#L1492>.
1098+
$format = "%s\n%s: %s in %s on line %s\n%s";
10921099
}
1093-
$filtered_output .= sprintf( $format, $type, $error['message'], $error['file'], $error['line'] );
1100+
$filtered_output .= sprintf( $format, ini_get( 'error_prepend_string' ), $type, $error['message'], $error['file'], $error['line'], ini_get( 'error_append_string' ) );
10941101
}
10951102

10961103
ini_set( 'display_errors', 1 );

tests/phpunit/tests/template.php

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ public function set_up() {
117117
wp_styles();
118118

119119
$this->original_theme_features = $GLOBALS['_wp_theme_features'];
120-
foreach ( array( 'display_errors', 'error_reporting', 'log_errors', 'error_log', 'default_mimetype', 'html_errors' ) as $config ) {
120+
foreach ( array( 'display_errors', 'error_reporting', 'log_errors', 'error_log', 'default_mimetype', 'html_errors', 'error_prepend_string', 'error_append_string' ) as $config ) {
121121
$this->original_ini_config[ $config ] = ini_get( $config );
122122
}
123123
}
@@ -1010,8 +1010,8 @@ public function data_provider_to_test_wp_finalize_template_enhancement_output_bu
10101010
'PHP Deprecated: You are history during action. in __FILE__ on line __LINE__',
10111011
),
10121012
'expected_displayed_errors' => array(
1013-
'<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>',
1013+
'<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>',
10151015
),
10161016
),
10171017
'notice' => array(
@@ -1028,8 +1028,8 @@ public function data_provider_to_test_wp_finalize_template_enhancement_output_bu
10281028
'PHP Notice: POSTED: No trespassing during action. in __FILE__ on line __LINE__',
10291029
),
10301030
'expected_displayed_errors' => array(
1031-
'<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>',
1031+
'<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>',
10331033
),
10341034
),
10351035
'warning' => array(
@@ -1046,8 +1046,8 @@ public function data_provider_to_test_wp_finalize_template_enhancement_output_bu
10461046
'PHP Warning: AVISO: Piso mojado durante acción. in __FILE__ on line __LINE__',
10471047
),
10481048
'expected_displayed_errors' => array(
1049-
'<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>',
1049+
'<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>',
10511051
),
10521052
),
10531053
'error' => array(
@@ -1064,8 +1064,8 @@ public function data_provider_to_test_wp_finalize_template_enhancement_output_bu
10641064
'PHP Warning: Uncaught exception "Exception" thrown: User error triggered: ERROR: Can this mistake be rectified during action? in __FILE__ on line __LINE__',
10651065
),
10661066
'expected_displayed_errors' => array(
1067-
'<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>',
1067+
'<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>',
10691069
),
10701070
),
10711071
'exception' => array(
@@ -1082,8 +1082,8 @@ public function data_provider_to_test_wp_finalize_template_enhancement_output_bu
10821082
'PHP Warning: Uncaught exception "Exception" thrown: I take exception to this action! in __FILE__ on line __LINE__',
10831083
),
10841084
'expected_displayed_errors' => array(
1085-
'<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>',
1085+
'<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>',
10871087
),
10881088
),
10891089
'multiple_non_errors' => array(
@@ -1108,12 +1108,12 @@ public function data_provider_to_test_wp_finalize_template_enhancement_output_bu
11081108
'PHP Warning: AVISO: Piso mojado durante acción. in __FILE__ on line __LINE__',
11091109
),
11101110
'expected_displayed_errors' => array(
1111-
'<b>Deprecated</b>: You are history during filter. in <b>__FILE__</b> on line <b>__LINE__</b>',
1112-
'<b>Notice</b>: POSTED: No trespassing during filter. in <b>__FILE__</b> on line <b>__LINE__</b>',
1113-
'<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>',
1111+
'<b>Deprecated</b>: You are history during filter. in <b>__FILE__</b> on line <b>__LINE__</b>',
1112+
'<b>Notice</b>: POSTED: No trespassing during filter. in <b>__FILE__</b> on line <b>__LINE__</b>',
1113+
'<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>',
11171117
),
11181118
),
11191119
'deprecated_without_html' => array(
@@ -1135,8 +1135,14 @@ public function data_provider_to_test_wp_finalize_template_enhancement_output_bu
11351135
'Deprecated: You are history during filter. in __FILE__ on line __LINE__',
11361136
),
11371137
),
1138-
'warning_in_eval' => array(
1139-
'ini_config_options' => $log_and_display_all,
1138+
'warning_in_eval_with_prepend_and_append' => array(
1139+
'ini_config_options' => array_merge(
1140+
$log_and_display_all,
1141+
array(
1142+
'error_prepend_string' => '<details><summary>PHP Problem!</summary>',
1143+
'error_append_string' => '</details>',
1144+
)
1145+
),
11401146
'emit_filter_errors' => static function () {
11411147
eval( "trigger_error( 'AVISO: Piso mojado durante filtro.', E_USER_WARNING );" ); // phpcs:ignore Squiz.PHP.Eval.Discouraged -- We're in a test!
11421148
},
@@ -1149,8 +1155,8 @@ public function data_provider_to_test_wp_finalize_template_enhancement_output_bu
11491155
'PHP Warning: AVISO: Piso mojado durante acción. in __FILE__ : eval()\'d code on line __LINE__',
11501156
),
11511157
'expected_displayed_errors' => array(
1152-
'<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>',
1158+
'<b>Warning</b>: AVISO: Piso mojado durante filtro. in <b>__FILE__ : eval()\'d code</b> on line <b>__LINE__</b>',
1159+
'<b>Warning</b>: AVISO: Piso mojado durante acción. in <b>__FILE__ : eval()\'d code</b> on line <b>__LINE__</b>',
11541160
),
11551161
),
11561162
);

0 commit comments

Comments
 (0)