diff --git a/src/wp-includes/class-wp-shutdown-handler.php b/src/wp-includes/class-wp-shutdown-handler.php index dc887a5cd4..c55901dd4a 100644 --- a/src/wp-includes/class-wp-shutdown-handler.php +++ b/src/wp-includes/class-wp-shutdown-handler.php @@ -122,12 +122,11 @@ class WP_Shutdown_Handler { * Displays the PHP error template and sends the HTTP status code, typically 500. * * A drop-in 'php-error.php' can be used as a custom template. This drop-in should control the HTTP status code and - * print the HTML markup indicating that a PHP error occurred. Alternatively, {@see wp_die()} can be used. Note - * that this drop-in may potentially be executed very early in the WordPress bootstrap process, so any core - * functions used that are not part of `wp-includes/load.php` should be checked for before being called. + * print the HTML markup indicating that a PHP error occurred. Note that this drop-in may potentially be executed + * very early in the WordPress bootstrap process, so any core functions used that are not part of + * `wp-includes/load.php` should be checked for before being called. * - * The default template also displays a link to the admin in order to fix the problem, however doing so is not - * mandatory. + * If no such drop-in is available, this will call {@see WP_Shutdown_Handler::display_default_error_template()}. * * @since 5.1.0 */ @@ -141,54 +140,57 @@ class WP_Shutdown_Handler { } } - // Otherwise, fail with a `wp_die()` message. - $message = $this->get_error_message_markup(); - - // `wp_die()` wraps the message in paragraph tags, so let's just try working around that. - if ( substr( $message, 0, 3 ) === '
' && substr( $message, -4 ) === '
' ) { - $message = substr( $message, 3, -4 ); - } - - wp_die( $message, '', 500 ); + // Otherwise, display the default error template. + $this->display_default_error_template(); } /** - * Returns the error message markup to display in the default error template. + * Displays the default PHP error template. + * + * This method is called conditionally if no 'php-error.php' drop-in is available. + * + * It calls {@see wp_die()} with a message indicating that the site is experiencing technical difficulties and a + * login link to the admin backend. The {@see 'wp_php_error_message'} and {@see 'wp_php_error_args'} filters can + * be used to modify these parameters. * * @since 5.1.0 - * - * @return string Error message HTML output. */ - protected function get_error_message_markup() { + protected function display_default_error_template() { if ( ! function_exists( '__' ) ) { wp_load_translations_early(); } - $message = sprintf( - '%s
', - __( 'The site is experiencing technical difficulties.' ) - ); + if ( ! function_exists( 'wp_die' ) ) { + require_once ABSPATH . WPINC . '/functions.php'; + } + $message = __( 'The site is experiencing technical difficulties.' ); + + $args = array( 'response' => 500 ); if ( function_exists( 'admin_url' ) ) { - $message .= sprintf( - '%s %s
', - __( 'Are you the site owner?' ), - admin_url(), - __( 'Log into the admin backend to fix this.' ) - ); + $args['link_url'] = admin_url(); + $args['link_text'] = __( 'Log into the admin backend to fix this.' ); } - if ( function_exists( 'apply_filters' ) ) { - /** - * Filters the message that the default PHP error page displays. - * - * @since 5.1.0 - * - * @param string $message HTML error message to display. - */ - $message = apply_filters( 'wp_technical_issues_display', $message ); - } + /** + * Filters the message that the default PHP error template displays. + * + * @since 5.1.0 + * + * @param string $message HTML error message to display. + */ + $message = apply_filters( 'wp_php_error_message', $message ); - return $message; + /** + * Filters the arguments passed to {@see wp_die()} for the default PHP error template. + * + * @since 5.1.0 + * + * @param array $args Associative array of arguments passed to `wp_die()`. By default these contain a + * 'response' key, and optionally 'link_url' and 'link_text' keys. + */ + $args = apply_filters( 'wp_php_error_args', $args ); + + wp_die( $message, '', $args ); } } diff --git a/src/wp-includes/functions.php b/src/wp-includes/functions.php index 43fa628b89..36b59ac8e1 100644 --- a/src/wp-includes/functions.php +++ b/src/wp-includes/functions.php @@ -2933,6 +2933,7 @@ function wp_nonce_ays( $action ) { * @since 2.0.4 * @since 4.1.0 The `$title` and `$args` parameters were changed to optionally accept * an integer to be used as the response code. + * @since 5.1.0 The `$link_url` and `$link_text` arguments were added. * * @param string|WP_Error $message Optional. Error message. If this is a WP_Error object, * and not an Ajax or XML-RPC request, the error's messages are used. @@ -2946,6 +2947,10 @@ function wp_nonce_ays( $action ) { * as the response code. Default empty array. * * @type int $response The HTTP response code. Default 200 for Ajax requests, 500 otherwise. + * @type string $link_url A URL to include a link to. Only works in combination with $link_text. + * Default empty string. + * @type string $link_text A label for the link to include. Only works in combination with $link_url. + * Default empty string. * @type bool $back_link Whether to include a link to go back. Default false. * @type string $text_direction The text direction. This is only useful internally, when WordPress * is still loading and the site's locale is not set up yet. Accepts 'rtl'. @@ -3035,6 +3040,15 @@ function _default_wp_die_handler( $message, $title = '', $args = array() ) { $message = "$message
"; } + if ( ! empty( $r['link_url'] ) && ! empty( $r['link_text'] ) ) { + $link_url = $r['link_url']; + if ( function_exists( 'esc_url' ) ) { + $link_url = esc_url( $link_url ); + } + $link_text = $r['link_text']; + $message .= "\n"; + } + if ( isset( $r['back_link'] ) && $r['back_link'] ) { $back_text = $have_gettext ? __( '« Back' ) : '« Back'; $message .= "\n";