/** * Deletes all resized versions of an image when the source is deleted */ static function add_actions() { add_action('delete_post', function ($post_id) { $post = get_post($post_id); $image_types = array('image/jpeg', 'image/png', 'image/gif', 'image/jpg'); if ($post->post_type == 'attachment' && in_array($post->post_mime_type, $image_types)) { $attachment = new TimberImage($post_id); TimberImageHelper::delete_resized_files($attachment->file_loc); TimberImageHelper::delete_letterboxed_files($attachment->file_loc); } }); }
function testLetterboxImageDeletion() { $data = array(); $file = self::copyTestImage('city-museum.jpg'); $upload_dir = wp_upload_dir(); $data['test_image'] = $upload_dir['url'] . '/city-museum.jpg'; $new_file = TimberImageHelper::letterbox($data['test_image'], 500, 500, '#00FF00'); $letterboxed_file = TimberImageHelper::get_letterbox_file_path($data['test_image'], 500, 500, '#00FF00'); $this->assertFileExists($letterboxed_file); //Now delete the "parent" image TimberImageHelper::delete_letterboxed_files($file); //Have the children been deleted as well? $this->assertFileNotExists($letterboxed_file); }