Skip to content
Open
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
47 changes: 47 additions & 0 deletions features/media-import.feature
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,53 @@ 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$#

When I run `wp eval 'var_export( file_exists( get_attached_file( {ATTACHMENT_ID} ) ) );'`
Then STDOUT should be:
"""
true
"""

Scenario: Import a file with --skip-copy using a relative path through a symlinked uploads 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 `mkdir shared-uploads`
And I run `cp {CACHE_DIR}/codeispoetry.png shared-uploads/codeispoetry.png`
And I run `rm -rf wp-content/uploads`
And I run `ln -s ../shared-uploads wp-content/uploads`

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$#
Comment on lines +157 to +186

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This step passes on main too, so it does not test the fix. A check on get_attached_file() fails on main (false) and passes with this PR:

Suggested change
When I run `wp eval 'echo wp_get_attachment_url( {ATTACHMENT_ID} );'`
Then STDOUT should match #/wp-content/uploads/codeispoetry\.png$#
When I run `wp eval 'var_export( file_exists( get_attached_file( {ATTACHMENT_ID} ) ) );'`
Then STDOUT should be:
"""
true
"""


Scenario: Import a file and use its filename as the title
Given download:
| path | url |
Expand Down
34 changes: 34 additions & 0 deletions src/Media_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -606,6 +606,10 @@ 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 ) ) {
$file = $this->get_absolute_path( $file );
}
$src_basename = Path::basename( $file );
if ( Utils\get_flag_value( $assoc_args, 'skip-duplicates' ) ) {
$check_basename = $src_basename;
Expand Down Expand Up @@ -2328,6 +2332,36 @@ private function get_real_attachment_url( $attachment_id ) {
return wp_get_attachment_url( $attachment_id );
}

/**
* Makes a relative path to a local file absolute, for imports with --skip-copy.
*
* Only the directory part goes through realpath(). When it is inside the real uploads
* directory, the configured uploads basedir is put back in front, so a symlinked uploads
* directory (or a symlinked file in it) gives the same result as an absolute path.
*
* @param string $path Relative path to an existing file.
* @return string
*/
private function get_absolute_path( $path ) {
$real_dir = realpath( dirname( $path ) );
if ( false === $real_dir ) {
return $path;
}
$real_dir = wp_normalize_path( $real_dir );
$absolute = trailingslashit( $real_dir ) . Path::basename( $path );

$basedir = wp_get_upload_dir()['basedir'];
$real_basedir = realpath( $basedir );
if ( false !== $real_basedir ) {
$real_basedir = wp_normalize_path( $real_basedir );
if ( 0 === strpos( trailingslashit( $real_dir ), trailingslashit( $real_basedir ) ) ) {
$absolute = $basedir . substr( $absolute, strlen( $real_basedir ) );
}
}

return $absolute;
}

/**
* Create image slug based on user input slug.
* Add basename extension to slug.
Expand Down