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

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