diff --git a/inc/class-plugin.php b/inc/class-plugin.php index 50c2b015..94d73706 100644 --- a/inc/class-plugin.php +++ b/inc/class-plugin.php @@ -552,22 +552,22 @@ public function set_attachment_files_acl( int $attachment_id, string $acl ) : ?W * @return list 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 */ @@ -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']; } } diff --git a/tests/test-s3-uploads.php b/tests/test-s3-uploads.php index a0b7fd04..22b98b17 100644 --- a/tests/test-s3-uploads.php +++ b/tests/test-s3-uploads.php @@ -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 ); + } + }