Bootstrap/Load: Introduce fatal error recovery mechanism allowing users to still log in to their admin dashboard.

This changeset introduces a `WP_Shutdown_Handler` class that detects fatal errors and which extension (plugin or theme) causes them. Such an error is then recorded, and an error message is displayed. Subsequently, in certain protected areas, for example the admin, the broken extension will be paused, ensuring that the website is still usable in the respective area. The major benefit is that this mechanism allows site owners to still log in to their website, to fix the problem by either disabling the extension or solving the bug and then resuming the extension.

Extensions are only paused in certain designated areas. The frontend for example stays unaffected, as it is impossible to know what pausing the extension would cause to be missing, so it might be preferrable to clearly see that the website is temporarily not accessible instead.

The fatal error recovery is especially important in scope of encouraging the switch to a maintained PHP version, as not necessarily every WordPress extension is compatible with all PHP versions. If problems occur now, non-technical site owners that do not have immediate access to the codebase are not locked out of their site and can at least temporarily solve the problem quickly.

Websites that have custom requirements in that regard can implement their own shutdown handler by adding a `shutdown-handler.php` drop-in that returns the handler instance to use, which must be based on a class that inherits `WP_Shutdown_Handler`. That handler will then be used in place of the default one.

Websites that would like to modify specifically the error template displayed in the frontend can add a `php-error.php` drop-in that works similarly to the existing `db-error.php` drop-in.

Props afragen, bradleyt, flixos90, ocean90, schlessera, SergeyBiryukov, spacedmonkey.
Fixes #44458.


git-svn-id: https://develop.svn.wordpress.org/trunk@44524 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Felix Arntz
2019-01-09 20:04:55 +00:00
parent 493b90cde1
commit fc37b1746e
16 changed files with 1310 additions and 30 deletions

View File

@@ -763,3 +763,127 @@ function customize_themes_print_templates() {
</script>
<?php
}
/**
* Determines whether a theme is technically active but was paused while
* loading.
*
* For more information on this and similar theme functions, check out
* the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
* Conditional Tags} article in the Theme Developer Handbook.
*
* @since 5.1.0
*
* @param string $theme Path to the theme directory relative to the themes directory.
* @return bool True, if in the list of paused themes. False, not in the list.
*/
function is_theme_paused( $theme ) {
if ( ! isset( $GLOBALS['_paused_themes'] ) ) {
return false;
}
if ( $theme !== get_stylesheet() && $theme !== get_template() ) {
return false;
}
return array_key_exists( $theme, $GLOBALS['_paused_themes'] );
}
/**
* Gets the error that was recorded for a paused theme.
*
* @since 5.1.0
*
* @param string $theme Path to the theme directory relative to the themes
* directory.
* @return array|false Array of error information as it was returned by
* `error_get_last()`, or false if none was recorded.
*/
function wp_get_theme_error( $theme ) {
if ( ! isset( $GLOBALS['_paused_themes'] ) ) {
return false;
}
if ( ! array_key_exists( $theme, $GLOBALS['_paused_themes'] ) ) {
return false;
}
return $GLOBALS['_paused_themes'][ $theme ];
}
/**
* Gets the number of sites on which a specific theme is paused.
*
* @since 5.1.0
*
* @param string $theme Path to the theme directory relative to the themes directory.
* @return int Site count.
*/
function count_paused_theme_sites_for_network( $theme ) {
if ( ! is_multisite() ) {
return is_theme_paused( $theme ) ? 1 : 0;
}
$query_args = array(
'count' => true,
'number' => 0,
'network_id' => get_current_network_id(),
'meta_query' => array(
wp_paused_themes()->get_site_meta_query_clause( $theme ),
),
);
return get_sites( $query_args );
}
/**
* Tries to resume a single theme.
*
* @since 5.1.0
*
* @param string $theme Single theme to resume.
* @return bool|WP_Error True on success, false if `$theme` was not paused,
* `WP_Error` on failure.
*/
function resume_theme( $theme ) {
$result = wp_forget_extension_error( 'themes', $theme );
if ( ! $result ) {
return new WP_Error(
'could_not_resume_theme',
__( 'Could not resume the theme.' )
);
}
return true;
}
/**
* Renders an admin notice in case some themes have been paused due to errors.
*
* @since 5.1.0
*/
function paused_themes_notice() {
if ( 'themes.php' === $GLOBALS['pagenow'] ) {
return;
}
if ( ! current_user_can( 'switch_themes' ) ) {
return;
}
if ( ! isset( $GLOBALS['_paused_themes'] ) || empty( $GLOBALS['_paused_themes'] ) ) {
return;
}
printf(
'<div class="notice notice-error"><p><strong>%s</strong><br>%s</p><p>%s</p></div>',
__( 'One or more themes failed to load properly.' ),
__( 'You can find more details and make changes on the Themes screen.' ),
sprintf(
'<a href="%s">%s</a>',
admin_url( 'themes.php' ),
'Go to the Themes screen'
)
);
}