Нет описания

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

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