diff --git a/src/wp-includes/functions.php b/src/wp-includes/functions.php index d9927baffbcd5..08d7d940d869a 100644 --- a/src/wp-includes/functions.php +++ b/src/wp-includes/functions.php @@ -4023,6 +4023,14 @@ function _default_wp_die_handler( $message, $title = '', $args = array() ) { $message .= "\n
"; } + // Ensure HTTP 500 status code for critical errors. + if ( 'internal_server_error' === $parsed_args['code'] || 500 === $parsed_args['response'] ) { + $parsed_args['response'] = 500; + if ( ! headers_sent() ) { + http_response_code( 500 ); + } + } + if ( ! did_action( 'admin_head' ) ) : if ( ! headers_sent() ) { header( "Content-Type: text/html; charset={$parsed_args['charset']}" ); diff --git a/tests/phpunit/tests/functions/defaultWpDieHandler.php b/tests/phpunit/tests/functions/defaultWpDieHandler.php new file mode 100644 index 0000000000000..1f3669a39df1f --- /dev/null +++ b/tests/phpunit/tests/functions/defaultWpDieHandler.php @@ -0,0 +1,176 @@ +assertSame( 500, $this->get_response_code_from_wp_die( 'Error message' ) ); + } + + /** + * @ticket 64256 + */ + public function test_should_send_500_response_code_when_response_is_500() { + $this->assertSame( 500, $this->get_response_code_from_wp_die( 'Error message', array( 'response' => 500 ) ) ); + } + + /** + * @ticket 64256 + */ + public function test_should_send_500_response_code_for_internal_server_error_code() { + $response_code = $this->get_response_code_from_wp_die( + 'Error message', + array( + 'code' => 'internal_server_error', + 'response' => 403, + ) + ); + + $this->assertSame( 500, $response_code ); + } + + /** + * @ticket 64256 + */ + public function test_should_send_500_response_code_for_internal_server_error_wp_error() { + $error = new WP_Error( 'internal_server_error', 'Error message', array( 'status' => 403 ) ); + + $this->assertSame( 500, $this->get_response_code_from_wp_die( $error ) ); + } + + /** + * Tests that a critical error sends a 500 response code even after the admin header was output. + * + * @ticket 64256 + */ + public function test_should_send_500_response_code_for_critical_error_after_admin_head() { + $this->mark_admin_head_as_done(); + + $error = new WP_Error( 'internal_server_error', 'There has been a critical error on this website.' ); + + $this->assertSame( 500, $this->get_response_code_from_wp_die( $error, array( 'response' => 500 ) ) ); + } + + /** + * Tests that non-critical errors still do not send a response code after the admin header was output. + * + * @ticket 64256 + */ + public function test_should_not_send_response_code_for_non_critical_error_after_admin_head() { + $this->mark_admin_head_as_done(); + + $response_code = $this->get_response_code_from_wp_die( + 'Error message', + array( + 'code' => 'not_found', + 'response' => 404, + ) + ); + + $this->assertFalse( $response_code ); + } + + /** + * Tests the default critical error template of the fatal error handler, after the admin header was output. + * + * @ticket 64256 + * + * @covers WP_Fatal_Error_Handler::display_default_error_template + */ + public function test_fatal_error_handler_default_template_should_send_500_response_code() { + $this->mark_admin_head_as_done(); + + $error = array( + 'type' => E_ERROR, + 'message' => 'Fatal error', + 'file' => __FILE__, + 'line' => __LINE__, + ); + + $method = new ReflectionMethod( 'WP_Fatal_Error_Handler', 'display_default_error_template' ); + if ( PHP_VERSION_ID < 80100 ) { + $method->setAccessible( true ); + } + + ob_start(); + $method->invoke( new WP_Fatal_Error_Handler(), $error, false ); + ob_end_clean(); + + $this->assertSame( 500, http_response_code() ); + } + + /** + * @ticket 64256 + */ + public function test_should_send_custom_response_code_for_non_critical_errors() { + $response_code = $this->get_response_code_from_wp_die( + 'Error message', + array( + 'code' => 'not_found', + 'response' => 404, + ) + ); + + $this->assertSame( 404, $response_code ); + } +}