=== WP Emergency Recovery ===
Contributors: pluginivo
Tags: recovery, safe mode, fatal error, mu-plugin, emergency
Requires at least: 6.0
Tested up to: 7.1
Requires PHP: 7.4
Stable tag: 1.2.1
License: GPLv2 or later

Emergency recovery and isolated safe-mode access for WordPress sites when a regular plugin causes a fatal error.

== Description ==

WP Emergency Recovery installs a small must-use (MU) recovery layer packaged with this plugin. It does not fetch or execute remote code.

The Free edition provides manual emergency recovery: fatal logging, an early tokenized recovery panel, a standalone fallback endpoint, a file-backed emergency block list, and manual plugin deactivation.

A separate WP Emergency Recovery Pro add-on is available outside WordPress.org. Pro code is not included or locked inside the Free plugin. The add-on can provide automatic fatal isolation, email alerts, pre-update activation snapshots, signed remote recovery, and private commercial updates.

== What it installs ==

The regular plugin is the installer/manager. The recovery files are copied from this plugin package to:

wp-content/mu-plugins/000-wp-emergency-recovery-loader.php
wp-content/mu-plugins/wp-emergency-recovery/engine.php
wp-content/mu-plugins/wp-emergency-recovery/recovery-core.php
wp-content/mu-plugins/wp-emergency-recovery/panel.php
wp-content/mu-plugins/wp-emergency-recovery/recovery.php

A random 64-character token is generated on activation and the installer attempts to add:

define('WP_RECOVERY_TOKEN', '...');

to wp-config.php. If wp-config.php is not writable, Tools > Emergency Recovery shows the exact line to add manually.

== Deactivation, removal and WP-CLI ==

Deactivating the plugin does NOT remove the recovery layer: the MU files keep logging fatal errors, keep emergency blocks in force and the emergency URLs keep working. To switch recovery off, use "Remove MU recovery layer" on Tools > Emergency Recovery, or delete the plugin (uninstall removes the MU layer and its protected storage).

When the plugin is activated with WP-CLI as root, created files are handed over to the owner of wp-content, and wp-config.php keeps its original owner and permissions. Tools > Emergency Recovery shows whether the protected storage is writable by the web server.

Emergency-blocked plugins are listed on Tools > Emergency Recovery (with a "Remove block" button) and announced on the Plugins screen.

== Recovery URLs ==

1. Early WordPress/MU route:
https://example.com/?wp_recovery=YOUR_TOKEN

2. Standalone fallback that does not load WordPress:
https://example.com/wp-content/mu-plugins/wp-emergency-recovery/recovery.php?token=YOUR_TOKEN

Keep the token private. It is an emergency administrator credential.

== Free features ==

* Must-use loader runs before normal plugins.
* register_shutdown_function() records fatal PHP errors.
* Detects whether a fatal error came from an active plugin.
* Manual plugin deactivation from an isolated recovery panel.
* File-backed emergency block list independent of the WordPress options table.
* Site and network active-plugin filtering when WordPress can bootstrap.
* Last active-plugin lists cached in a file for standalone fallback.
* Recovery state and fatal logs are stored in protected wp-content/.wper-private/ PHP-guarded files (or WPER_STORAGE_DIR when configured).
* PHP guards protect payloads independently of Apache; .htaccess and web.config are added as defense-in-depth.
* Five failed token attempts per IP in 15 minutes trigger a 15-minute file-backed lock.
* Standalone best-effort direct database deactivation for conventional literal DB_* constants in wp-config.php.

== Privacy / external services ==

The Free plugin does not send site, recovery, usage, license, or fatal-error data to an external service. The optional Pro add-on has its own licensing/update disclosure and is distributed separately.

== Last-resort kill switch ==

If the recovery MU layer itself ever causes a problem, add this to wp-config.php:

define('WPER_DISABLE', true);

The MU loader checks the constant before loading the recovery engine. You can also rename/delete wp-content/mu-plugins/000-wp-emergency-recovery-loader.php using hosting File Manager/FTP.

