WordPress.org

Make WordPress Core

Changeset 44624


Ignore:
Timestamp:
01/16/2019 03:20:04 PM (5 months ago)
Author:
flixos90
Message:

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.

Location:
trunk/src/wp-includes
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-shutdown-handler.php

    r44623 r44624  
    123123     *
    124124     * A drop-in 'php-error.php' can be used as a custom template. This drop-in should control the HTTP status code and
    125      * print the HTML markup indicating that a PHP error occurred. Alternatively, {@see wp_die()} can be used. Note
    126      * that this drop-in may potentially be executed very early in the WordPress bootstrap process, so any core
    127      * functions used that are not part of `wp-includes/load.php` should be checked for before being called.
     125     * print the HTML markup indicating that a PHP error occurred. Note that this drop-in may potentially be executed
     126     * very early in the WordPress bootstrap process, so any core functions used that are not part of
     127     * `wp-includes/load.php` should be checked for before being called.
    128128     *
    129      * The default template also displays a link to the admin in order to fix the problem, however doing so is not
    130      * mandatory.
     129     * If no such drop-in is available, this will call {@see WP_Shutdown_Handler::display_default_error_template()}.
    131130     *
    132131     * @since 5.1.0
     
    142141        }
    143142
    144         // Otherwise, fail with a `wp_die()` message.
    145         $message = $this->get_error_message_markup();
    146 
    147         // `wp_die()` wraps the message in paragraph tags, so let's just try working around that.
    148         if ( substr( $message, 0, 3 ) === '<p>' && substr( $message, -4 ) === '</p>' ) {
    149             $message = substr( $message, 3, -4 );
    150         }
    151 
    152         wp_die( $message, '', 500 );
     143        // Otherwise, display the default error template.
     144        $this->display_default_error_template();
    153145    }
    154146
    155147    /**
    156      * Returns the error message markup to display in the default error template.
     148     * Displays the default PHP error template.
     149     *
     150     * This method is called conditionally if no 'php-error.php' drop-in is available.
     151     *
     152     * It calls {@see wp_die()} with a message indicating that the site is experiencing technical difficulties and a
     153     * login link to the admin backend. The {@see 'wp_php_error_message'} and {@see 'wp_php_error_args'} filters can
     154     * be used to modify these parameters.
    157155     *
    158156     * @since 5.1.0
    159      *
    160      * @return string Error message HTML output.
    161157     */
    162     protected function get_error_message_markup() {
     158    protected function display_default_error_template() {
    163159        if ( ! function_exists( '__' ) ) {
    164160            wp_load_translations_early();
    165161        }
    166162
    167         $message = sprintf(
    168             '<p>%s</p>',
    169             __( 'The site is experiencing technical difficulties.' )
    170         );
    171 
    172         if ( function_exists( 'admin_url' ) ) {
    173             $message .= sprintf(
    174                 '<hr><p><em>%s <a href="%s">%s</a></em></p>',
    175                 __( 'Are you the site owner?' ),
    176                 admin_url(),
    177                 __( 'Log into the admin backend to fix this.' )
    178             );
     163        if ( ! function_exists( 'wp_die' ) ) {
     164            require_once ABSPATH . WPINC . '/functions.php';
    179165        }
    180166
    181         if ( function_exists( 'apply_filters' ) ) {
    182             /**
    183              * Filters the message that the default PHP error page displays.
    184              *
    185              * @since 5.1.0
    186              *
    187              * @param string $message HTML error message to display.
    188              */
    189             $message = apply_filters( 'wp_technical_issues_display', $message );
     167        $message = __( 'The site is experiencing technical difficulties.' );
     168
     169        $args = array( 'response' => 500 );
     170        if ( function_exists( 'admin_url' ) ) {
     171            $args['link_url']  = admin_url();
     172            $args['link_text'] = __( 'Log into the admin backend to fix this.' );
    190173        }
    191174
    192         return $message;
     175        /**
     176         * Filters the message that the default PHP error template displays.
     177         *
     178         * @since 5.1.0
     179         *
     180         * @param string $message HTML error message to display.
     181         */
     182        $message = apply_filters( 'wp_php_error_message', $message );
     183
     184        /**
     185         * Filters the arguments passed to {@see wp_die()} for the default PHP error template.
     186         *
     187         * @since 5.1.0
     188         *
     189         * @param array $args Associative array of arguments passed to `wp_die()`. By default these contain a
     190         *                    'response' key, and optionally 'link_url' and 'link_text' keys.
     191         */
     192        $args = apply_filters( 'wp_php_error_args', $args );
     193
     194        wp_die( $message, '', $args );
    193195    }
    194196}
  • trunk/src/wp-includes/functions.php

    r44590 r44624  
    29342934 * @since 4.1.0 The `$title` and `$args` parameters were changed to optionally accept
    29352935 *              an integer to be used as the response code.
     2936 * @since 5.1.0 The `$link_url` and `$link_text` arguments were added.
    29362937 *
    29372938 * @param string|WP_Error  $message Optional. Error message. If this is a WP_Error object,
     
    29472948 *
    29482949 *     @type int    $response       The HTTP response code. Default 200 for Ajax requests, 500 otherwise.
     2950 *     @type string $link_url       A URL to include a link to. Only works in combination with $link_text.
     2951 *                                  Default empty string.
     2952 *     @type string $link_text      A label for the link to include. Only works in combination with $link_url.
     2953 *                                  Default empty string.
    29492954 *     @type bool   $back_link      Whether to include a link to go back. Default false.
    29502955 *     @type string $text_direction The text direction. This is only useful internally, when WordPress
     
    30343039    } elseif ( is_string( $message ) ) {
    30353040        $message = "<p>$message</p>";
     3041    }
     3042
     3043    if ( ! empty( $r['link_url'] ) && ! empty( $r['link_text'] ) ) {
     3044        $link_url = $r['link_url'];
     3045        if ( function_exists( 'esc_url' ) ) {
     3046            $link_url = esc_url( $link_url );
     3047        }
     3048        $link_text = $r['link_text'];
     3049        $message  .= "\n<p><a href='{$link_url}'>{$link_text}</a></p>";
    30363050    }
    30373051
Note: See TracChangeset for help on using the changeset viewer.