class-wp-fatal-error-handler.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. <?php
  2. /**
  3. * Error Protection API: WP_Fatal_Error_Handler class
  4. *
  5. * @package WordPress
  6. * @since 5.2.0
  7. */
  8. /**
  9. * Core class used as the default shutdown handler for fatal errors.
  10. *
  11. * A drop-in 'fatal-error-handler.php' can be used to override the instance of this class and use a custom
  12. * implementation for the fatal error handler that WordPress registers. The custom class should extend this class and
  13. * can override its methods individually as necessary. The file must return the instance of the class that should be
  14. * registered.
  15. *
  16. * @since 5.2.0
  17. */
  18. class WP_Fatal_Error_Handler {
  19. /**
  20. * Runs the shutdown handler.
  21. *
  22. * This method is registered via `register_shutdown_function()`.
  23. *
  24. * @since 5.2.0
  25. */
  26. public function handle() {
  27. if ( defined( 'WP_SANDBOX_SCRAPING' ) && WP_SANDBOX_SCRAPING ) {
  28. return;
  29. }
  30. try {
  31. // Bail if no error found.
  32. $error = $this->detect_error();
  33. if ( ! $error ) {
  34. return;
  35. }
  36. if ( ! isset( $GLOBALS['wp_locale'] ) && function_exists( 'load_default_textdomain' ) ) {
  37. load_default_textdomain();
  38. }
  39. $handled = false;
  40. if ( ! is_multisite() && wp_recovery_mode()->is_initialized() ) {
  41. $handled = wp_recovery_mode()->handle_error( $error );
  42. }
  43. // Display the PHP error template if headers not sent.
  44. if ( is_admin() || ! headers_sent() ) {
  45. $this->display_error_template( $error, $handled );
  46. }
  47. } catch ( Exception $e ) {
  48. // Catch exceptions and remain silent.
  49. }
  50. }
  51. /**
  52. * Detects the error causing the crash if it should be handled.
  53. *
  54. * @since 5.2.0
  55. *
  56. * @return array|null Error that was triggered, or null if no error received or if the error should not be handled.
  57. */
  58. protected function detect_error() {
  59. $error = error_get_last();
  60. // No error, just skip the error handling code.
  61. if ( null === $error ) {
  62. return null;
  63. }
  64. // Bail if this error should not be handled.
  65. if ( ! $this->should_handle_error( $error ) ) {
  66. return null;
  67. }
  68. return $error;
  69. }
  70. /**
  71. * Determines whether we are dealing with an error that WordPress should handle
  72. * in order to protect the admin backend against WSODs.
  73. *
  74. * @since 5.2.0
  75. *
  76. * @param array $error Error information retrieved from error_get_last().
  77. * @return bool Whether WordPress should handle this error.
  78. */
  79. protected function should_handle_error( $error ) {
  80. $error_types_to_handle = array(
  81. E_ERROR,
  82. E_PARSE,
  83. E_USER_ERROR,
  84. E_COMPILE_ERROR,
  85. E_RECOVERABLE_ERROR,
  86. );
  87. if ( isset( $error['type'] ) && in_array( $error['type'], $error_types_to_handle, true ) ) {
  88. return true;
  89. }
  90. /**
  91. * Filters whether a given thrown error should be handled by the fatal error handler.
  92. *
  93. * This filter is only fired if the error is not already configured to be handled by WordPress core. As such,
  94. * it exclusively allows adding further rules for which errors should be handled, but not removing existing
  95. * ones.
  96. *
  97. * @since 5.2.0
  98. *
  99. * @param bool $should_handle_error Whether the error should be handled by the fatal error handler.
  100. * @param array $error Error information retrieved from error_get_last().
  101. */
  102. return (bool) apply_filters( 'wp_should_handle_php_error', false, $error );
  103. }
  104. /**
  105. * Displays the PHP error template and sends the HTTP status code, typically 500.
  106. *
  107. * A drop-in 'php-error.php' can be used as a custom template. This drop-in should control the HTTP status code and
  108. * print the HTML markup indicating that a PHP error occurred. Note that this drop-in may potentially be executed
  109. * very early in the WordPress bootstrap process, so any core functions used that are not part of
  110. * `wp-includes/load.php` should be checked for before being called.
  111. *
  112. * If no such drop-in is available, this will call {@see WP_Fatal_Error_Handler::display_default_error_template()}.
  113. *
  114. * @since 5.2.0
  115. * @since 5.3.0 The `$handled` parameter was added.
  116. *
  117. * @param array $error Error information retrieved from `error_get_last()`.
  118. * @param true|WP_Error $handled Whether Recovery Mode handled the fatal error.
  119. */
  120. protected function display_error_template( $error, $handled ) {
  121. if ( defined( 'WP_CONTENT_DIR' ) ) {
  122. // Load custom PHP error template, if present.
  123. $php_error_pluggable = WP_CONTENT_DIR . '/php-error.php';
  124. if ( is_readable( $php_error_pluggable ) ) {
  125. require_once $php_error_pluggable;
  126. return;
  127. }
  128. }
  129. // Otherwise, display the default error template.
  130. $this->display_default_error_template( $error, $handled );
  131. }
  132. /**
  133. * Displays the default PHP error template.
  134. *
  135. * This method is called conditionally if no 'php-error.php' drop-in is available.
  136. *
  137. * It calls {@see wp_die()} with a message indicating that the site is experiencing technical difficulties and a
  138. * login link to the admin backend. The {@see 'wp_php_error_message'} and {@see 'wp_php_error_args'} filters can
  139. * be used to modify these parameters.
  140. *
  141. * @since 5.2.0
  142. * @since 5.3.0 The `$handled` parameter was added.
  143. *
  144. * @param array $error Error information retrieved from `error_get_last()`.
  145. * @param true|WP_Error $handled Whether Recovery Mode handled the fatal error.
  146. */
  147. protected function display_default_error_template( $error, $handled ) {
  148. if ( ! function_exists( '__' ) ) {
  149. wp_load_translations_early();
  150. }
  151. if ( ! function_exists( 'wp_die' ) ) {
  152. require_once ABSPATH . WPINC . '/functions.php';
  153. }
  154. if ( ! class_exists( 'WP_Error' ) ) {
  155. require_once ABSPATH . WPINC . '/class-wp-error.php';
  156. }
  157. if ( true === $handled && wp_is_recovery_mode() ) {
  158. $message = __( 'There has been a critical error on your website, putting it in recovery mode. Please check the Themes and Plugins screens for more details. If you just installed or updated a theme or plugin, check the relevant page for that first.' );
  159. } elseif ( is_protected_endpoint() ) {
  160. $message = __( 'There has been a critical error on your website. Please check your site admin email inbox for instructions.' );
  161. } else {
  162. $message = __( 'There has been a critical error on your website.' );
  163. }
  164. $message = sprintf(
  165. '<p>%s</p><p><a href="%s">%s</a></p>',
  166. $message,
  167. /* translators: Documentation explaining debugging in WordPress. */
  168. esc_url( __( 'https://wordpress.org/support/article/debugging-in-wordpress/' ) ),
  169. __( 'Learn more about debugging in WordPress.' )
  170. );
  171. $args = array(
  172. 'response' => 500,
  173. 'exit' => false,
  174. );
  175. /**
  176. * Filters the message that the default PHP error template displays.
  177. *
  178. * @since 5.2.0
  179. *
  180. * @param string $message HTML error message to display.
  181. * @param array $error Error information retrieved from `error_get_last()`.
  182. */
  183. $message = apply_filters( 'wp_php_error_message', $message, $error );
  184. /**
  185. * Filters the arguments passed to {@see wp_die()} for the default PHP error template.
  186. *
  187. * @since 5.2.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. * @param array $error Error information retrieved from `error_get_last()`.
  192. */
  193. $args = apply_filters( 'wp_php_error_args', $args, $error );
  194. $wp_error = new WP_Error(
  195. 'internal_server_error',
  196. $message,
  197. array(
  198. 'error' => $error,
  199. )
  200. );
  201. wp_die( $wp_error, '', $args );
  202. }
  203. }