<?php
/**
 * WPEA_MANAGED_FATAL_HANDLER
 * Managed by Erriscope. Do not edit manually.
 */
if ( ! defined( 'ABSPATH' ) || ! class_exists( 'WP_Fatal_Error_Handler' ) ) {
	return null;
}

if ( ! defined( 'WPEA_FATAL_HANDLER_ACTIVE' ) ) {
	define( 'WPEA_FATAL_HANDLER_ACTIVE', true );
}
if ( ! defined( 'WPEA_FATAL_HANDLER_VERSION' ) ) {
	define( 'WPEA_FATAL_HANDLER_VERSION', __WPEA_VERSION__ );
}

if ( ! class_exists( 'WPEA_Managed_Fatal_Error_Handler', false ) ) {
	final class WPEA_Managed_Fatal_Error_Handler extends WP_Fatal_Error_Handler {
		public function handle() {
			if ( defined( 'WP_SANDBOX_SCRAPING' ) && WP_SANDBOX_SCRAPING ) {
				return;
			}
			if ( function_exists( 'wp_is_maintenance_mode' ) && wp_is_maintenance_mode() ) {
				return;
			}

			try {
				$error = $this->detect_error();
				if ( ! $error ) {
					return;
				}

				// Capture before Recovery Mode, translations, templates, or any second error can alter error_get_last().
				$this->wpea_capture_error( $error );

				if ( ! isset( $GLOBALS['wp_locale'] ) && function_exists( 'load_default_textdomain' ) ) {
					load_default_textdomain();
				}

				$handled = false;
				if ( function_exists( 'is_multisite' ) && ! is_multisite() && function_exists( 'wp_recovery_mode' ) && wp_recovery_mode()->is_initialized() ) {
					$handled = wp_recovery_mode()->handle_error( $error );
				}

				if ( ( function_exists( 'is_admin' ) && is_admin() ) || ! headers_sent() ) {
					$this->display_error_template( $error, $handled );
				}
			} catch ( \Throwable $e ) {
				// A fatal handler must never create a secondary visible failure.
			}
		}

		private function wpea_capture_error( array $error ) {
			$type = isset( $error['type'] ) ? (int) $error['type'] : 0;
			if ( ! in_array( $type, array( E_ERROR, E_PARSE, E_CORE_ERROR, E_COMPILE_ERROR, E_USER_ERROR, E_RECOVERABLE_ERROR ), true ) ) {
				return;
			}

			$file = isset( $error['file'] ) ? str_replace( '\\\\', '/', (string) $error['file'] ) : '';
			$plugin_dir = rtrim( str_replace( '\\', '/', (string) __WPEA_PLUGIN_DIR__ ), '/' ) . '/';
			if ( '/' !== $plugin_dir && 0 === strpos( $file, $plugin_dir ) ) {
				return;
			}

			$message = isset( $error['message'] ) ? (string) $error['message'] : 'Unknown fatal PHP error';
			$message = str_replace( array( "\\0", "\\r", "\\n" ), array( '', ' ', ' ' ), strip_tags( $message ) );
			if ( strlen( $message ) > 4000 ) {
				$message = substr( $message, 0, 4000 );
			}
			if ( strlen( $file ) > 2048 ) {
				$file = substr( $file, 0, 2048 );
			}

			$request = isset( $_SERVER['REQUEST_URI'] ) ? (string) $_SERVER['REQUEST_URI'] : '';
			$request = str_replace( array( "\\0", "\\r", "\\n" ), array( '', ' ', ' ' ), strip_tags( $request ) );
			if ( strlen( $request ) > 2048 ) {
				$request = substr( $request, 0, 2048 );
			}

			$record = array(
				'type'         => $type,
				'message'      => $message,
				'file'         => $file,
				'line'         => isset( $error['line'] ) ? max( 0, (int) $error['line'] ) : 0,
				'request_path' => $request,
				'captured_at'  => time(),
				'capture_path' => 'wordpress_fatal_dropin_v2',
			);
			$record['fingerprint'] = hash( 'sha256', implode( '|', array( (string) $record['type'], $record['message'], $record['file'], (string) $record['line'], $record['request_path'] ) ) );

			$encoded = json_encode( $record, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE );
			if ( ! is_string( $encoded ) || '' === $encoded ) {
				return;
			}
			$line = $encoded . "\\n";

			$primary = __WPEA_BUFFER_PATH__;
			if ( $this->wpea_append( $primary, $line ) ) {
				return;
			}

		}

		private function wpea_append( $path, $line ) {
			if ( ! is_string( $path ) || '' === $path ) {
				return false;
			}
			$handle = @fopen( $path, 'ab' );
			if ( ! is_resource( $handle ) ) {
				return false;
			}
			$ok = false;
			if ( @flock( $handle, LOCK_EX ) ) {
				$ok = false !== @fwrite( $handle, $line );
				@fflush( $handle );
				@flock( $handle, LOCK_UN );
			}
			@fclose( $handle );
			return $ok;
		}
	}
}

return new WPEA_Managed_Fatal_Error_Handler();
