Skip to content

Commit 8fc573a

Browse files
Copilotswissspidy
andcommitted
Address PR review feedback: fix noun, temp file cleanup, error handling, skip-delete test
Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
1 parent c10d8b1 commit 8fc573a

2 files changed

Lines changed: 31 additions & 10 deletions

File tree

features/media-replace.feature

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ Feature: Replace WordPress attachment files
2020
"""
2121
And STDOUT should contain:
2222
"""
23-
Success: Replaced 1 of 1 images.
23+
Success: Replaced 1 of 1 attachments.
2424
"""
2525

2626
Scenario: Replace an attachment file from a URL
@@ -39,7 +39,7 @@ Feature: Replace WordPress attachment files
3939
"""
4040
And STDOUT should contain:
4141
"""
42-
Success: Replaced 1 of 1 images.
42+
Success: Replaced 1 of 1 attachments.
4343
"""
4444

4545
Scenario: Replace an attachment file and output only the attachment ID in porcelain mode
@@ -71,7 +71,7 @@ Feature: Replace WordPress attachment files
7171
When I run `wp media replace {ATTACHMENT_ID} {CACHE_DIR}/canola.jpg`
7272
Then STDOUT should contain:
7373
"""
74-
Success: Replaced 1 of 1 images.
74+
Success: Replaced 1 of 1 attachments.
7575
"""
7676

7777
When I run `wp post get {ATTACHMENT_ID} --field=post_title`
@@ -106,3 +106,24 @@ Feature: Replace WordPress attachment files
106106
Error: Invalid attachment ID 999999.
107107
"""
108108
And the return code should be 1
109+
110+
Scenario: Skip deletion of old thumbnails when --skip-delete flag is used
111+
Given download:
112+
| path | url |
113+
| {CACHE_DIR}/large-image.jpg | http://wp-cli.org/behat-data/large-image.jpg |
114+
| {CACHE_DIR}/canola.jpg | http://wp-cli.org/behat-data/canola.jpg |
115+
And I run `wp option update uploads_use_yearmonth_folders 0`
116+
117+
When I run `wp media import {CACHE_DIR}/large-image.jpg --porcelain`
118+
Then save STDOUT as {ATTACHMENT_ID}
119+
120+
When I run `wp post meta get {ATTACHMENT_ID} _wp_attached_file`
121+
Then save STDOUT as {OLD_FILE}
122+
123+
When I run `wp media replace {ATTACHMENT_ID} {CACHE_DIR}/canola.jpg --skip-delete`
124+
Then STDOUT should contain:
125+
"""
126+
Success: Replaced 1 of 1 attachments.
127+
"""
128+
129+
Then the wp-content/uploads/{OLD_FILE} file should exist

src/Media_Command.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -656,11 +656,12 @@ public function replace( $args, $assoc_args = array() ) {
656656
}
657657

658658
// For big-image scaling (WP 5.3+), delete the original image if present in metadata.
659-
if ( ! empty( $old_metadata['original_image'] ) && ! empty( $old_metadata['file'] ) ) {
659+
$original_image = isset( $old_metadata['original_image'] ) ? (string) $old_metadata['original_image'] : '';
660+
if ( '' !== $original_image && ! empty( $old_metadata['file'] ) ) {
660661
$uploads = wp_get_upload_dir();
661662
if ( ! empty( $uploads['basedir'] ) ) {
662-
$dirname = dirname( $old_metadata['file'] );
663-
$original_image_rel = ( '.' === $dirname || '/' === $dirname ) ? $old_metadata['original_image'] : $dirname . '/' . $old_metadata['original_image'];
663+
$dirname = dirname( $old_metadata['file'] );
664+
$original_image_rel = ( '.' === $dirname || '/' === $dirname ) ? $original_image : $dirname . '/' . $original_image;
664665
$original_image_abspath = $uploads['basedir'] . '/' . $original_image_rel;
665666
if ( $original_image_abspath !== $new_file_path && file_exists( $original_image_abspath ) ) {
666667
@unlink( $original_image_abspath );
@@ -677,10 +678,9 @@ public function replace( $args, $assoc_args = array() ) {
677678
),
678679
true
679680
);
680-
if ( false === $updated || is_wp_error( $updated ) ) {
681-
$message = is_wp_error( $updated ) ? $updated->get_error_message() : 'Unknown error.';
681+
if ( is_wp_error( $updated ) ) {
682682
WP_CLI::warning(
683-
sprintf( 'Failed to update MIME type for attachment %d: %s', $attachment_id, $message )
683+
sprintf( 'Failed to update MIME type for attachment %d: %s', $attachment_id, $updated->get_error_message() )
684684
);
685685
}
686686

@@ -694,7 +694,7 @@ public function replace( $args, $assoc_args = array() ) {
694694
} else {
695695
WP_CLI::warning(
696696
sprintf(
697-
"Failed to generate new attachment metadata for attachment ID %d. Existing metadata has been preserved.",
697+
'Failed to generate new attachment metadata for attachment ID %d. Existing metadata has been preserved.',
698698
$attachment_id
699699
)
700700
);

0 commit comments

Comments
 (0)