function testImageDeletion() { $data = array(); $data['size'] = array('width' => 500, 'height' => 300); $upload_dir = wp_upload_dir(); $file = self::copyTestImage('city-museum.jpg'); $data['test_image'] = $upload_dir['url'] . '/city-museum.jpg'; $data['crop'] = 'default'; Timber::compile('assets/image-test.twig', $data); $resized_500_file = TimberImageHelper::get_resize_file_path($data['test_image'], $data['size']['width'], $data['size']['height'], $data['crop']); $data['size'] = array('width' => 520, 'height' => 250); $data['crop'] = 'left'; Timber::compile('assets/image-test.twig', $data); $resized_520_file = TimberImageHelper::get_resize_file_path($data['test_image'], $data['size']['width'], $data['size']['height'], $data['crop']); //make sure it generated the sizes we're expecting $this->assertFileExists($resized_500_file); $this->assertFileExists($resized_520_file); //Now delete the "parent" image TimberImageHelper::delete_resized_files($file); //Have the children been deleted as well? $this->assertFileNotExists($resized_520_file); $this->assertFileNotExists($resized_500_file); }
/** * 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 testImageDeletionByAttachmentLocation() { $post_id = $this->factory->post->create(); $filename = self::copyTestImage('flag.png'); $wp_filetype = wp_check_filetype(basename($filename), null); $attachment = array('post_mime_type' => $wp_filetype['type'], 'post_title' => preg_replace('/\\.[^.]+$/', '', basename($filename)), 'post_content' => '', 'post_status' => 'inherit'); $attach_id = wp_insert_attachment($attachment, $filename, $post_id); $data = array(); $data['size'] = array('width' => 500, 'height' => 300); $upload_dir = wp_upload_dir(); $data['test_image'] = $upload_dir['url'] . '/flag.png'; $data['crop'] = 'default'; Timber::compile('assets/image-test.twig', $data); $resized_500_file = TimberImageHelper::get_resize_file_path($data['test_image'], $data['size']['width'], $data['size']['height'], $data['crop']); $data['size'] = array('width' => 520, 'height' => 250); $data['crop'] = 'left'; Timber::compile('assets/image-test.twig', $data); $resized_520_file = TimberImageHelper::get_resize_file_path($data['test_image'], $data['size']['width'], $data['size']['height'], $data['crop']); //make sure it generated the sizes we're expecting $this->assertFileExists($resized_500_file); $this->assertFileExists($resized_520_file); //Now delete the "parent" image $post = new TimberImage($attach_id); TimberImageHelper::delete_resized_files($post->file_loc); //Have the children been deleted as well? $this->assertFileNotExists($resized_520_file); $this->assertFileNotExists($resized_500_file); }