== Important limitations ==

* The ?wp_recovery= route still requires WordPress core to reach MU-plugin loading. Use recovery.php when the normal bootstrap cannot get that far.
* A complete database outage cannot be repaired simply by disabling a plugin. The standalone panel can still use the cached active-plugin list and write a block for the next DB-capable request.
* Dynamic DB credentials are intentionally not evaluated by the standalone parser. It writes the file-backed block instead.
* The recovery system cannot protect against failures before MU-plugin loading, such as a broken wp-config.php, damaged WordPress core, PHP startup failure, or a missing PHP extension.
* Fatal errors in themes/core are logged but are not treated as plugin failures.
* Query-string tokens may appear in server access logs. Use HTTPS and rotate the token if it may have leaked.
* For maximum isolation you may define WPER_STORAGE_DIR to an absolute directory outside the public web root. The standalone endpoint reads this constant from wp-config.php.
* The standalone endpoint reads literal define() values from wp-config.php and ignores commented-out lines. Constants built at runtime (for example from getenv()) are not evaluated.
* The recovery token must be at least 32 characters long.

== Installation ==

1. Upload and activate the plugin as a normal WordPress plugin.
2. Open Tools > Emergency Recovery.
3. Confirm MU loader = Installed and wp-config.php token = Configured.
4. Save both emergency URLs in a secure password manager.
5. Test manual recovery on staging with a deliberately broken test plugin.

== Changelog ==

= 1.2.1 =
* Fixed: activating with WP-CLI as root could make wp-config.php unreadable for the web server (site outage on hosts with 0640/0600 permissions). wp-config.php now keeps its owner and permissions; created recovery files are handed over to the site owner.
* Fixed: a commented-out (revoked) WP_RECOVERY_TOKEN was accepted by the standalone panel and "Regenerate token" could edit the commented line instead of the active one. wp-config.php is now read with the PHP tokenizer and comments are ignored.
* Fixed: standalone direct database deactivation could read commented-out DB credentials.
* Fixed: the IP lockout also blocked the correct token, so anyone sharing the IP (CDN, proxy, office NAT) could lock the administrator out of recovery. The lock now only stops guessing; tokens shorter than 32 characters are refused.
* Fixed: fatal errors were not logged when WordPress' own fatal-error page raised a notice first (for example WordPress 6.0 with WP_DEBUG).
* Fixed: the standalone endpoint ignored WPER_STORAGE_DIR, so its blocks were written to the wrong place.
* Fixed: requesting the MU engine files directly over HTTP could wipe the cached active-plugin list. All MU files now refuse direct access and the copy inside the plugin package is inert.
* Fixed: normal page views no longer write a probe file to disk on every request.
* Added: protected-storage health check, list of emergency-blocked plugins with "Remove block" on Tools > Emergency Recovery, and a notice on the Plugins screen.
* Added: translatable admin interface; "Tested up to" header.
* Improved: the temporary token copy is removed from the database once the token is configured in wp-config.php.

= 1.2.0 =
* Moved recovery state/logs into PHP-guarded protected storage with Apache/IIS defense-in-depth and legacy-state migration.
* Made MU deployment failure-atomic: complete staged engine is verified first and the loader is published last; incomplete packages fail open instead of breaking WordPress.
* Added verified block/unblock persistence and accurate partial-recovery reporting when DB/cache writes are unavailable.
* Throttled active-plugin cache writes and hardened token regeneration with POST/redirect/GET.
* Clean uninstall removes legacy protection/state while preserving shared Pro state when present.

= 1.1.0 =
* Refactored Free/Pro architecture so premium code lives only in a separate add-on.
* Free now focuses on manual recovery, fatal logging and file-backed safe mode.
* Removed automatic isolation/email implementation from the Free package.
* Added a non-aggressive Pro information card on the plugin's own Tools screen.
* Added MU package self-repair/version migration after normal ZIP updates.
* Hardened atomic file writes against partial fwrite() results.

= 1.0.0 =
* Initial recovery release.
