Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions lib/Utils/TimberDynamicResize.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Twig\TwigFilter;
use Timber\ImageHelper;
use Timber\Image\Operation\Resize;
use Timber\Timber;
use Timber\URLHelper;
use WP;
use WP_Rewrite;
Expand Down Expand Up @@ -121,6 +122,9 @@ protected function addHooks()
);
return $twig;
});

add_action('delete_attachment', [$this, 'deleteAttachment']);

if ($this->enabled) {
add_action('after_switch_theme', function (): void {
add_action('shutdown', 'flush_rewrite_rules');
Expand Down Expand Up @@ -431,4 +435,20 @@ public function changeRelativeUploadPath(string $relativeUploadPath): void
flush_rewrite_rules(false);
});
}

/**
* Deletes the auto-generated files for resized images.
*
* @param integer $postId The attachment id.
*/
public function deleteAttachment(int $postId): void
{
if (wp_attachment_is_image($postId)) {
$attachment = Timber::get_post($postId);
if ($fileLoc = $attachment->file_loc()) {
$fileLoc = $this->addImageSeparatorToUploadPath($fileLoc);
ImageHelper::delete_generated_files($fileLoc);
}
}
}
}