=== Fatal Rescue ===
Contributors: kapoordev
Tags: fatal error, error, maintenance, security, debug
Requires at least: 5.9
Tested up to: 7.1
Requires PHP: 7.4
Stable tag: 1.0.0
License: GPLv2 or later
License URI: https://www.gnu.org/licenses/gpl-2.0.html

A PHP fatal from a plugin or theme? Get a recovery panel that works while wp-admin is down, and pause the culprit for every visitor in one click.

== Description ==

When a plugin fatals, WordPress shows "There has been a critical error on this website" to everyone, and the usual fix is to log in and deactivate it. You cannot log in. Recovery Mode emails a link that only works for one browser on one device, and if the database or the theme is the problem, it often fails too.

Fatal Rescue changes what happens on the server:

* **The site gets its visitors back.** One click on the emailed link skips the broken extension for *every* visitor, not just you — your pages, your products and your contact form keep working while it sits in quarantine. Fatal Rescue does not disable an extension you have not approved it to disable, so the first fatal of a new outage still shows an error page until you pause it; the email says exactly which extension to pause and the panel is one tap away.
* **The fix does not need wp-admin.** A standalone recovery panel is reachable at a private URL. It never loads WordPress and never queries the database, so it answers on a request where the front end, `/wp-admin/` and `/wp-login.php` all return 500.
* **Nothing is destroyed.** Pausing an extension is not deactivating it. The database still says the plugin is active, its options are untouched, and Restore puts it back exactly as it was.
* **It says what broke and mails you the link.** Each fatal is recorded with the responsible plugin or theme, the bootstrap stage it died in (`loading_plugin`, `plugins_loaded`, `template_render`, `setup_theme`…), the error type and the message. The recovery link reaches the site admin address by email, so you do not have to reach wp-admin to find out where to go.
* **It catches itself falling.** Restore an extension and the same fatal comes straight back? Fatal Rescue pauses it again by itself, at any bootstrap stage, so a recovery you talked yourself into cannot leave the site down. That watch ends as soon as a request finishes cleanly, and expires on its own after five minutes.
* **Safe mode.** Cannot tell which of twelve plugins broke the site? Skip every plugin and the theme in one click, then turn them back on until the fatal returns.
* **You can test it before you need it.** From the Fatal Rescue screen, a rehearsal makes the site fatal on purpose using a test file Fatal Rescue ships for the job and loads only while a rehearsal is armed — never one of yours, and nothing executable is ever written to your site — and then recovers it through the same code path a real outage uses. The rehearsal expires by itself after five minutes, and stops the moment you deactivate the plugin.

The visitor-facing page is a plain "temporarily unavailable" notice. Only a logged-in administrator sees the diagnosis, and the recovery link is never printed on a page any visitor can read.

= What this is not =

It is not a backup plugin and it does not repair code. It does not stop a fatal from happening; it stops one fatal from taking the site down while you decide what to do. Use it with backups, not instead of them.

= How it works =

WordPress registers its fatal-error handler at `wp-settings.php` line 69, before plugins and themes load, and only a `wp-content/fatal-error-handler.php` drop-in can replace it. A normal plugin registering `shutdown` runs too late: core's handler has already rendered the error page and exited. On activation, Fatal Rescue installs two small files to get in front of that:

1. `wp-content/fatal-error-handler.php` — captures the fatal and names the extension responsible.
2. `wp-content/mu-plugins/fatal-rescue-loader.php` — filters `option_active_plugins`, `option_template` and `option_stylesheet` so the quarantine applies to every visitor.

Those two files are also the only way a URL can reach any PHP this plugin ships. The recovery panel answers at whichever of them this install owns — the drop-in normally, and the mu-plugin when another tool holds the handler slot — and it reads the state file directly, with no database, no theme and no other plugin loaded. Request any file inside the plugin folder and you get a blank page: the panel code exits before it runs anything unless one of its two guards called it.

Everything else lives inside the plugin folder. The recovery state is a JSON file in `wp-content/uploads/fatal-rescue/`, outside the plugin directory, so a plugin update or a failed upload mid-update cannot delete the record of what is paused. That folder carries its own deny rules, the file is written `0600`, and its name is a random per-install token rather than `state.json`, so a request that guesses the URL finds nothing even on nginx or IIS, where `.htaccess` is never read. If you would rather block the folder outright at the server level, deny `wp-content/uploads/fatal-rescue` as you would any other protected upload folder.

== Installation ==

1. Plugins → Add New → Upload Plugin → `fatal-rescue.zip` → Install Now → Activate.
2. Read the panel URL in the green notice and save it somewhere outside the site (password manager, notes app, phone). It is the only way in when the site is down. The plugin never sends it anywhere on its own: use the "Email me the panel link now" button on the Fatal Rescue screen when you want a copy off the site.
3. That is the whole setup. Nothing needs configuring, and there is nothing to do until something breaks.

If `wp-content/` is not writable by PHP, the plugin tells you exactly which two files to upload and shows their contents. Both are removed on deactivation, and never overwritten if another tool owns them.

== Frequently Asked Questions ==

= Does pausing a plugin delete its settings? =

No. The `active_plugins` option is never modified. The extension is filtered out at load time, which is why Restore is instant and safe.

= Does this work when the theme is the problem? =

Yes. A fatal in a theme's `functions.php` is attributed to the theme, and pausing it switches to a bundled default theme such as Twenty Twenty-Five until you restore yours.

= Does the panel get emailed to me? =

Not on activation. Installing the plugin sends no mail of any kind, because the link is a credential and an inbox is not a place to put one unasked.

