=== WebP Image Converter & Replacer – Convert to WebP, No Duplicates ===
Contributors: coreessentials
Tags: webp, webp converter, convert to webp, image optimization, media library
Requires at least: 5.8
Requires PHP: 7.4
Tested up to: 7.1.2
Stable tag: 1.1.9
License: GPLv2 or later
License URI: https://www.gnu.org/licenses/gpl-2.0.html

Convert images to WebP and replace originals—no duplicates. Save storage and speed up your site. Auto-convert on upload & bulk tools (premium).

== Description ==

**WebP Image Converter & Replacer** converts your images (JPEG, PNG, static GIF) to **WebP**, replaces originals **only when smaller**, and updates database references so your site transparently serves `.webp`. No duplicate files. No front-end bloat. Just **faster pages** and **less disk usage**.

**Why this plugin**
- **No Duplicate Images:** Converts and replaces originals when WebP is smaller, keeping `/uploads` lean.
- **Real Performance Gains:** WebP typically reduces size ~25–35% at similar quality, aiding Core Web Vitals.
- **Safety First:** Never deletes the original unless conversion succeeds **and** the WebP is smaller.
- **Full DB Integration:** All generated sizes are converted; URLs in content/meta are updated to `.webp`.
- **On-Server Only:** Uses PHP **GD** or **Imagick**. No external API, keys, or privacy concerns.
- **Lightweight:** Runs only during conversion—nothing extra on the front end.

**Free features**
- Convert individual images from **Media → Library** (list view).
- **Optimization Overview** with counts: convertible, converted, remaining, skipped, failed, total space saved.
- Smart memory handling to avoid PHP exhaustion on large images.

**Premium features**
- **Auto-convert on upload:** New images become WebP automatically.
- **Bulk convert selected:** Multi-select in Media Library (list view).
- **Bulk convert all:** Library-wide conversion with progress + pause/resume.

> Whether you’re tight on storage or chasing better performance, this is a **safe, effective** way to modernize images.

**IMPORTANT:** Because originals may be deleted, **back up** your `/wp-content/uploads/` and database before converting. Test on staging if possible.

=== How it works ===
1. Plugin checks server support (GD/Imagick with WebP).
2. You convert images (single/bulk/premium auto).
3. If the `.webp` is smaller, the original file is removed and **all DB refs switch to `.webp`**.
4. Your site now serves WebP transparently—no theme changes required.

== Installation ==

1. **Back up** `/wp-content/uploads/` and your database.
2. Install via **Plugins → Add New** (search “WebP Image Converter & Replacer”) or upload the ZIP.
3. Activate.
4. Ensure **Imagick** or **GD** with WebP is available (the plugin will warn if not).
5. Go to **Settings → Media → WebP Image Converter & Replacer** for overview & tools.
6. (Recommended) Install **Phoenix Media Rename** for robust database URL updates (see below).

== Usage ==

**Optimization Overview**
- **Settings → Media** → “WebP Image Converter & Replacer”
- See totals converted/remaining/skipped/failed and **space saved**.

**Convert individual images**
- **Media → Library** (list view) → **Convert to WebP** link under each item.

**Premium**
- **Auto-convert on upload**: toggle under **Settings → Media**.  
- **Bulk convert selected**: select items in Media Library (list view) → bulk action **Convert to WebP**.  
- **Bulk convert all**: **Start Bulk Optimization** with progress + pause/resume.  

**After conversion**
- Verify pages serve `.webp` in browser dev tools.
- Clear any caches/CDN.

== Phoenix Media Rename Integration ==

