Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add --file_name=<name> argument for wp media import #187

Merged
merged 4 commits into from
Nov 10, 2023
Merged
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
11 changes: 11 additions & 0 deletions features/media-import.feature
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,17 @@ Feature: Manage WordPress attachments
Success: Imported 1 of 1 items.
"""

Scenario: Import media from remote URL and use input file as attachment name
When I run `wp media import 'http://wp-cli.org/behat-data/codeispoetry.png' --file_name=abc`
Then STDOUT should contain:
"""
file name abc.png
"""
And STDOUT should contain:
"""
Success: Imported 1 of 1 items.
"""

Scenario: Fail to import missing image
When I try `wp media import gobbledygook.png`
Then STDERR should be:
Expand Down
29 changes: 29 additions & 0 deletions src/Media_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,9 @@ public function regenerate( $args, $assoc_args = array() ) {
* [--post_name=<post_name>]
* : Name of the post to attach the imported files to.
*
* [--file_name=<name>]
* : Attachment name (post_name field).
*
* [--title=<title>]
* : Attachment title (post title field).
*
Expand Down Expand Up @@ -249,6 +252,7 @@ public function import( $args, $assoc_args = array() ) {
$assoc_args = wp_parse_args(
$assoc_args,
array(
'file_name' => '',
'title' => '',
'caption' => '',
'alt' => '',
Expand Down Expand Up @@ -328,6 +332,11 @@ public function import( $args, $assoc_args = array() ) {
$name = strtok( Utils\basename( $file ), '?' );
}

if ( ! empty( $assoc_args['file_name'] ) ) {
$image_name = $this->get_image_name( $name, $assoc_args['file_name'] );
$name = ! empty( $image_name ) ? $image_name : $name;
}

$file_array = array(
'tmp_name' => $tempfile,
'name' => $name,
Expand Down Expand Up @@ -414,6 +423,10 @@ public function import( $args, $assoc_args = array() ) {
}

$attachment_success_text = '';
if ( $assoc_args['file_name'] ) {
$attachment_success_text .= " with file name {$name}";
}

if ( $assoc_args['post_id'] ) {
$attachment_success_text = " and attached to post {$assoc_args['post_id']}";
if ( Utils\get_flag_value( $assoc_args, 'featured_image' ) ) {
Expand Down Expand Up @@ -1259,4 +1272,20 @@ private function get_real_attachment_url( $attachment_id ) {

return wp_get_attachment_url( $attachment_id );
}

/**
* Create image slug based on user input slug.
* Add basename extension to slug.
*
* @param string $basename Default slu of image.
* @param string $slug User input slug.
*
* @return string Image slug with extension.
*/
private function get_image_name( $basename, $slug ) {

$extension = pathinfo( $basename, PATHINFO_EXTENSION );

return $slug . '.' . $extension;
}
}