Skip to content
Merged
Show file tree
Hide file tree
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
8 changes: 4 additions & 4 deletions inc/class-plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -552,22 +552,22 @@ public function set_attachment_files_acl( int $attachment_id, string $acl ) : ?W
* @return list<string> Array of all full paths to the attachment's files.
*/
public static function get_attachment_files( int $attachment_id ) : array {
$uploadpath = wp_get_upload_dir();
/** @var string */
$main_file = get_attached_file( $attachment_id );
$main_file_directory = dirname( $main_file );
$files = [ $main_file ];

$meta = wp_get_attachment_metadata( $attachment_id );
if ( isset( $meta['sizes'] ) ) {
foreach ( $meta['sizes'] as $size => $sizeinfo ) {
$files[] = $uploadpath['basedir'] . $sizeinfo['file'];
$files[] = $main_file_directory . '/' . $sizeinfo['file'];
}
}

/** @var string|false */
$original_image = get_post_meta( $attachment_id, 'original_image', true );
if ( $original_image ) {
$files[] = $uploadpath['basedir'] . $original_image;
$files[] = $main_file_directory . '/' . $original_image;
}

/** @var array<string,array{file: string}> */
Expand All @@ -576,7 +576,7 @@ public static function get_attachment_files( int $attachment_id ) : array {
foreach ( $backup_sizes as $size => $sizeinfo ) {
// Backup sizes only store the backup filename, which is relative to the
// main attached file, unlike the metadata sizes array.
$files[] = path_join( dirname( $main_file ), $sizeinfo['file'] );
$files[] = $main_file_directory . '/' . $sizeinfo['file'];
}
}

Expand Down
57 changes: 57 additions & 0 deletions tests/test-s3-uploads.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,4 +183,61 @@ function test_wp_unique_filename() {
$this->assertEquals( 'my-new-file.jpg', $filename );
}

function test_get_attachment_files() {
S3_Uploads\Plugin::get_instance()->setup();
$upload_dir = wp_upload_dir();
copy( dirname( __FILE__ ) . '/data/sunflower.jpg', $upload_dir['path'] . '/sunflower.jpg' );
$test_file = $upload_dir['path'] . '/sunflower.jpg';
$attachment_id = self::factory()->attachment->create_object( $test_file, 0, array(
'post_mime_type' => 'image/jpeg',
'post_excerpt' => 'A sample caption',
) );

$files = S3_Uploads\Plugin::get_instance()->get_attachment_files( $attachment_id );
$this->assertIsArray( $files );
$this->assertNotEmpty( $files );
$this->assertContains( $upload_dir['path'] . '/sunflower.jpg', $files );
}

function test_get_attachment_files_intermediate_sizes() {
S3_Uploads\Plugin::get_instance()->setup();
$upload_dir = wp_upload_dir();
copy( dirname( __FILE__ ) . '/data/sunflower.jpg', $upload_dir['path'] . '/sunflower.jpg' );
$test_file = $upload_dir['path'] . '/sunflower.jpg';
$attachment_id = self::factory()->attachment->create_object( $test_file, 0, array(
'post_mime_type' => 'image/jpeg',
'post_excerpt' => 'A sample caption',
) );

// Generate metadata to create intermediate sizes
wp_generate_attachment_metadata( $attachment_id, $test_file );

$files = S3_Uploads\Plugin::get_instance()->get_attachment_files( $attachment_id );
$this->assertIsArray( $files );
$this->assertNotEmpty( $files );
$this->assertContains( $upload_dir['path'] . '/sunflower-300x196.jpg', $files );
$this->assertContains( $upload_dir['path'] . '/sunflower-150x150.jpg', $files );
}

function test_get_attachment_files_intermediate_sizes_in_subdir() {
S3_Uploads\Plugin::get_instance()->setup();
$upload_dir = wp_upload_dir();
copy( dirname( __FILE__ ) . '/data/sunflower.jpg', $upload_dir['path'] . '/a/sub/dir/sunflower.jpg' );
$test_file = $upload_dir['path'] . '/a/sub/dir/sunflower.jpg';
$attachment_id = self::factory()->attachment->create_object( $test_file, 0, array(
'post_mime_type' => 'image/jpeg',
'post_excerpt' => 'A sample caption',
) );

// Generate metadata to create intermediate sizes
wp_generate_attachment_metadata( $attachment_id, $test_file );

$files = S3_Uploads\Plugin::get_instance()->get_attachment_files( $attachment_id );
$this->assertIsArray( $files );
$this->assertNotEmpty( $files );
$this->assertContains( $upload_dir['path'] . '/a/sub/dir/sunflower.jpg', $files );
$this->assertContains( $upload_dir['path'] . '/a/sub/dir/sunflower-300x196.jpg', $files );
$this->assertContains( $upload_dir['path'] . '/a/sub/dir/sunflower-150x150.jpg', $files );
}

}
Loading