We integrate with [**Phoenix Media Rename**](https://en-gb.wordpress.org/plugins/phoenix-media-rename/) to safely update **all** database references.

**Why Phoenix?**
- Mature, reliable database URL updates across posts, custom fields, widgets, Customizer, etc.
- Reduces risk vs. “DIY” find/replace logic used by other optimizers.
- Lets this plugin focus on **conversion** while Phoenix handles **DB updates**.

**Flow**
1. Convert to WebP.
2. Phoenix updates URLs from `.jpg/.png/.gif` to `.webp`.
3. Your site serves WebP with no broken links.

== Frequently Asked Questions ==

= Will this plugin delete my images? =
Only if the WebP conversion **succeeds** and the WebP is **smaller**. Otherwise, the original stays. Always keep backups.

= Why are the “space saved” numbers sometimes larger than the single file size? =
WordPress creates multiple thumbnail sizes. Savings are calculated across **the original + all thumbs** vs. **their WebP equivalents**. Thumbnails can outweigh the original file, so total savings can exceed the original’s size.

= What if I uninstall the plugin? =
Your site will continue serving the existing WebP files. Settings are removed; deleted originals are not restored.

= Does this compress or just change format? =
It **converts** format using standard WebP quality defaults (approx. 80–85). No downscaling or custom per-image quality controls in this version.

= Can I change WebP quality? =
Not via UI. Advanced users can use the `webpicr_webp_quality` filter (0–100). Default is 80.

Example (custom plugin or theme `functions.php`):

add_filter( 'webpicr_webp_quality', function( $quality, $source_path, $dest_path ) {
    return 72; // lower = smaller files; higher = better quality
}, 10, 3 );

= AVIF or other formats? =
Currently WebP only. AVIF may be added when browser and server support are broadly stable.

= Animated GIFs? =
We auto-detect animated gifs and mark them as skipped, we do not convert them.

= Multisite support? =
Yes. Network-activate, then run conversions per site. Always back up each site first.

= I hit a timeout/error. =
Use bulk pause/resume, try smaller batches, or raise PHP memory/time limits. The plugin includes memory guards, but extreme libraries may need server tweaks.

= Does it support all browsers? =
Modern browsers do. Legacy/very old browsers may not; if those matter, use a different approach.

= Does conversion update links in posts? =
Phoenix Media Rename does. This plugin does not rewrite post content. Phoenix covers the builders and themes. After Phoenix finishes, the optimized WebP replaces the converted file and thumbnails are not generated a second time.

== Screenshots ==

1. Optimization Overview – library status and space saved  
2. Phoenix Integration Status – compatibility & guidance  
3. Media Library – “Convert to WebP” links  
4. Example Savings – footprint cut after conversion  
5. Premium – bulk convert & auto-convert on upload  
6. Dev Tools – confirming `.webp` on the front end

== Changelog ==

= 1.1.9 =
* Bug fix: JPEG EXIF orientation is applied before the WebP is written, so orientation is respected after conversion
* Bug fix: A WordPress `-rotated` upload no longer makes Phoenix look for a missing `.webp` of the pre-rotation file

= 1.1.8 =
* Bug fix: Conversion generates only the thumbnail sizes the site still has registered. Older sizes listed in the attachment metadata are deleted after Phoenix finishes. Empty name-scaled-WxH leftovers are deleted. Files that were never in the metadata are left on disk
* The saved-bytes total includes those deleted thumbnails minus the new WebP thumbnails. Files that were never in the metadata are not counted
* Bulk convert leaves out wide images whose original-file link is missing, so those JPEGs are not kept beside a new WebP set

= 1.1.7 =
* Bug fix: Wide images whose metadata no longer points at the full original still use that sibling file, so Phoenix rewrites thumbnail URLs and thumbnails are not named name-scaled-WxH

= 1.1.6 =
* Bug fix: Keep Phoenix Media Rename responsible for content URL updates
* Bug fix: After Phoenix renames and generates thumbnails once, swap in the optimized WebP without generating thumbnails again. The second regeneration was stacking another `-scaled` file and a second thumbnail set
* Bug fix: After Phoenix finishes, delete thumbnails still listed in the pre-convert metadata that Phoenix did not remove because those sizes are no longer registered
* Bug fix: Images over the 2560px big-image threshold no longer produce a `-scaled-scaled` file or leave the old `-scaled` JPEG behind. Phoenix was copying the full original because the library file was already named `-scaled`, and thumbnail generation never finished
* Bug fix: Big-image thumbnail URLs in post content are rewritten. Phoenix only searches posts for the attached filename, which is `name-scaled`, so URLs such as `name-640x800.jpg` were skipped

= 1.1.5 =
* Enhancement (Premium): Retry Failed & Skipped — re-queue images locked out of bulk after disk-full or other failures
* Bug fix: Stop showing “all eligible converted” when the queue is empty only because items are failed/skipped

= 1.1.4 =
* Compatibility: Tested with WordPress 7.1
* Bug fix: Set image/webp MIME type after Phoenix-assisted conversions (single, bulk, and auto-convert)
* Bug fix: Restore missing total-savings helper used by timeout status polling
* Bug fix: Align auto-convert success/failure meta keys with the current `_webpicr_*` schema
* Bug fix: Store size meta on Phoenix auto-convert so Recalculate Savings stays accurate
* Bug fix: Use tracked conversion counters instead of stale legacy SQL counts
* Bug fix: Auto-convert in the block editor — defer until WP 7.1 media finalize so image blocks/srcset use WebP URLs (not mixed PNG sizes)
* Security: Require upload/edit caps on conversion status AJAX checks
* Enhancement: Documented `webpicr_webp_quality` filter for advanced quality control

= 1.1.3 =
* Bug fix: "Fix WebP MIME Types" Utility
* Enhancement: MIME type updated after metadata generation

= 1.1.2 =
* Enhancement: Animated GIFs - Better protected & auto-skipped
* Enhancement: Oversized images - Detected & auto-skipped
* Enhancement: Server limits - Respects ImageMagick & WebP constraints
* Bug fix: Large images: 5-min timeout + execution limit
* Bug fix: Timeout handling - Client-side polling for accuracy

= 1.1.1 =
* Bug fix: improved handling of premium activation edge cases.

= 1.1.0 =
* Major: Auto-Convert on Upload (Premium)
* Major: Optimization Overview (real-time stats)
* Enhancement: Phoenix Media Rename integration + auto-install/activation help
* Enhancement: Memory management to prevent PHP exhaustion on large images
* Enhancement: Stats cleanup on media deletion
* Enhancement: Bulk conversion with pause/resume and better progress
* Enhancement: Bulk select conversion via Media Library
* Enhancement: Caching for faster operations
* Enhancement: Better error handling and debug logging
* Enhancement: WPCS compliance and clearer UI messaging
* Bug fix: improved handling of animated GIFs in stats and conversion logic

= 1.0.1 =
* Minor fixes and performance improvements
* Enhanced compatibility testing

= 1.0.0 =
* Initial release: manual bulk conversion, automatic replacement when WebP is smaller, individual conversion links, JPEG/PNG/static GIF support, GD/Imagick required.

== Upgrade Notice ==

= 1.1.9 =
Phone photos keep their orientation when converted. Uploads that WordPress saved as `-rotated` no longer trigger a Phoenix rename error.

= 1.1.7 =
Wide images that lost their original-file link are converted like a normal big upload, so posts get the WebP thumbnail URLs.

= 1.1.6 =
Recommended. Phoenix still updates content links. Conversion no longer generates thumbnails a second time after Phoenix, which was doubling files on disk.

= 1.1.5 =
Recommended: Retry Failed & Skipped for bulk (e.g. after disk full), and clearer messaging when the queue is empty only due to failed/skipped items.

= 1.1.4 =
Recommended update: WordPress 7.1 compatibility, Phoenix MIME fix, block-editor auto-convert URL fix, and conversion status/stats reliability improvements.

= 1.1.0 =
Major update: auto-convert on upload (premium), overview stats, Phoenix integration, reliability & UX improvements.

= 1.1.1 =
* Enhancement: Longer timeouts for slower servers / larger images
* Enhancement: Status polling to prevent failed false positive
* Enhancement: Better animated Gif detection
* Enhancement: Reduced checking for faster conversions