=== Tugak Image Converter ===
Contributors: amarantine04
Donate link: https://ko-fi.com/E5E82AYNUK
Tags: webp, image optimization, duplicate images, media library, backup
Requires at least: 6.4
Tested up to: 7.1
Stable tag: 2.6.2
Requires PHP: 8.1
License: GPLv2 or later
License URI: https://www.gnu.org/licenses/gpl-2.0.html

Convert JPG and PNG images to WebP and clean up duplicate uploads—with automatic database link updates, full backups, and one-click rollback.

== Description ==

Facing a huge media library that's slowing down your site, and worried about losing files during cleanup? I ran into that exact wall during a site migration a few months ago, so I wrote this plugin to convert images to WebP and merge duplicates with a proper safety net. Hope it saves you some time! — Amarantine

1. **WebP Conversion:** Converts JPG and PNG images to WebP and updates all image links across your database.
2. **Duplicate Cleanup:** Finds identical uploads or TIF/WebP pairs with the same name, keeps one main file, rewrites all links and attachment IDs to point to it, and safely moves the extra copies to a backup folder.

Nothing is permanently deleted until you explicitly confirm it in a separate step. If anything looks off on your site, you can roll back the changes.

**Plugin tabs at a glance**

- **Conversion** – Configure dimensions, quality, and safety options, run **Analyze only**, **Start optimization**, **Finish cleanup**, **Roll everything back**, or **Delete backups**.
- **Duplicates** – Run **Scan for duplicates**, review found groups, **Merge selected**, **Undo merges**, or **Delete backups of merged copies**.
- **Backups** – Generate a ZIP archive containing a database dump and your original JPG/PNG files (split into manageable parts), create a quick **database-only** backup, and download your exports.
- **Settings** – Choose the interface language, color theme, server load throttling, ZIP part size, and cleanup behavior on uninstall.
- **About** – Plugin info, bug report e-mail, and the support link.

**How WebP conversion works**

- **Complete conversion:** Converts JPG and PNG files from your media library to WebP, including all generated thumbnail sizes, while preserving PNG transparency and photo EXIF orientation.
- **Safe database updates:** Replaces old image URLs in post content, excerpts, custom fields, options, and term meta with the new WebP paths. Serialized data and JSON are updated safely without breaking data structures.
- **Safety first (no broken images):** By default, your original JPG and PNG files stay right where they are after conversion. You can inspect your site first, and only move the originals to a backup folder once you press **Finish cleanup**. Before moving anything, the plugin also scans generated CSS, JS, and JSON files (including Divi and Elementor caches) so no stray links are left behind.
- **Full rollback:** Originals are stored safely in a backup folder. You can download them as a ZIP archive or restore everything with one click.
- **Built for real-world hosting:** Runs in small server-side batches. Closing your browser won't stop a running job, interrupted runs can be resumed, and a busy shared server simply slows the pace rather than crashing the process.
- **Dry-run analysis:** "Analyze only" scans your library without changing a single file—showing total image counts, missing files, estimated space savings, and a breakdown of all files in your uploads folder by extension.
- **Clear results:** Displays exact space saved in both MB and percentage once finished.

**How duplicate cleanup works**

- **Read-only scan:** Compares every image in your media library byte by byte (caching file hashes between scans) and builds a report without modifying anything.
- **Three clear categories:**
  - **Identical files** – Exact byte-for-byte matches. Pre-selected for you by default.
  - **TIF + WebP with the same name** – Pairs a TIF (or TIFF) attachment with a WebP file sharing the same base name (ignoring suffixes like `-2` or `-scaled`, provided the aspect ratio matches). Keeps the WebP and rewrites all links from the TIF to it.
  - **Suspected duplicates** – Files with matching names and pixel dimensions but different byte signatures (such as the same photo saved twice at different compression levels). Left unchecked for your manual review.
- **Smart file selection:** For identical files, the plugin keeps the attachment with the lowest ID whose filename lacks `-1` or `-2` suffixes. For TIF + WebP pairs, the WebP is always kept. One copy from every group always remains untouched.
- **Deep link and ID replacement:** Rewrites all URLs (full size, thumbnails, `-scaled`) and attachment IDs across featured images, `wp-image-123` classes, Gutenberg blocks (image, cover, media-text, file, audio, video, gallery), and `[gallery ids="…"]` shortcodes. Extra copies—including unused ones—are moved to the backup folder and removed from the media library.
- **Reversible merges:** **Undo merges** restores backed-up files and media library entries with their original IDs. Post links remain pointed to the kept image so nothing breaks; if you need to revert post content to its exact prior state, restore your database backup.
- **Built-in guardrails:** The plugin won't let you merge duplicates while a conversion is still running, stops immediately if a target file is missing on disk, and never deletes a copy if a database row couldn't be updated safely. Before you merge, it checks when your last backup was created and offers a one-click **database-only** backup right before proceeding.
- **Note on external caches:** Links cached inside third-party page builders or CDNs aren't modified directly—simply clear your site and CDN caches after merging and check your pages.

