From 0baf0ee9ccaeaf4169a0cbb1fdd0f736d021bbc4 Mon Sep 17 00:00:00 2001 From: Yair Zehavi Date: Sun, 11 Oct 2026 16:23:28 +0200 Subject: [PATCH] Resolve relative paths when importing with --skip-copy Co-Authored-By: Claude Opus 5.5 --- features/media-import.feature | 19 +++++++++++++++++++ src/Media_Command.php | 7 +++++++ 2 files changed, 26 insertions(+) diff --git a/features/media-import.feature b/features/media-import.feature index 1c800f26..f3e36cc0 100644 --- a/features/media-import.feature +++ b/features/media-import.feature @@ -138,6 +138,25 @@ Feature: Manage WordPress attachments And the wp-content/uploads/large-image.jpg file should not exist And the return code should be 0 + Scenario: Import a file with --skip-copy using a path relative to the current directory + Given download: + | path | url | + | {CACHE_DIR}/codeispoetry.png | http://wp-cli.github.io/behat-data/codeispoetry.png | + And I run `wp option update uploads_use_yearmonth_folders 0` + And I run `wp media import {CACHE_DIR}/codeispoetry.png` + + When I run `wp media import wp-content/uploads/codeispoetry.png --skip-copy --porcelain` + Then save STDOUT as {ATTACHMENT_ID} + + When I run `wp post meta get {ATTACHMENT_ID} _wp_attached_file` + Then STDOUT should be: + """ + codeispoetry.png + """ + + When I run `wp eval 'echo wp_get_attachment_url( {ATTACHMENT_ID} );'` + Then STDOUT should match #/wp-content/uploads/codeispoetry\.png$# + Scenario: Import a file and use its filename as the title Given download: | path | url | diff --git a/src/Media_Command.php b/src/Media_Command.php index 4c3391b7..c526233b 100644 --- a/src/Media_Command.php +++ b/src/Media_Command.php @@ -606,6 +606,13 @@ public function import( $args, $assoc_args = array() ) { ++$errors; continue; } + // With --skip-copy the path is stored as-is, so a relative one would not resolve against the uploads directory. + if ( Utils\get_flag_value( $assoc_args, 'skip-copy' ) && ! Path::is_absolute( $file ) ) { + $absolute_file = realpath( $file ); + if ( false !== $absolute_file ) { + $file = $absolute_file; + } + } $src_basename = Path::basename( $file ); if ( Utils\get_flag_value( $assoc_args, 'skip-duplicates' ) ) { $check_basename = $src_basename;