@@ -185,6 +185,9 @@ public function regenerate( $args, $assoc_args = array() ) {
185185 * [--post_name=<post_name>]
186186 * : Name of the post to attach the imported files to.
187187 *
188+ * [--file_name=<name>]
189+ * : Attachment name (post_name field).
190+ *
188191 * [--title=<title>]
189192 * : Attachment title (post title field).
190193 *
@@ -249,6 +252,7 @@ public function import( $args, $assoc_args = array() ) {
249252 $ assoc_args = wp_parse_args (
250253 $ assoc_args ,
251254 array (
255+ 'file_name ' => '' ,
252256 'title ' => '' ,
253257 'caption ' => '' ,
254258 'alt ' => '' ,
@@ -328,6 +332,11 @@ public function import( $args, $assoc_args = array() ) {
328332 $ name = strtok ( Utils \basename ( $ file ), '? ' );
329333 }
330334
335+ if ( ! empty ( $ assoc_args ['file_name ' ] ) ) {
336+ $ image_name = $ this ->get_image_name ( $ name , $ assoc_args ['file_name ' ] );
337+ $ name = ! empty ( $ image_name ) ? $ image_name : $ name ;
338+ }
339+
331340 $ file_array = array (
332341 'tmp_name ' => $ tempfile ,
333342 'name ' => $ name ,
@@ -414,6 +423,10 @@ public function import( $args, $assoc_args = array() ) {
414423 }
415424
416425 $ attachment_success_text = '' ;
426+ if ( $ assoc_args ['file_name ' ] ) {
427+ $ attachment_success_text .= " with file name {$ name }" ;
428+ }
429+
417430 if ( $ assoc_args ['post_id ' ] ) {
418431 $ attachment_success_text = " and attached to post {$ assoc_args ['post_id ' ]}" ;
419432 if ( Utils \get_flag_value ( $ assoc_args , 'featured_image ' ) ) {
@@ -1259,4 +1272,20 @@ private function get_real_attachment_url( $attachment_id ) {
12591272
12601273 return wp_get_attachment_url ( $ attachment_id );
12611274 }
1275+
1276+ /**
1277+ * Create image slug based on user input slug.
1278+ * Add basename extension to slug.
1279+ *
1280+ * @param string $basename Default slu of image.
1281+ * @param string $slug User input slug.
1282+ *
1283+ * @return string Image slug with extension.
1284+ */
1285+ private function get_image_name ( $ basename , $ slug ) {
1286+
1287+ $ extension = pathinfo ( $ basename , PATHINFO_EXTENSION );
1288+
1289+ return $ slug . '. ' . $ extension ;
1290+ }
12621291}
0 commit comments