**Languages and appearance**

The interface automatically matches your WordPress language (English, Slovak, and Spanish are included, covering all regional Spanish locales such as `es_MX` or `es_EC`). You can also override the language just for this plugin in Settings. Includes three color themes—Light, Rosé, and Dark—plus a "System default" option.

**Free and independent**

Tugak Image Converter is completely free, with no accounts, tracking, or locked features. If it helped tidy up your media library and saved you a few hours of work, you can support its ongoing development with a coffee on Ko-fi.

== Installation ==

1. In your WordPress dashboard, go to **Plugins → Add New**, search for "Tugak Image Converter", and click **Install Now**—or upload the plugin ZIP via **Plugins → Add New → Upload Plugin**.
2. Activate the plugin. **Tugak Image Converter** will appear in your admin menu.
3. Make a site backup first, open the plugin, adjust your width and quality preferences, run **Analyze only** to preview the savings, and click **Start optimization**.

If you previously installed an older copy (in a `tugak_image_converter` folder with underscores), the plugin will display a notice. Simply deactivate and delete the older copy—your settings are stored in the database and will stay intact.

== Frequently Asked Questions ==

= How do I clean up duplicate uploads? =

Head over to the **Duplicates** tab, create a quick database backup, and click **Scan for duplicates**. Identical files are pre-selected for you, and you can review any TIF/WebP pairs or suspected duplicates before hitting **Merge selected**. Once you clear your site cache and check that everything looks good, you can safely delete the backed-up copies.

= I converted my TIF files to WebP by hand and uploaded them to the media library. Can the plugin point my posts to the WebP files? =

Yes. Make sure the WebP files share the same filename as your TIFs, run a scan, and merge the **TIF + WebP with the same name** groups. All links pointing to the TIFs across your posts and pages will be updated to the WebP files, and the heavy TIFs will be moved to the backup folder.

= Which image formats are converted? =

JPG and PNG images in your media library. Files where the resulting WebP wouldn't actually be smaller are skipped automatically (unless you enable the option to convert them anyway). Images wider or taller than 16,383 px are also skipped due to a hard limit of the WebP format itself.

= What does my server need? =

PHP with either the GD or Imagick extension (with WebP support enabled), enough free disk space to hold a backup of your original images (the plugin checks this before starting), and an administrator account.

= Can I undo the conversion? =

Yes, anytime—as long as you haven't deleted the backup of your originals. Just click **Roll everything back** in the plugin, and your media library will return to its previous state.

= What happens to my backups if I uninstall the plugin? =

Removing the plugin always cleans up its settings, unfinished jobs, and locks. Your backed-up original images and ZIP archives are deleted only if you explicitly enabled "Also delete backups when the plugin is removed" in Settings.

= Does the plugin connect to any external server? =

Never during normal use. Your images and data stay strictly on your own server. The only external request happens if you open the **About** tab, where your browser loads the Ko-fi support button image directly from `storage.ko-fi.com` (just like any standard image on the web). No third-party scripts or trackers are loaded.

= Why is the ZIP backup split into parts? =

Many shared hosts time out or block downloads of very large single files. Splitting backups into parts prevents failed downloads. The default part size is 250 MB, and you can adjust it to 100 MB, 250 MB, 500 MB, 1 GB, or 2 GB in Settings.

= Which WordPress and PHP versions have been tested? =

Tested on WordPress 7.1 and PHP 8.3. Requires WordPress 6.4 or newer and PHP 8.1 or newer.

== Changelog ==

= 2.6.2 =
- On the plugin's own screen only the admin notices of other plugins and themes are hidden; WordPress notices (updates, security, site health) stay visible.
- The menu icon and the plugin icon no longer carry embedded metadata.
- Old image addresses are redirected (301) to the new WebP files when the originals were moved to the backup or deleted, so existing links, search results and bookmarks keep working. The redirect code is loaded on the public site only after the plugin has removed old files, and it acts only on requests that would otherwise end in a 404.

= 2.6.1 =
- Family menu merge: the plugin now joins the shared "Tugak" parent menu instead of creating its own top-level entry.
- With two or more Tugak plugins active, the top-level item is renamed to "Tugak Suite" and all family plugins sit under it; alone, the entry keeps the plugin's own name and opens the plugin's page directly.
- The family overview screen can be rendered by any Tugak plugin and lists all active family plugins, this one included.

