Skip to content

Commit 8e11b25

Browse files
change hook params and only trigger once per image.
1 parent 2dc0ccd commit 8e11b25

4 files changed

Lines changed: 72 additions & 41 deletions

File tree

docs/hooks/tiny_image_size_before_compression.md renamed to docs/hooks/tiny_image_before_compression.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,13 @@ Action that is done before compressing an single image size.
88
## Arguments
99

1010
1. `int $attachment_id` - The attachment ID.
11-
2. `int|string $size_name` - The image size name. 0 for the original.
12-
3. `string $filepath` - The file path to the image being compressed.
1311

1412
## Example
1513

1614
```php
1715
add_filter(
18-
'tiny_image_size_before_compression',
19-
function ( $attachment_id, $size_name, $filename ) {
16+
'tiny_image_before_compression',
17+
function ( $id ) {
2018
// notify system of compression
2119
}
2220
);

src/class-tiny-image.php

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,19 @@ public function compress() {
196196
'name' => $this->name,
197197
)
198198
);
199+
200+
/**
201+
* Fires before an image is sent for compression.
202+
*
203+
* @since 3.6.8
204+
*
205+
* @param int The attachment ID
206+
*/
207+
do_action(
208+
'tiny_image_before_compression',
209+
$this->id
210+
);
211+
199212
if ( $this->settings->get_compressor() === null || ! $this->file_type_allowed() ) {
200213
return;
201214
}
@@ -222,22 +235,6 @@ public function compress() {
222235
$size->add_tiny_meta_start();
223236
$this->update_tiny_post_meta();
224237

225-
/**
226-
* Fires before an image size is sent for compression.
227-
*
228-
* @since 3.6.8
229-
*
230-
* @param int $attachment_id The attachment ID.
231-
* @param int|string $size_name The image size name. 0 for the original.
232-
* @param string $filepath The file path to the image being compressed.
233-
*/
234-
do_action(
235-
'tiny_image_size_before_compression',
236-
$this->id,
237-
$size_name,
238-
$size->filename
239-
);
240-
241238
$resize = $this->settings->get_resize_options( $size_name );
242239
$preserve = $this->settings->get_preserve_options( $size_name );
243240
Tiny_Logger::debug(

src/class-tiny-plugin.php

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,10 @@ public function init() {
6868
add_action( 'delete_attachment', $this->get_method( 'clean_attachment' ), 10, 2 );
6969

7070
add_action(
71-
'tiny_image_size_before_compression',
72-
$this->get_method( 'backup_image_size' ),
71+
'tiny_image_before_compression',
72+
$this->get_method( 'backup_original_image' ),
7373
10,
74-
3
74+
1
7575
);
7676

7777
load_plugin_textdomain(
@@ -871,38 +871,40 @@ public function clean_attachment( $post_id ) {
871871
/**
872872
* Creates a backup of an image size before compression.
873873
*
874-
* Hooked to the `tiny_image_size_before_compression` action. Only creates
874+
* Hooked to the `tiny_image_before_compression` action. Only creates
875875
* a backup for the original image size when the backup setting is enabled.
876876
* The backup is stored under {upload_dir}/tinify_backup/, preserving the
877877
* original path structure relative to the uploads base directory.
878878
*
879879
* @since 3.6.8
880880
*
881-
* @param int $image_id The attachment ID.
882-
* @param int|string $size_name The image size name. 0 for the original.
883-
* @param string $filepath The file path to the image to be backed up.
884-
* @return bool return true on backup created
881+
* @param int $attachment_id The ID of the attachment
882+
* @return bool return true on backup created
885883
*/
886-
public function backup_image_size( $image_id, $size_name, $filepath ) {
887-
if ( ! Tiny_Image::is_original( $size_name ) ) {
888-
return false;
889-
}
890-
884+
public function backup_original_image( $attachment_id ) {
891885
if ( ! $this->settings->get_backup_enabled() ) {
892886
return false;
893887
}
894888

889+
$tiny_image = new Tiny_Image( $this->settings, $attachment_id );
890+
$original_image = $tiny_image->get_image_size();
891+
895892
$upload_dir = wp_upload_dir();
896893
$upload_base = trailingslashit( $upload_dir['basedir'] );
897-
$relative_path = ltrim( str_replace( $upload_base, '', $filepath ), '/' );
894+
$relative_path = ltrim( str_replace( $upload_base, '', $original_image->filename ), '/' );
898895
$backup_file = $upload_base . 'tinify_backup/' . $relative_path;
899-
$backup_dir = dirname( $backup_file );
896+
897+
if ( file_exists( $backup_file ) ) {
898+
return false;
899+
}
900+
901+
$backup_dir = dirname( $backup_file );
900902

901903
if ( ! wp_mkdir_p( $backup_dir ) ) {
902904
return false;
903905
}
904906

905-
return copy( $filepath, $backup_file );
907+
return copy( $original_image->filename, $backup_file );
906908
}
907909

908910
public static function request_review() {

test/unit/TinyPluginTest.php

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -503,7 +503,7 @@ public function test_init_adds_backup_image_size_action() {
503503
$tiny_plugin->init();
504504

505505
// assert that backup is hooked into `tiny_image_size_before_compression`
506-
WordPressStubs::assertHook('tiny_image_size_before_compression', array($tiny_plugin, 'backup_image_size'));
506+
WordPressStubs::assertHook('tiny_image_before_compression', array($tiny_plugin, 'backup_original_image'));
507507
}
508508

509509
public function test_will_copy_original_file_on_backup() {
@@ -520,15 +520,49 @@ public function test_will_copy_original_file_on_backup() {
520520
$mock_settings->method('get_backup_enabled')->willReturn(true);
521521
$settings_prop->setValue($tiny_plugin, $mock_settings);
522522

523-
$tiny_plugin->backup_image_size(1, 0, $og_file_path);
523+
$this->wp->stub('wp_get_attachment_metadata', function ($i) {
524+
return array(
525+
'width' => 1256,
526+
'height' => 1256,
527+
'file' => '2026/04/testfile.png',
528+
'sizes' => array(),
529+
);
530+
});
531+
532+
$backup_made = $tiny_plugin->backup_original_image(1);
524533

534+
assertTrue($backup_made, 'expected backup to be made');
525535
assertTrue(file_exists($expected_backup), 'expected backup to be created');
526536
}
527537

528-
public function test_when_not_original_will_not_backup() {
538+
public function test_no_backup_when_backup_exists() {
539+
$this->wp->createImage( 37857, '2026/04', 'testfile.png' );
540+
$og_file_path = $this->vfs->url() . '/wp-content/uploads/2026/04/testfile.png';
541+
$expected_backup = $this->vfs->url() . '/wp-content/uploads/tinify_backup/2026/04/testfile.png';
542+
529543
$tiny_plugin = new Tiny_Plugin();
530-
$created = $tiny_plugin->backup_image_size(1, 'thumbnail', 'filepath');
531544

532-
assertFalse($created, 'expected backup not te be created');
545+
$ref = new \ReflectionClass($tiny_plugin);
546+
$settings_prop = $ref->getProperty('settings');
547+
$settings_prop->setAccessible(true);
548+
$mock_settings = $this->createMock(Tiny_Settings::class);
549+
$mock_settings->method('get_backup_enabled')->willReturn(true);
550+
$settings_prop->setValue($tiny_plugin, $mock_settings);
551+
552+
$this->wp->stub('wp_get_attachment_metadata', function ($i) {
553+
return array(
554+
'width' => 1256,
555+
'height' => 1256,
556+
'file' => '2026/04/testfile.png',
557+
'sizes' => array(),
558+
);
559+
});
560+
561+
$this->wp->createImage( 37857, 'tinify_backup/2026/04', 'testfile.png' );
562+
563+
$backup_made = $tiny_plugin->backup_original_image(1);
564+
565+
assertFalse($backup_made, 'expected backup not to be made');
566+
assertTrue(file_exists($expected_backup), 'expected backup to exist');
533567
}
534568
}

0 commit comments

Comments
 (0)