=== Tasvira – Image Optimizer & Media Cleaner ===
Contributors: mrhamidreza
Tags: webp, resize images, regenerate thumbnails, media cleaner, image optimization
Requires at least: 6.4
Tested up to: 7.1
Requires PHP: 7.4
Stable tag: 1.1.3
License: GPLv2 or later
License URI: https://www.gnu.org/licenses/gpl-2.0.html

Resize oversized images, serve WebP, regenerate thumbnails and clean unused media, all on your own server. No account, no external service.

== Description ==

Tasvira brings the image tools most sites need into one plugin, so you can drop several separate ones.

**Right-sized uploads**

* Images larger than the size you set are scaled down the moment they are uploaded, with the camera orientation applied. The oversized file is not kept.
* Separate limits for images uploaded to the media library, to a post, page or product, and anywhere else (theme logo, header, other plugins).
* Optionally delete the original that WordPress keeps next to its "-scaled" copy of very large images.
* Choose the JPEG quality for scaled uploads and for the thumbnails WordPress creates.
* Optionally turn BMP files, and PNG photos without transparency, into JPEG.

**Shrink the images you already have**

* A background tool scales down existing images that are larger than the limit, keeping the new file only when it is smaller.
* It can also delete the originals kept next to "-scaled" copies, which often frees a lot of disk space.
* Shrink a single image from the media library.

**WebP, made and served locally**

* A WebP copy is saved next to each image and each of its sizes (`photo.jpg` → `photo.jpg.webp`), using the image library already on your server (GD or Imagick).
* A copy is kept only when it is smaller than the original.
* Browsers that support WebP get the WebP copy at the same address; other browsers get the original. Page HTML and page caches are not touched.
* The delivery rules live in the `.htaccess` file of the uploads folder only and are removed when the plugin is deactivated.

**Regenerate thumbnails**

* Recreate the image sizes your theme and plugins use, for the whole library, only featured images, or a single image.
* Create only the missing sizes (fast), or recreate every size after a theme change.
* Optionally delete the files of sizes that are no longer registered, to free space.
* Row actions and bulk actions in the media library list, and a button in the attachment details.

**Media cleaner**

* Scans posts, pages, products, product galleries, page builder data, ACF image fields, widgets, theme settings, category images and the site logo and icon.
* Lists media with no detected references and unowned media files in year/month upload folders. Review each candidate before moving it.
* The scan only reports candidates. Selected items move to an encrypted trash inside uploads, or to `TASVIRA_PRIVATE_DIR` if you define one outside the web root. Files are encrypted before they are published into the trash, so a server that ignores `.htaccess` (for example Nginx) can only return ciphertext.
* Items you want to keep can be excluded from future scans.
* Files added in the last day are never listed, so uploads in progress are safe.

**Light on the server**

* Every long task runs in the background in short, time-limited batches. You can pause, resume and limit the work to certain hours (for example at night), or let it run while the settings page is open.

Tasvira sends no data anywhere and needs no account.

**Server note:** WebP delivery uses `.htaccess` rules, which work on Apache and LiteSpeed. On Nginx, add an equivalent rule to the server configuration; the settings page has a delivery test that tells you whether it works. Media Cleaner trash is different: every file is encrypted before it is stored, so direct access returns ciphertext even on servers that ignore `.htaccess` and `web.config`. The trash directory is also mode 0700 and each file is mode 0600.

== Installation ==

1. Upload and activate the plugin.
2. Go to Media → Image optimization → Settings, check the settings and save.
3. On the WebP tab, press "Start" to convert existing images in the background, then "Test WebP delivery".
4. Use the other tabs to shrink existing images, regenerate thumbnails or find unused media when you need them.
5. Media Cleaner works without configuration: selected files move to an encrypted folder inside uploads (`wp-content/uploads/tasvira-private`). For extra isolation, create a directory writable by PHP outside every public web-server root and alias, then set `TASVIRA_PRIVATE_DIR` to its absolute path in `wp-config.php`. For example: `define( 'TASVIRA_PRIVATE_DIR', '/srv/private/tasvira' );` Use your actual hosting path. The per-site encryption key is stored in the `tasvira_storage_key` option; you can instead define `TASVIRA_STORAGE_KEY` in `wp-config.php` as a 64-character hexadecimal key. Back up the key with the database, because the encrypted trash cannot be restored without it.

== Frequently Asked Questions ==

= Are my original images changed? =

