Skip to content

create a backup of the original image#98

Open
tijmenbruggeman wants to merge 29 commits into
tinify:masterfrom
wcreateweb:feat/backup-original
Open

create a backup of the original image#98
tijmenbruggeman wants to merge 29 commits into
tinify:masterfrom
wcreateweb:feat/backup-original

Conversation

@tijmenbruggeman

@tijmenbruggeman tijmenbruggeman commented Mar 18, 2026

Copy link
Copy Markdown
Collaborator

This will add the option to create a backup of the original uncompressed image.

image

Changes

  • adds tinypng_backup setting
    • considered to add it to preserve_opts but that targets the attributes to preserve on the image, not the image itself.
    • disabled by default
  • before compressing, will optionally create a backup
    • only for the original image
    • when option is enabled
    • uses copy instead of using the filedata, copy uses io instead of memory write

Further development

further development before we release it

  • add ability to restore the original image
  • on deleting the attachment, should also delete the backup

Comment thread src/class-tiny-image.php Outdated

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds an opt-in setting to create a .bak backup of the original (uncompressed) uploaded image before the plugin compresses it, exposing the option in the settings UI and adding unit tests around the new behavior.

Changes:

  • Introduces a new tinypng_backup setting (registered in settings and surfaced in the “Original image” settings view).
  • Adds backup creation logic to Tiny_Image::compress() (backup named <filename>.bak.<ext>).
  • Adds unit tests and test helpers/docs to validate backup behavior.

Reviewed changes

Copilot reviewed 8 out of 9 changed files in this pull request and generated 8 comments.

Show a summary per file
File Description
src/class-tiny-image.php Creates a backup prior to compression and logs whether it was created.
src/class-tiny-settings.php Registers the new setting and adds get_backup_enabled() for feature gating.
src/views/settings-original-image.php Adds a checkbox to enable/disable backups in the settings UI.
test/unit/TinyImageTest.php Adds tests asserting when backups should/shouldn’t be created.
test/unit/TinySettingsAdminTest.php Updates expectations for settings registration to include the new key.
test/helpers/wordpress.php Updates helper docs used by unit tests (image creation/metadata).
test/unit/TinyTestCase.php Adds docblock for WP stubs property.
src/class-tiny-compress.php Improves compress_file parameter documentation.
bin/run-mocks Updates docker compose invocation to include --wait.
Comments suppressed due to low confidence (1)

src/class-tiny-compress.php:103

  • PHPDoc for compress_file is inaccurate: the method returns the $details array, not void. Also, "@param array{ string }" is not a valid shape for a list of mimetypes; consider documenting this as "array" (or "string[]") for $convert_to.
	 * @param string $file path to file
	 * @param array $resize_opts
	 * @param array $preserve_opts
	 * @param array{ string } conversion options
	 * @return void

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/class-tiny-image.php Outdated
Comment thread src/class-tiny-image.php Outdated
Comment thread src/class-tiny-settings.php Outdated
Comment thread test/unit/TinyImageTest.php Outdated
Comment thread test/unit/TinyImageTest.php Outdated
Comment thread test/unit/TinyImageTest.php Outdated
Comment thread test/helpers/wordpress.php Outdated
Comment thread test/helpers/wordpress.php Outdated
Comment thread src/class-tiny-plugin.php Outdated

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 13 out of 13 changed files in this pull request and generated 7 comments.

Comment on lines +1 to +6
# tiny_image_size_before_compression

Action that is done before compressing an single image size.

**Location:** `src/class-tiny-image.php`
**Since:** 3.7.0
Comment on lines +14 to +21
```php
add_filter(
'tiny_image_before_compression',
function ( $id ) {
// notify system of compression
}
);
```
Comment thread docs/hooks/index.md
Comment on lines 7 to +10
- [updated_tiny_postmeta](updated_tiny_postmeta.md) — Triggered when tinify meta data has been updated
- [tiny_image_after_compression](tiny_image_after_compression.md) — Triggered after successful optimization.
- [tiny_image_size_before_compression](tiny_image_size_before_compression.md) — Triggered before optimizing an image size.

Comment thread src/class-tiny-image.php
Comment on lines +200 to 214
/**
* Fires before an image is sent for compression.
*
* @since 3.6.8
*
* @param int The attachment ID
*/
do_action(
'tiny_image_before_compression',
$this->id
);

if ( $this->settings->get_compressor() === null || ! $this->file_type_allowed() ) {
return;
}
Comment thread src/class-tiny-plugin.php
Comment on lines +891 to +895
$tiny_image = new Tiny_Image( $this->settings, $attachment_id );
$original_image = $tiny_image->get_image_size();

$file_path = $original_image->filename;
$upload_dir = wp_upload_dir();
Comment on lines +175 to +186
/**
* Polyfill for `str_starts_with()` function added in PHP 8.0.
*
* Performs a case-sensitive check indicating if
* the haystack begins with needle.
*
* @since 5.9.0
*
* @param string $haystack The string to search in.
* @param string $needle The substring to search for in the `$haystack`.
* @return bool True if `$haystack` starts with `$needle`, otherwise false.
*/
$tiny_plugin = new Tiny_Plugin();
$tiny_plugin->init();

// assert that backup is hooked into `tiny_image_size_before_compression`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants