# Square Thumbnails Plugin - Modifications

## Purpose
These modifications prevent the Square Thumbnails plugin from modifying original images, add auto-save functionality to the settings page, and improve file management for better handling of scaled images and format changes.

## Changes Made

### 1. `admin/class-square-thumbnails-admin.php`
- **Line 614-645**: Added centralized `get_source_for_regeneration()` function to handle original vs scaled images
- **Line 400-438**: Added `cleanup_old_square_thumbnails()` function to remove old files when format changes
- **Line 179-198**: Fixed path duplication issue in `getPaths()` function
- **Line 299-389**: Added new `getSquareSizes()` function for proper square size calculation
- **Line 470-494**: Fixed dimension calculation to use requested thumbnail size instead of original
- **Line 578-583**: Fixed image resampling to use actual source dimensions
- **Line 853-876**: Added format change detection and cleanup before processing
- **Line 857-872**: Commented out the code that applies square transformation to original images
- **Line 985-987**: Removed the code that saves the `tooriginal` option
- **Line 623-637**: Added clear suffixes to files (`-sqt` and `-sqt-transparent`) for better identification
- **Line 1382-1428**: Added `cleanup_on_attachment_delete()` to remove orphaned files

### 2. `admin/partials/square-thumbnails-admin-display.php`
- **Line 96-103**: Completely removed the "Apply to Original" option from the interface
- **Line 39**: Updated the feature list to reflect that only thumbnails are processed
- **Line 183**: Added `style="display:none;"` to background color row
- **Line 351-354**: Added notice that settings are saved automatically

### 3. `admin/js/square-thumbnails-admin.js`
- **Line 39-40**: Removed the `tooriginal` parameter from AJAX requests and added `force` parameter
- **Line 13-67**: Added auto-save functionality that saves settings when changed
- **Line 70-81**: Added event listeners for automatic saving on all form inputs
- **Line 166-177**: Fixed background type visibility initialization
- **Line 173-199**: Modified quick selector buttons to auto-save after selection

### 4. `admin/css/square-thumbnails-admin.css`
- **Line 98-115**: Added CSS for auto-save indicators and animations

### 5. `includes/class-square-thumbnails.php`
- **Line 168-169**: Added hook for `delete_attachment` to clean up files when images are deleted

## Recent Bug Fixes (January 2025)

### Path Duplication Fix
- Fixed issue where file paths were duplicated (e.g., `/uploads//uploads/`)
- Now properly handles both relative and absolute paths in metadata

### Square Sizing Fix
- Fixed issue where all thumbnails were being created at 1200x1200 regardless of requested size
- Now properly creates squares at the requested thumbnail dimensions
- Improved scaling algorithm to fit images properly within target square

### Image Rotation Fix
- Fixed issue where images were being incorrectly rotated or cropped
- Now uses actual source image dimensions for proper resampling

### Suffix Duplication Fix
- Fixed issue where -sqt suffix was being added multiple times on regeneration
- Added cleanup function to remove old square thumbnails before creating new ones
- Improved pattern matching to handle files with multiple suffixes
- Enhanced deletion cleanup to properly remove all variants

## New Features

### 1. Centralized Source Management
- Consistent handling of original vs scaled images
- WordPress 5.3+ scaled image support
- Prevents duplicate processing issues

### 2. File Format Management
- Clear suffixes for identification: `-sqt` for regular, `-sqt-transparent` for transparent
- Automatic cleanup when switching between transparent and color backgrounds
- Proper conversion from JPEG to PNG when using transparent background

### 3. Auto-Save Functionality
- Settings are saved automatically when any option is changed
- Checkbox and radio button changes save immediately
- Number and text inputs save 1 second after user stops typing
- Color picker saves 500ms after color change
- Visual indicators show when settings are being saved
- Quick selector buttons automatically save after selection

### 4. Enhanced Cleanup
- Automatic removal of old format files when settings change
- Cleanup on attachment deletion to prevent orphaned files
- Support for scaled image cleanup

## Technical Details

### Original Image Protection
The plugin previously had an option to apply square formatting to original images. This behavior has been completely removed to ensure that source images remain untouched, and only generated thumbnails are made square.

### File Management Strategy
1. Use clear suffixes for easy identification
2. Clean up old files when format changes
3. Handle WordPress scaled images properly
4. Remove all associated files when attachment is deleted

### Square Sizing Algorithm
1. Determine requested thumbnail size from metadata
2. Calculate target square dimensions
3. Load original image for best quality
4. Scale image to fit within square while maintaining aspect ratio
5. Center image within square canvas

## Impact

- Original uploaded images will always remain in their original aspect ratio
- Only registered thumbnail sizes will be made square
- No duplicate or orphaned files
- Settings are saved automatically without needing to click "Save Settings"
- Proper handling of WordPress 5.3+ scaled images
- Clean migration between transparent and color backgrounds
- Correct square dimensions for each thumbnail size

## Date Modified
January 2025

## Important Note
If you update the plugin in the future, these modifications will be lost. Keep a backup of these files or reapply the changes after updating.

## Complete List of Changes
1. Removed UI checkbox for "Apply to Original"
2. Removed JavaScript code that handles this option
3. Removed PHP code that saves this option
4. Commented out the actual processing code for original images
5. Updated feature list to reflect new behavior
6. Added auto-save functionality to all settings
7. Added visual indicators for auto-saving
8. Modified quick selector buttons to include auto-save
9. Added centralized source file management
10. Added automatic cleanup for format changes
11. Added cleanup on attachment deletion
12. Added clear file suffixes for identification
13. Fixed background color field visibility issues
14. Fixed path duplication in getPaths function
15. Fixed square sizing to use correct dimensions
16. Fixed image rotation and cropping issues
17. Improved scaling algorithm for proper fit
18. Fixed suffix duplication on regeneration
19. Added cleanup_existing_square_thumbnails function
20. Enhanced deletion cleanup to handle multiple suffixes