WebP conversion never changes originals. New uploads larger than the limit are scaled down, as with any resize-on-upload plugin. The "Resize & originals" and "Thumbnails" tools change or delete files only when you start them, after you confirm you have a backup.

= Can the media cleaner break my site? =

It lists candidates based on references it can detect. Themes, third-party plugins, custom tables, and external sites may use media the scan misses. Review candidates and keep a backup before moving or deleting anything.

= What happens to media that was already in trash in version 1.1.0 or 1.1.2? =

On the next request Tasvira encrypts each file into the new trash and deletes that public copy only after the encrypted copy is complete. It does not wait for Media Cleaner to be opened. If encryption fails, the existing contents stay in place and an administrator sees an error. Restrict web access to the old directory on your server until encryption succeeds. Files cached by a CDN may also need to be purged.

= What happens if I delete the plugin? =

Tasvira attempts to restore tracked trash items before removing its settings and progress. If a file cannot be restored, resolve the remaining items in Media Cleaner before uninstalling. WebP copies stay on disk.

= What happens if I deactivate the plugin? =

Tasvira removes its delivery rules. Items in encrypted storage remain there until you activate Tasvira and restore them or uninstall the plugin successfully.

= Where does the Media Cleaner trash live? =

By default, Tasvira creates `wp-content/uploads/tasvira-private` and a per-site folder whose name is derived from the encryption key, not from the site URL or database prefix. `.htaccess`, `index.php` and `web.config` deny direct access where the server honors them. Those rules are defense-in-depth. Every trashed file is encrypted with a per-site key before it is placed in the folder, so a server that ignores those files (for example Nginx) and serves the URL returns ciphertext, not the original media. The site folder is mode 0700 and each file is mode 0600, which also blocks a web server running as a different system user. If you define `TASVIRA_PRIVATE_DIR`, Tasvira uses that directory instead and still encrypts. The encryption key is stored in the `tasvira_storage_key` option, or in the `TASVIRA_STORAGE_KEY` constant if you define it in `wp-config.php`. Back up the key with the database; without it, encrypted trash files cannot be restored. Do not change the key while the trash contains files. Tasvira uses the Sodium extension when available, otherwise OpenSSL AES-256-GCM. If neither is available, Media Cleaner reports that encrypted storage cannot start; the other tools keep working.

= Does it work with page caching or a CDN? =

The page HTML does not change, so page caches are fine. A CDN must forward the Accept header (or vary on it) to deliver WebP; otherwise it keeps serving the originals.

== Screenshots ==

1. WebP conversion in the background.
2. Shrinking existing images and deleting kept originals.
3. The media cleaner results.

== Changelog ==

= 1.1.3 =
* Encrypt every Media Cleaner trash file before it is stored, so Nginx and other servers that ignore `.htaccess` can only return ciphertext.
* Encrypt public trash left by 1.1.0 and 1.1.2 on the next request, and delete each public copy only after its encrypted copy is complete.
* Store the trash in an unguessable per-site folder, mode 0700, with each file mode 0600.
* Corrected the IIS `web.config` deny rule.
* Added the optional `TASVIRA_STORAGE_KEY` constant for keeping the encryption key in `wp-config.php`.

= 1.1.2 =
* Removed the unused core `image.php` include from thumbnail regeneration; staging now uses `wp_unique_filename()`.
* Media Cleaner trash now defaults to a protected folder inside uploads, so `TASVIRA_PRIVATE_DIR` is optional.
* Replaced `ABSPATH` and `WP_CONTENT_DIR` in the storage path checks with uploads and public-root checks.
* Added `.htaccess`, `index.php` and `web.config` protection to the default trash folder.

= 1.1.1 =
* Store trashed media outside the web root and keep scanner working data in the database.
* Preserve recovery records when moving, restoring, or deleting files fails.
* Review scan candidates individually and preserve repeated metadata values during scanning.
* Repair missing thumbnail files and stage replacements before publishing them.
* Check both WebP and original-format delivery with TLS verification.

= 1.1.0 =
* New: separate size limits for library, post and other uploads.
* New: delete the originals WordPress keeps next to "-scaled" copies, on upload and for existing images.
* New: shrink existing images in the background or one at a time.
* New: regenerate thumbnails (all, missing only, featured only, single image), with optional removal of unregistered sizes.
* New: media cleaner with a restorable trash.
* New: optimization column, row actions and bulk actions in the media library.

= 1.0.1 =
* Files that are listed in the media library but missing on disk are counted separately, not as errors.
* The delivery test now uses a converted image.

= 1.0.0 =
* First release.