What does get emailed is an outage: when Fatal Rescue catches a fatal, the next page view mails the site admin address a notice naming the extension, the stage it died at, and the panel link. The send happens on the next page view rather than inside the crash - the fatal handler runs at shutdown with whatever is left of a working WordPress, and a fatal caused by the database cannot also be delivered by a database query. There is no cron and no third-party service, so a site nobody visits holds the notice until someone loads a page, and the Fatal Rescue screen always shows what is waiting and why.

You can ask for the link itself at any time with the "Email me the panel link now" button, which is also how you prove the site can deliver mail before an outage has to depend on it. Nothing sends that one unasked.

Delivery goes through `wp_mail()`, so it follows whatever mail setup the site already has. Define `FATAL_RESCUE_NO_MAIL` in `wp-config.php` to stop it, or point the `fatal_rescue_notify_email` filter at an agency address. A site that cannot send mail says so on that screen instead of failing quietly.

= Can it fix a broken must-use plugin? =

It reports one honestly and tells you to rename it over FTP. WordPress loads `mu-plugins/` before any filter runs, so no plugin — including this one — can skip an mu-plugin. That is a WordPress limitation, not a Fatal Rescue shortcut.

= Does it work on a multisite network? =

Yes, with one consequence worth knowing. Both guard files and the recovery state live once per installation — in `wp-content/`, not per site — because they are read before WordPress resolves which site a request is for. So pausing an extension from any site's panel pauses it for the whole network, and Restore returns it network-wide. You do not need to network-activate it: activating on one site installs the shared guards that cover every site. The Fatal Rescue screen is registered under each site's admin rather than Network Admin, so run the rehearsal and the mail test from the site you activated on.

= How do I know this works before an outage? =

Run a rehearsal from the Fatal Rescue screen. The failure comes from `src/rehearsal-fixture.php`, a file this plugin ships and loads only while a rehearsal is armed, so nothing executable is ever written to your site. Arming writes one file: an expiry marker holding one integer, in `wp-content/uploads/fatal-rescue`, the folder Fatal Rescue already keeps its recovery state in. The next front-end page view fatals for real; Fatal Rescue catches it, names Fatal Rescue Rehearsal as the culprit and hands you the panel. Pause it and the site renders again; stop the rehearsal and the marker is deleted. If you walk away, the marker expires in five minutes and the rehearsal ends on its own. Your own plugins, themes and options are never involved.

= Will it slow my site down? =

On a healthy site it registers four option filters, records which bootstrap stage WordPress is in, and touches no database and no file on the request that renders a page: the one write it makes is a refresh of the plain-file snapshot of what is active, and that happens only when the list actually changes or once an hour. No cron job, no outbound request, no query. Mail leaves only when there is something to report, and it reads the state file the guard had already loaded. The settings screen and the installer are only loaded on admin requests, and `fatal-error-handler.php` is read by WordPress only when a fatal is already happening. Nine interleaved pairs of the same page on a local test bed (guard files present, guard files renamed away) came out within noise of each other; absolute timings on one laptop track swap, not code, so the paired comparison is the one worth quoting.

= What happens if another plugin already owns the fatal-error handler? =

Fatal Rescue leaves that file alone and says so on its settings screen. Quarantine still works, so a broken extension is still skipped for every visitor; only the custom error page belongs to the other tool. The recovery panel moves to its other guard file, `wp-content/mu-plugins/fatal-rescue-loader.php`, and the link on the settings screen, in the dashboard notice and in the outage email points there instead — so the page you open from your phone on the day the site is broken is still there.

== Screenshots ==

1. The recovery panel during an outage, opened from the emailed link with no wp-admin, no database and
   no WordPress load. It names the extension responsible, the bootstrap stage it died in, the file and
   line, and how confident the attribution is.
2. The Fatal Rescue screen in wp-admin while a rehearsal is running: guard status, the two files it owns,
   where notices go and what is waiting to send, the last fatal it caught, and the countdown until the
   rehearsal ends on its own.
3. What a visitor sees while a broken extension is quarantined instead of a PHP stack trace: a plain
   "temporarily unavailable" notice, with no diagnosis and no recovery link on it.

== Changelog ==

= 1.0.0 =
* First public release: installable from wp-admin, with a settings screen that reports whether both guard files are in place.
* Recovery panel works with no database, no session and no WordPress load, so it answers on a request where the front end, `/wp-admin/` and `/wp-login.php` all return 500.
* Installing sends no mail. A caught fatal queues a notice to the site admin address, delivered on the next page view with a 30-minute cooldown and a daily re-report while a problem stays unfixed, and the same screen can send you the panel link on request. The screen shows what is waiting and what failed.
* Self-heal re-pause when a restored extension fatals again, for fatals at every bootstrap stage - including the ones that happen after `wp_loaded`, inside a template or on the `wp` hook. That watch ends as soon as a request finishes cleanly and expires on its own after five minutes, so it cannot outlive the restore it belongs to.
* Safe mode for "I do not know which plugin did this".
* Rehearsal mode: watch a real fatal get recovered, on a test file this plugin ships for the purpose, before you have a real one.

== Upgrade Notice ==

= 1.0.0 =
First release. The panel URL is shown on the Fatal Rescue screen when you activate - save it somewhere outside the site, since it is the only way in during an outage. A notice with that link is mailed only when a fatal is actually caught.

== Technical notes ==

Fatal Rescue is deliberately dependency-free in its critical path: no JavaScript, no HTTP requests, no background cron, no third-party service, and no account. The recovery panel is protected by a random key in the URL and HMAC-signed, expiring action tokens, so the link is the credential and a forged form post changes nothing. The only place that credential leaves the server is the outage notice, which is why that mail tells you to save the link and then forget the mail.
