error-protection.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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 {@see error_get_last()}
  38. *
  39. * @return string Formatted error description.
  40. */
  41. function wp_get_extension_error_description( $error ) {
  42. $constants = get_defined_constants( true );
  43. $constants = isset( $constants['Core'] ) ? $constants['Core'] : $constants['internal'];
  44. $core_errors = array();
  45. foreach ( $constants as $constant => $value ) {
  46. if ( 0 === strpos( $constant, 'E_' ) ) {
  47. $core_errors[ $value ] = $constant;
  48. }
  49. }
  50. if ( isset( $core_errors[ $error['type'] ] ) ) {
  51. $error['type'] = $core_errors[ $error['type'] ];
  52. }
  53. /* translators: 1: Error type, 2: Error line number, 3: Error file name, 4: Error message. */
  54. $error_message = __( 'An error of type %1$s was caused in line %2$s of the file %3$s. Error message: %4$s' );
  55. return sprintf(
  56. $error_message,
  57. "<code>{$error['type']}</code>",
  58. "<code>{$error['line']}</code>",
  59. "<code>{$error['file']}</code>",
  60. "<code>{$error['message']}</code>"
  61. );
  62. }
  63. /**
  64. * Registers the shutdown handler for fatal errors.
  65. *
  66. * The handler will only be registered if {@see wp_is_fatal_error_handler_enabled()} returns true.
  67. *
  68. * @since 5.2.0
  69. */
  70. function wp_register_fatal_error_handler() {
  71. if ( ! wp_is_fatal_error_handler_enabled() ) {
  72. return;
  73. }
  74. $handler = null;
  75. if ( defined( 'WP_CONTENT_DIR' ) && is_readable( WP_CONTENT_DIR . '/fatal-error-handler.php' ) ) {
  76. $handler = include WP_CONTENT_DIR . '/fatal-error-handler.php';
  77. }
  78. if ( ! is_object( $handler ) || ! is_callable( array( $handler, 'handle' ) ) ) {
  79. $handler = new WP_Fatal_Error_Handler();
  80. }
  81. register_shutdown_function( array( $handler, 'handle' ) );
  82. }
  83. /**
  84. * Checks whether the fatal error handler is enabled.
  85. *
  86. * A constant `WP_DISABLE_FATAL_ERROR_HANDLER` can be set in `wp-config.php` to disable it, or alternatively the
  87. * {@see 'wp_fatal_error_handler_enabled'} filter can be used to modify the return value.
  88. *
  89. * @since 5.2.0
  90. *
  91. * @return bool True if the fatal error handler is enabled, false otherwise.
  92. */
  93. function wp_is_fatal_error_handler_enabled() {
  94. $enabled = ! defined( 'WP_DISABLE_FATAL_ERROR_HANDLER' ) || ! WP_DISABLE_FATAL_ERROR_HANDLER;
  95. /**
  96. * Filters whether the fatal error handler is enabled.
  97. *
  98. * @since 5.2.0
  99. *
  100. * @param bool $enabled True if the fatal error handler is enabled, false otherwise.
  101. */
  102. return apply_filters( 'wp_fatal_error_handler_enabled', $enabled );
  103. }
  104. /**
  105. * Access the WordPress Recovery Mode instance.
  106. *
  107. * @since 5.2.0
  108. *
  109. * @return WP_Recovery_Mode
  110. */
  111. function wp_recovery_mode() {
  112. static $wp_recovery_mode;
  113. if ( ! $wp_recovery_mode ) {
  114. $wp_recovery_mode = new WP_Recovery_Mode();
  115. }
  116. return $wp_recovery_mode;
  117. }