=== OrigiSafe — Advanced Image Optimizer (WebP) — Keep Originals Safe ===
Contributors: ipodguy79
Tags: webp, images, media library, optimization,
Requires at least: 5.8
Tested up to: 6.9
Requires PHP: 7.4
Stable tag: 0.0.122
License: GPLv2 or later
License URI: https://www.gnu.org/licenses/gpl-2.0.html

Convert JPG/PNG uploads (and existing library) to WebP, move originals to /uploads/_originals/, and update Media Library metadata - WP serves .webp

== Description ==

Keep Originals Safe, convert JPEG/PNG images to WebP and makes WebP the canonical file. WP returns `.webp` URLs, Keeping originals safely archived.

### How it works (high level)

1. **Convert**
   - For a JPG/PNG attachment, the plugin generates a WebP for the full image and all WordPress-generated sizes.
   - Conversion is done through WordPress’ image editor system (**WP_Image_Editor**), meaning it uses **GD** or **Imagick** (whichever your server supports) with WebP support enabled.

2. **Move originals**
   - The original JPG/PNG files are moved to:
     - `wp-content/uploads/_originals/...`
   - The folder structure mirrors your uploads structure.

3. **Update WordPress to serve WebP**
   - Attachment “file” + metadata are updated so WordPress serves `.webp` as the attachment file.
   - Attachment mime type is updated to `image/webp`.

4. **Optional: Replace old URLs everywhere**
   - If enabled, the plugin can replace old `.jpg/.png` URLs across:
     - post content
     - postmeta
   - Replacement is **serialized-safe** (does not break serialized arrays).

### Background jobs (keeps running after page reload)

Conversion/repair/sweep jobs are designed to run in the background. They keep ticking even if you refresh the page or close the tab.

The plugin uses multiple loopback methods (admin-post / REST / public tick fallback) so it can keep working even on restrictive hosting setups.

### Log + “it looks frozen” note

A rolling “terminal style” log is written to:
- `wp-content/uploads/hsbc-webp-only/logs/webp-only.log`

If the log appears to freeze in the admin page:
- click **Refresh Log**, or
- refresh the page

The job may still be running server-side; refreshing the log/page usually catches up.

== Installation ==

1. Upload the plugin folder to `/wp-content/plugins/`
2. Activate the plugin
3. Go to **Media → WebP Only**

== Usage ==

Go to **Media → WebP Only**.

### Settings

- **Quality**
  - WebP quality for conversion (0–100).
- **Batch size**
  - How many items to process per tick. Smaller batches reduce server load.
- **Replace old .jpg/.png URLs across content + postmeta (serialized-safe)**
  - Recommended if your site content contains hard-coded image URLs.
- **Sweep Include / Exclude**
  - Used by the Sweep tools to control which folders are scanned.

### Conversion Jobs (buttons)

**Start Bulk Convert**
- Converts Media Library attachments (JPG/PNG) in batches.
- Moves originals to `uploads/_originals/`.
- Updates attachment metadata so WordPress serves `.webp`.
- Optional URL replacement (content + postmeta).

**Start Repair/Cleanup**
- Repairs inconsistencies if something was partially converted or moved.
- Useful after restores/migrations or if some attachments have mismatched metadata.
- Optional URL replacement.

**Start Folder Sweep**
- Scans the filesystem for eligible images (based on your Sweep Include/Exclude rules).
- Designed for sites with additional image folders beyond standard uploads.
- Optional URL replacement.

**Stop Job**
- Stops the currently running job.

### Maintenance / Cleanup (buttons)

**Delete duplicate _originals**
- Deletes backup files in `uploads/_originals/` *only when* an identical file already exists in `uploads/`.
- This is a cleanup tool for situations where originals were restored back into uploads and you no longer need the archived duplicate.

**Delete duplicate _webp**
- Removes orphaned WebP cache files in:
  - `uploads/_webp/`
- Only deletes WebP files that no longer match anything in `uploads/` or `uploads/_originals/`.

**Delete duplicate sweep _originals**
- Maintenance cleanup for **non-uploads** roots (folders under `wp-content/`) that contain their own `_originals/` folders.
- It uses your **Sweep Include** list to find those roots.
- Only deletes a file from `<root>/_originals/` when the same file exists at `<root>/` AND the file size matches (safe duplicate).

**Revert Everything**
- Restores original JPG/PNG files from `uploads/_originals/` back into `uploads/`.
- Reverts attachment metadata so WordPress serves the originals again.

### Log controls

**Refresh Log**
- Pulls the latest log tail into the admin screen.

**Reset Log**
- Clears the log file.

== Requirements ==

- WordPress’ image editor must support WebP.
  - **GD** with WebP support, or
  - **Imagick** with WebP support
- If WebP conversion is not supported on your server, conversion/repair/sweep will refuse to run (revert tools can still work).

== Frequently Asked Questions ==

= Does this delete my originals? =
No. Originals are moved into `wp-content/uploads/_originals/` and can be restored with **Revert Everything**.

= Does this create “sidecar” WebP while keeping the JPG/PNG as the main file? =
No. This plugin is designed to make WebP the canonical attachment file in WordPress (while keeping originals archived).

= What does “Replace everywhere” do? =
It replaces old `.jpg/.png` URLs in post content and postmeta. It is serialized-safe to avoid breaking serialized values.

= Why do I see an `_webp` folder? =
The plugin can reuse archived WebP files and also provides a cleanup tool that removes orphaned WebP files that no longer match anything.

= What this plugin does NOT do =
- It does not generate AVIF (WebP only).
- It does not add `<picture>` tags or do theme-level markup changes.
- It does not optimize already-WebP images.
- It does not rewrite external/CDN image URLs outside your WordPress database content/postmeta.

== Changelog ==

= 0.0.122 =
* Initial build: WebP conversion, move originals, bulk/repair/sweep/revert tools, cleanup tools, background runner, and logging.
