error-protection.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. /**
  3. * Error Protection API: Functions
  4. *
  5. * @package WordPress
  6. * @since 5.2.0
  7. */
  8. /**
  9. * Get the instance for storing paused plugins.
  10. *
  11. * @return WP_Paused_Extensions_Storage
  12. */
  13. function wp_paused_plugins() {
  14. static $storage = null;
  15. if ( null === $storage ) {
  16. $storage = new WP_Paused_Extensions_Storage( 'plugin' );
  17. }
  18. return $storage;
  19. }
  20. /**
  21. * Get the instance for storing paused extensions.
  22. *
  23. * @return WP_Paused_Extensions_Storage
  24. */
  25. function wp_paused_themes() {
  26. static $storage = null;
  27. if ( null === $storage ) {
  28. $storage = new WP_Paused_Extensions_Storage( 'theme' );
  29. }
  30. return $storage;
  31. }
  32. /**
  33. * Get a human readable description of an extension's error.
  34. *
  35. * @since 5.2.0
  36. *
  37. * @param array $error Error details from `error_get_last()`.
  38. * @return string Formatted error description.
  39. */
  40. function wp_get_extension_error_description( $error ) {
  41. $constants = get_defined_constants( true );
  42. $constants = isset( $constants['Core'] ) ? $constants['Core'] : $constants['internal'];
  43. $core_errors = array();
  44. foreach ( $constants as $constant => $value ) {
  45. if ( 0 === strpos( $constant, 'E_' ) ) {
  46. $core_errors[ $value ] = $constant;
  47. }
  48. }
  49. if ( isset( $core_errors[ $error['type'] ] ) ) {
  50. $error['type'] = $core_errors[ $error['type'] ];
  51. }
  52. /* translators: 1: Error type, 2: Error line number, 3: Error file name, 4: Error message. */
  53. $error_message = __( 'An error of type %1$s was caused in line %2$s of the file %3$s. Error message: %4$s' );
  54. return sprintf(
  55. $error_message,
  56. "<code>{$error['type']}</code>",
  57. "<code>{$error['line']}</code>",
  58. "<code>{$error['file']}</code>",
  59. "<code>{$error['message']}</code>"
  60. );
  61. }
  62. /**
  63. * Registers the shutdown handler for fatal errors.
  64. *
  65. * The handler will only be registered if {@see wp_is_fatal_error_handler_enabled()} returns true.
  66. *
  67. * @since 5.2.0
  68. */
  69. function wp_register_fatal_error_handler() {
  70. if ( ! wp_is_fatal_error_handler_enabled() ) {
  71. return;
  72. }
  73. $handler = null;
  74. if ( defined( 'WP_CONTENT_DIR' ) && is_readable( WP_CONTENT_DIR . '/fatal-error-handler.php' ) ) {
  75. $handler = include WP_CONTENT_DIR . '/fatal-error-handler.php';
  76. }
  77. if ( ! is_object( $handler ) || ! is_callable( array( $handler, 'handle' ) ) ) {
  78. $handler = new WP_Fatal_Error_Handler();
  79. }
  80. register_shutdown_function( array( $handler, 'handle' ) );
  81. }
  82. /**
  83. * Checks whether the fatal error handler is enabled.
  84. *
  85. * A constant `WP_DISABLE_FATAL_ERROR_HANDLER` can be set in `wp-config.php` to disable it, or alternatively the
  86. * {@see 'wp_fatal_error_handler_enabled'} filter can be used to modify the return value.
  87. *
  88. * @since 5.2.0
  89. *
  90. * @return bool True if the fatal error handler is enabled, false otherwise.
  91. */
  92. function wp_is_fatal_error_handler_enabled() {
  93. $enabled = ! defined( 'WP_DISABLE_FATAL_ERROR_HANDLER' ) || ! WP_DISABLE_FATAL_ERROR_HANDLER;
  94. /**
  95. * Filters whether the fatal error handler is enabled.
  96. *
  97. * **Important:** This filter runs before it can be used by plugins. It cannot
  98. * be used by plugins, mu-plugins, or themes. To use this filter you must define
  99. * a `$wp_filter` global before WordPress loads, usually in `wp-config.php`.
  100. *
  101. * Example:
  102. *
  103. * $GLOBALS['wp_filter'] = array(
  104. * 'wp_fatal_error_handler_enabled' => array(
  105. * 10 => array(
  106. * array(
  107. * 'accepted_args' => 0,
  108. * 'function' => function() {
  109. * return false;
  110. * },
  111. * ),
  112. * ),
  113. * ),
  114. * );
  115. *
  116. * Alternatively you can use the `WP_DISABLE_FATAL_ERROR_HANDLER` constant.
  117. *
  118. * @since 5.2.0
  119. *
  120. * @param bool $enabled True if the fatal error handler is enabled, false otherwise.
  121. */
  122. return apply_filters( 'wp_fatal_error_handler_enabled', $enabled );
  123. }
  124. /**
  125. * Access the WordPress Recovery Mode instance.
  126. *
  127. * @since 5.2.0
  128. *
  129. * @return WP_Recovery_Mode
  130. */
  131. function wp_recovery_mode() {
  132. static $wp_recovery_mode;
  133. if ( ! $wp_recovery_mode ) {
  134. $wp_recovery_mode = new WP_Recovery_Mode();
  135. }
  136. return $wp_recovery_mode;
  137. }