Skip to content

Commit 0baf0ee

Browse files
yair-zehaviclaude
andcommitted
Resolve relative paths when importing with --skip-copy
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent af1ad95 commit 0baf0ee

2 files changed

Lines changed: 26 additions & 0 deletions

File tree

‎features/media-import.feature‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,25 @@ Feature: Manage WordPress attachments
138138
And the wp-content/uploads/large-image.jpg file should not exist
139139
And the return code should be 0
140140

141+
Scenario: Import a file with --skip-copy using a path relative to the current directory
142+
Given download:
143+
| path | url |
144+
| {CACHE_DIR}/codeispoetry.png | http://wp-cli.github.io/behat-data/codeispoetry.png |
145+
And I run `wp option update uploads_use_yearmonth_folders 0`
146+
And I run `wp media import {CACHE_DIR}/codeispoetry.png`
147+
148+
When I run `wp media import wp-content/uploads/codeispoetry.png --skip-copy --porcelain`
149+
Then save STDOUT as {ATTACHMENT_ID}
150+
151+
When I run `wp post meta get {ATTACHMENT_ID} _wp_attached_file`
152+
Then STDOUT should be:
153+
"""
154+
codeispoetry.png
155+
"""
156+
157+
When I run `wp eval 'echo wp_get_attachment_url( {ATTACHMENT_ID} );'`
158+
Then STDOUT should match #/wp-content/uploads/codeispoetry\.png$#
159+
141160
Scenario: Import a file and use its filename as the title
142161
Given download:
143162
| path | url |

‎src/Media_Command.php‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -606,6 +606,13 @@ public function import( $args, $assoc_args = array() ) {
606606
++$errors;
607607
continue;
608608
}
609+
// With --skip-copy the path is stored as-is, so a relative one would not resolve against the uploads directory.
610+
if ( Utils\get_flag_value( $assoc_args, 'skip-copy' ) && ! Path::is_absolute( $file ) ) {
611+
$absolute_file = realpath( $file );
612+
if ( false !== $absolute_file ) {
613+
$file = $absolute_file;
614+
}
615+
}
609616
$src_basename = Path::basename( $file );
610617
if ( Utils\get_flag_value( $assoc_args, 'skip-duplicates' ) ) {
611618
$check_basename = $src_basename;

0 commit comments

Comments
 (0)