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.


git-svn-id: https://develop.svn.wordpress.org/trunk@44624 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Felix Arntz 2019-01-16 15:20:04 +00:00
parent 33f524f9c2
commit 633a444219
2 changed files with 55 additions and 39 deletions

View File

@ -122,12 +122,11 @@ class WP_Shutdown_Handler {
* Displays the PHP error template and sends the HTTP status code, typically 500. * 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 * 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 * print the HTML markup indicating that a PHP error occurred. Note that this drop-in may potentially be executed
* that this drop-in may potentially be executed very early in the WordPress bootstrap process, so any core * very early in the WordPress bootstrap process, so any core functions used that are not part of
* functions used that are not part of `wp-includes/load.php` should be checked for before being called. * `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 * If no such drop-in is available, this will call {@see WP_Shutdown_Handler::display_default_error_template()}.
* mandatory.
* *
* @since 5.1.0 * @since 5.1.0
*/ */
@ -141,54 +140,57 @@ class WP_Shutdown_Handler {
} }
} }
// Otherwise, fail with a `wp_die()` message. // Otherwise, display the default error template.
$message = $this->get_error_message_markup(); $this->display_default_error_template();
// `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 );
} }
/** /**
* 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 * @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( '__' ) ) { if ( ! function_exists( '__' ) ) {
wp_load_translations_early(); wp_load_translations_early();
} }
$message = sprintf( if ( ! function_exists( 'wp_die' ) ) {
'<p>%s</p>', require_once ABSPATH . WPINC . '/functions.php';
__( 'The site is experiencing technical difficulties.' ) }
);
$message = __( 'The site is experiencing technical difficulties.' );
if ( function_exists( 'admin_url' ) ) {
$message .= sprintf( $args = array( 'response' => 500 );
'<hr><p><em>%s <a href="%s">%s</a></em></p>', if ( function_exists( 'admin_url' ) ) {
__( 'Are you the site owner?' ), $args['link_url'] = admin_url();
admin_url(), $args['link_text'] = __( 'Log into the admin backend to fix this.' );
__( '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 * @since 5.1.0
* *
* @param string $message HTML error message to display. * @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 2.0.4
* @since 4.1.0 The `$title` and `$args` parameters were changed to optionally accept * @since 4.1.0 The `$title` and `$args` parameters were changed to optionally accept
* an integer to be used as the response code. * 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, * @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. * 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. * as the response code. Default empty array.
* *
* @type int $response The HTTP response code. Default 200 for Ajax requests, 500 otherwise. * @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 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 * @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'. * 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>"; $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'] ) { if ( isset( $r['back_link'] ) && $r['back_link'] ) {
$back_text = $have_gettext ? __( '&laquo; Back' ) : '&laquo; Back'; $back_text = $have_gettext ? __( '&laquo; Back' ) : '&laquo; Back';
$message .= "\n<p><a href='javascript:history.back()'>$back_text</a></p>"; $message .= "\n<p><a href='javascript:history.back()'>$back_text</a></p>";