Skip to content

Commit

Permalink
Add --file_name=<name> argument for wp media import (#187)
Browse files Browse the repository at this point in the history
* Added slug option for media

* Changed slug to file_name

* Added functional test

* Tweak success output

Co-authored-by: Alain Schlesser <[email protected]>

---------

Co-authored-by: Daniel Bachhuber <[email protected]>
Co-authored-by: Alain Schlesser <[email protected]>
  • Loading branch information
3 people authored Nov 10, 2023
1 parent 49ef52c commit 4950ed4
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
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;
}
}

0 comments on commit 4950ed4

Please sign in to comment.