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
Changes from 1 commit
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
25 changes: 25 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.
*
* [--slug=<slug>]
sandeshjangam marked this conversation as resolved.
Show resolved Hide resolved
* : Attachment slug (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(
'slug' => '',
'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['slug'] ) ) {
$image_slug = $this->get_image_slug( $name, $assoc_args['slug'] );
$name = ! empty( $image_slug ) ? $image_slug : $name;
}

$file_array = array(
'tmp_name' => $tempfile,
'name' => $name,
Expand Down Expand Up @@ -1259,4 +1268,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_slug( $basename, $slug ) {

$extension = pathinfo( $basename, PATHINFO_EXTENSION );

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