= 2.6.0 =
- New **Duplicates** tab: read-only scan for identical files, TIF + WebP pairs with the same name and suspected duplicates.
- Merge: one image stays, links (file, all sizes) and attachment IDs (featured image, `wp-image-N`, block ids, gallery) are rewritten, the other copies, including unused ones, go to the backup folder and leave the media library.
- **Undo merges** restores the files and library entries; **Delete backups of merged copies** is a separate step.
- New **Create database backup only** button on the Backups tab. The merge window shows the time of the last backup and offers to create one first.
- The **About** button now sits apart on the right side of the menu.

= 2.5.9 =
- The interface language now follows the WordPress language (site or user). The language switch on the Settings tab stays: choose "Automatic" to follow WordPress, or pick Slovak, English or Spanish for this plugin only. Source texts are English with the text domain `tugak-image-converter`; translations are in the `languages` folder (Slovak, Spanish; every Spanish locale such as es_MX or es_EC uses the one Spanish file). A missing translation shows the English original.
- "Analyze" now also counts every file in the media folder by extension (for example 23 webp, 43 jpg, 12 tif, 34 txt) with the total, the files without an extension and how many are thumbnail sizes. It only reads; the plugin's own backup folders and symbolic links are not counted, and a time limit gives a partial result with a notice.

= 2.5.1 =
- The backup confirmation now says what the estimate counts (images in the media library) and that the backup contains more files, because every thumbnail size and any kept originals are included.

= 2.5.0 =
- Safer default: after a conversion the original files stay in place. A new **Finish cleanup** button moves them to the backup once you have checked the site (rollback stays possible). The old behavior is still available as "Move to backup right away".
- Before the originals are moved, generated CSS, JS and JSON files (uploads folder and Divi's et-cache) are checked for links to the old images; if any are found the originals are not moved and the files are named in the log.
- "Delete backups" no longer touches originals that were kept in place.
- The final message and the status box now say clearly that disk space is freed only after **Finish cleanup**.

= 2.4.3 =
- Server load no longer stops a run. On busy shared hosting the plugin only slows down and says so once in the log.
- New "Cancel job" button: an unfinished job can no longer block starting a new one. The old "Cancel" button is now called "Pause", which is what it did.
- Confirmation windows now use the plugin's own design (all color themes, usable with the keyboard) instead of the browser's.

= 2.4.2 =
- The About tab has a "Bug reports" e-mail link with the plugin, WordPress and PHP versions pre-filled.
- The support button is now the official Ko-fi button (blue for Light and Rosé, purple for Dark). Its image is loaded from Ko-fi when the About tab is opened; no scripts are loaded.

= 2.4.1 =
- The support button was restyled for every color theme.

= 2.4.0 =
- The support button is now a plain styled link (no image, no scripts).
- Minimum requirements raised to PHP 8.1 and WordPress 6.4.

= 2.3.9 =
- The menu icon sits lower so it lines up with the other menu items.
- Added the GPL license declaration and the WordPress.org readme.
- Reworded the support text.

= 2.3.8 =
- Added a support card (Ko-fi) to the About tab and a link on the Plugins screen. No third-party scripts are loaded.
- Revised the Slovak, English and Spanish texts.

= 2.3.7 =
- New single-color admin menu icon that follows the admin color scheme.

= 2.3.6 =
- The plugin folder is now `tugak-image-converter`; an update replaces the existing copy.
- Notice when a second copy of the plugin is installed.

= 2.3.5 =
- Fixed "WebP output is invalid" for indexed (palette) PNG images.
- Images over 16,383 px on a side are skipped with an explanation instead of failing.
- "Download all parts" button for split backups.

= 2.0 - 2.3.4 =
- Rewritten engine with server-side steps, verification pass and rollback, ZIP backups in parts, analysis mode, languages and color themes.

== Upgrade Notice ==

= 2.6.0 =
New Duplicates tab: find identical uploads and TIF/WebP pairs, merge them into one image with all links rewritten, with a backup and undo. Make a database backup before merging (Backups tab, **Create database backup only**).

= 2.5.9 =
The language now follows WordPress by default (the switch on the Settings tab still works). The analysis also lists all files in the media folder by type.

= 2.5.1 =
Clearer wording of the backup estimate.

= 2.5.0 =
Originals now stay in place after a conversion until you press "Finish cleanup", so a stale cache can no longer show broken images.

= 2.4.3 =
Fixes a run that could wait forever on a busy shared server and adds a "Cancel job" button.

= 2.4.2 =
The About tab now shows the official Ko-fi button, loaded from Ko-fi's servers.

= 2.4.1 =
Cosmetic update of the support button. Requires PHP 8.1 or newer.

= 2.4.0 =
Requires PHP 8.1 or newer. No change to conversion behavior.
