Site Health Guard - Source Excerpt 02
Summary
This source excerpt preserves a bounded section of Antichrist.net/wp-content/plugins/uaix-locale-router/includes/site-health-guard.php so readers can inspect the evidence without opening the full source file.
**Source path:** Antichrist.net/wp-content/plugins/uaix-locale-router/includes/site-health-guard.php
function jai_site_health_guard_log_fatal_error( $error, $source ) {
$hash = jai_site_health_guard_error_hash( $error );
$data = array(
'source' => $source,
'hash' => $hash,
'error' => array(
'type' => isset( $error['type'] ) ? jai_site_health_guard_error_type_name( (int) $error['type'] ) : '',
'message' => isset( $error['message'] ) ? str_replace( array( ABSPATH, WP_CONTENT_DIR ), array( './', 'wp-content' ), (string) $error['message'] ) : '',
'file' => isset( $error['file'] ) ? jai_site_health_guard_clean_path( $error['file'] ) : '',
'line' => isset( $error['line'] ) ? (int) $error['line'] : 0,
),
);
$last = jai_site_health_guard_read_state( 'last-fatal' );
if ( is_array( $last ) && isset( $last['hash'], $last['updated_epoch'] ) && $last['hash'] === $hash && time() - (int) $last['updated_epoch'] < 5 ) {
return;
}
jai_site_health_guard_log( 'fatal_error', $data );
jai_site_health_guard_write_state(
'last-fatal',
array_merge(
$data,
array(
'frontend_request' => jai_site_health_guard_is_frontend_request(),
)
)
);
}
function jai_site_health_guard_flag_path( $name ) {
$name = preg_replace( '/[^a-z0-9\-\.]/i', '', (string) $name );
return jai_site_health_guard_log_dir() . '/' . $name;
}
function jai_site_health_guard_read_flag( $name ) {
$file = jai_site_health_guard_flag_path( $name );
if ( ! is_readable( $file ) ) {
return null;
}
return array(
'name' => $name,
'path' => jai_site_health_guard_clean_path( $file ),
'updated_epoch' => (int) filemtime( $file ),
'content' => trim( (string) @file_get_contents( $file ) ),
);
}
function jai_site_health_guard_recovery_signal() {
$manual = jai_site_health_guard_read_flag( 'frontdoor-recovery.flag' );
if ( is_array( $manual ) ) {
return array(
'active' => true,
'source' => 'manual-frontdoor-recovery-flag',
'flag' => $manual,
);
}
$last_fatal = jai_site_health_guard_read_state( 'last-fatal' );
if ( ! is_array( $last_fatal ) || empty( $last_fatal['frontend_request'] ) || empty( $last_fatal['updated_epoch'] ) ) {
return array( 'active' => false );
}
$age = time() - (int) $last_fatal['updated_epoch'];
if ( $age < 0 || $age > (int) JAI_SITE_HEALTH_GUARD_RECOVERY_TTL ) {
return array( 'active' => false );
}
return array(
'active' => true,
'source' => 'recent-frontend-fatal',
'age' => $age,
'fatal' => $last_fatal,
);
}
function jai_site_health_guard_empty_array() {
return array();
}
function jai_site_health_guard_disable_active_plugins_if_needed() {
if ( PHP_SAPI === 'cli' ) {
return;
}
$manual_disable = jai_site_health_guard_read_flag( 'disable-plugins.flag' );
$signal = jai_site_health_guard_recovery_signal();
if ( is_array( $manual_disable ) || ( ! empty( $signal['active'] ) && jai_site_health_guard_is_guard_rest_request() ) ) {
add_filter( 'pre_option_active_plugins', 'jai_site_health_guard_empty_array', PHP_INT_MAX );
add_filter( 'pre_site_option_active_sitewide_plugins', 'jai_site_health_guard_empty_array', PHP_INT_MAX );
define( 'JAI_SITE_HEALTH_GUARD_PLUGIN_ISOLATION', true );
}
}
function jai_site_health_guard_recovery_theme_slug() {
$themes_dir = rtrim( WP_CONTENT_DIR, '/\\' ) . '/themes';
foreach ( array( 'twentytwentysix', 'twentytwentyfive', 'twentytwentyfour', 'twentytwentythree', 'twentytwentytwo' ) as $slug ) {
if ( is_dir( $themes_dir . '/' . $slug ) ) {
return $slug;
}
}
return '';
}
function jai_site_health_guard_disable_active_theme_if_needed() {
if ( PHP_SAPI === 'cli' || ! is_array( jai_site_health_guard_read_flag( 'disable-theme.flag' ) ) ) {
return;
}
$theme = jai_site_health_guard_recovery_theme_slug();
if ( '' === $theme ) {
return;
}
add_filter(
'pre_option_stylesheet',
static function () use ( $theme ) {
return $theme;
},
PHP_INT_MAX
);
add_filter(
'pre_option_template',
static function () use ( $theme ) {
return $theme;
},
PHP_INT_MAX
);
define( 'JAI_SITE_HEALTH_GUARD_THEME_ISOLATION', true );
}
function jai_site_health_guard_render_recovery_page( $signal ) {
if ( ! headers_sent() ) {
http_response_code( 503 );
header( 'Content-Type: text/html; charset=utf-8' );
header( 'Retry-After: 300' );
header( 'Cache-Control: no-store, max-age=0' );
header( 'X-Robots-Tag: noindex, noarchive' );
header( 'X-Site-Health-Guard: recovery' );
}
jai_site_health_guard_log(
'recovery_response',
array(
'source' => isset( $signal['source'] ) ? (string) $signal['source'] : 'unknown',
)
);
if ( 'HEAD' === jai_site_health_guard_request_method() ) {
exit;
}
$label = htmlspecialchars( jai_site_health_guard_site_label(), ENT_QUOTES, 'UTF-8' );
$host = htmlspecialchars( jai_site_health_guard_public_host(), ENT_QUOTES, 'UTF-8' );
$links = array(
'/robots.txt' => 'Robots',
'/sitemap.xml' => 'Sitemap',
'/.well-known/uai.json' => 'UAI discovery',
);
echo '<!doctype html><html lang="en"><head><meta charset="utf-8">';
echo '<meta name="viewport" content="width=device-width,initial-scale=1">';
echo '<title>' . $label . ' recovery mode</title>';
echo '<style>';
echo 'body{margin:0;font-family:system-ui,-apple-system,Segoe UI,sans-serif;background:#f6f7f4;color:#18201b}';
echo '.wrap{min-height:100vh;display:flex;align-items:center;justify-content:center;padding:32px}';
echo '.panel{max-width:760px;border:1px solid #cfd8cf;background:#fff;padding:32px}';
echo 'h1{margin:0 0 12px;font-size:30px;line-height:1.15}p{font-size:17px;line-height:1.55;margin:0 0 16px}';
echo '.meta{font-size:13px;color:#536057;text-transform:uppercase;letter-spacing:.08em;margin-bottom:14px}';
echo '.links{display:flex;flex-wrap:wrap;gap:10px;margin-top:22px}.links a{color:#0e5f3b;border:1px solid #b8c9bd;padding:9px 12px;text-decoration:none}';
echo '</style></head><body><main class="wrap"><section class="panel">';
echo '<div class="meta">Recovery mode</div>';
echo '<h1>' . $label . ' is temporarily protecting the front door.</h1>';
echo '<p>The public WordPress runtime hit an error, so this site is serving a safe recovery response while the deployment is repaired.</p>';
echo '<p>Static files and administrative recovery paths may still be available on ' . $host . '.</p>';
echo '<div class="links">';
foreach ( $links as $href => $text ) {
echo '<a href="' . htmlspecialchars( $href, ENT_QUOTES, 'UTF-8' ) . '">' . htmlspecialchars( $text, ENT_QUOTES, 'UTF-8' ) . '</a>';
}
echo '</div></section></main></body></html>';
exit;
}
function jai_site_health_guard_maybe_serve_recovery_page() {
$signal = jai_site_health_guard_recovery_signal();
if ( empty( $signal['active'] ) || ! jai_site_health_guard_is_frontend_request() ) {
return;
}
jai_site_health_guard_render_recovery_page( $signal );
}
function jai_site_health_guard_maybe_recover() {
if ( ! jai_site_health_guard_is_frontend_request() ) {
return;
}
$last_fatal = jai_site_health_guard_read_state( 'last-fatal' );
if ( ! is_array( $last_fatal ) || empty( $last_fatal['frontend_request'] ) || empty( $last_fatal['updated_epoch'] ) ) {
return;
}
if ( time() - (int) $last_fatal['updated_epoch'] > (int) JAI_SITE_HEALTH_GUARD_RECOVERY_TTL ) {
return;
}
$hash = isset( $last_fatal['hash'] ) ? (string) $last_fatal['hash'] : '';
$last_recovery = jai_site_health_guard_read_state( 'last-recovery' );
if ( is_array( $last_recovery ) && isset( $last_recovery['hash'], $last_recovery['updated_epoch'] ) && $last_recovery['hash'] === $hash && time() - (int) $last_recovery['updated_epoch'] < 300 ) {
return;
}
$actions = array();
if ( function_exists( 'wp_cache_flush' ) ) {
@wp_cache_flush();
$actions[] = 'wp_cache_flush';
}
if ( function_exists( 'delete_option' ) ) {
@delete_option( 'rewrite_rules' );
$actions[] = 'delete_rewrite_rules_option';
}
if ( function_exists( 'wp_clean_themes_cache' ) ) {
@wp_clean_themes_cache( true );
$actions[] = 'wp_clean_themes_cache';
}
jai_site_health_guard_log(
'recovery_attempt',
array(
'hash' => $hash,
'actions' => $actions,
)
);
jai_site_health_guard_write_state(
'last-recovery',
array(
'hash' => $hash,
'actions' => $actions,
)
);
}
function jai_site_health_guard_shutdown() {
$error = error_get_last();
if ( jai_site_health_guard_is_fatal_error( $error ) ) {
jai_site_health_guard_log_fatal_error( $error, 'plugin-shutdown' );
return;
}
$status = function_exists( 'http_response_code' ) ? (int) http_response_code() : 0;
$duration = (int) round( ( microtime( true ) - JAI_SITE_HEALTH_GUARD_STARTED_AT ) * 1000 );
if ( $status >= 500 ) {
jai_site_health_guard_log(
'http_5xx',
array(
'status' => $status,
'duration_ms' => $duration,
)
);
return;
}
if ( $duration >= 8000 ) {
jai_site_health_guard_log(
'slow_request',
array(
'status' => $status,
'duration_ms' => $duration,
)
);
}