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