Bootstrap/Load: Fix workaround to display admin link in PHP error template by introducing `$link_url` and `$link_text` arguments to `wp_die()`.

This changeset removes the hack that was used before to display more complex HTML markup than a simple message in the default PHP error template via `wp_die()`. By removing HTML markup from the arguments passed to `wp_die()` it furthermore paves the way for supporting other content types than the default.

The message and arguments can be modified with new `wp_php_error_message` and `wp_php_error_args` filters respectively.

Furthermore this changeset fixes a few issues of functions not existing which could potentially have caused fatal errors when executed early in the WordPress bootstrap process.

Props flixos90, spacedmonkey.
See #45933, #44458.

Built from https://develop.svn.wordpress.org/trunk@44624


git-svn-id: http://core.svn.wordpress.org/trunk@44455 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Felix Arntz 2019-01-16 15:21:50 +00:00
parent 225f3055b8
commit 30d5ca9172
3 changed files with 56 additions and 40 deletions

View File

@ -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 ) === '<p>' && substr( $message, -4 ) === '</p>' ) {
$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(
'<p>%s</p>',
__( 'The site is experiencing technical difficulties.' )
);
if ( function_exists( 'admin_url' ) ) {
$message .= sprintf(
'<hr><p><em>%s <a href="%s">%s</a></em></p>',
__( 'Are you the site owner?' ),
admin_url(),
__( 'Log into the admin backend to fix this.' )
);
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' ) ) {
$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.
* 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_technical_issues_display', $message );
}
$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 );
}
}

View File

@ -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 = "<p>$message</p>";
}
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<p><a href='{$link_url}'>{$link_text}</a></p>";
}
if ( isset( $r['back_link'] ) && $r['back_link'] ) {
$back_text = $have_gettext ? __( '&laquo; Back' ) : '&laquo; Back';
$message .= "\n<p><a href='javascript:history.back()'>$back_text</a></p>";

View File

@ -13,7 +13,7 @@
*
* @global string $wp_version
*/
$wp_version = '5.1-beta1-44623';
$wp_version = '5.1-beta1-44624';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.