OSDN Git Service

Initial implementation of wp media import
authorgoldenapples <goldenapplesdesign@gmail.com>
Wed, 27 Mar 2013 22:03:45 +0000 (15:03 -0700)
committergoldenapples <goldenapplesdesign@gmail.com>
Thu, 25 Apr 2013 19:43:30 +0000 (12:43 -0700)
Introduces `wp media import` command to sideload images and import
them as attachments. Needs tests, second opinion, etc.

man-src/media-import.txt [new file with mode: 0644]
man/media-import.1 [new file with mode: 0644]
php/commands/media.php
utils/wp-completion.bash

diff --git a/man-src/media-import.txt b/man-src/media-import.txt
new file mode 100644 (file)
index 0000000..ed910ff
--- /dev/null
@@ -0,0 +1,20 @@
+## OPTIONS
+
+* `<file>`:
+
+       Path to file or files to be imported. Supports the glob(3) capabilities of the current shell.
+
+* `--post-id=<post_id>`
+
+       ID of the post to attach the imported files to.
+
+* `--desc=<description>`
+
+       "Description" field (post title) of attachment post
+
+
+## EXAMPLES
+
+       wp media import ~/Pictures/**/*.jpg
+
+       wp media import ~/Downloads/image.png --post_id=123 --desc="A downloaded picture"
diff --git a/man/media-import.1 b/man/media-import.1
new file mode 100644 (file)
index 0000000..304529c
--- /dev/null
@@ -0,0 +1,43 @@
+.\" generated with Ronn/v0.7.3
+.\" http://github.com/rtomayko/ronn/tree/0.7.3
+.
+.TH "WP\-MEDIA\-IMPORT" "1" "" "WP-CLI"
+.
+.SH "NAME"
+\fBwp\-media\-import\fR \- Sideload images from file(s) and import as attachments, optionally attached to a post
+.
+.SH "SYNOPSIS"
+wp media import \fIfile\fR\.\.\. [\-\-post_id=\fIpost_id\fR] [\-\-desc=\fIdescription\fR]
+.
+.SH "OPTIONS"
+.
+.IP "\(bu" 4
+\fB<file>\fR:
+.
+.IP
+Path to file or files to be imported\. Supports the glob(3) capabilities of the current shell\.
+.
+.IP "\(bu" 4
+\fB\-\-post\-id=<post_id>\fR
+.
+.IP
+ID of the post to attach the imported files to\.
+.
+.IP "\(bu" 4
+\fB\-\-desc=<description>\fR
+.
+.IP
+"Description" field (post title) of attachment post
+.
+.IP "" 0
+.
+.SH "EXAMPLES"
+.
+.nf
+
+wp media import ~/Pictures/**/*\.jpg
+
+wp media import ~/Downloads/image\.png \-\-post_id=123 \-\-desc="A downloaded picture"
+.
+.fi
+
index 36c37ac..2ac40ad 100644 (file)
@@ -53,6 +53,45 @@ class Media_Command extends WP_CLI_Command {
                WP_CLI::success( sprintf( 'Finished regenerating %1$s.', ngettext('the image', 'all images', $count) ) );
        }
 
+       /**
+        * Sideload images from file(s) and import as attachments, optionally attached to a post
+        *
+        * @synopsis <file>... [--post_id=<post_id>] [--desc=<description>]
+        */
+       function import( $args, $assoc_args = array() ) {
+
+               $assoc_args = wp_parse_args(
+                       $assoc_args,
+                       array(
+                               'post_id' => 0,
+                       )
+               );
+
+               foreach( $args as $file ) {
+                       $file_array = array(
+                               'tmp_name' => $file,
+                               'name' => basename( $file )
+                       );
+                       $success = media_handle_sideload( $file_array, $assoc_args['post_id'], $assoc_args['desc'] );
+                       if ( is_wp_error( $success ) )
+                               WP_CLI::error(
+                                       sprintf(
+                                               'Unable to import file %s. Reason: %s',
+                                               $file_array['tmp_name'], implode( ', ', $success->get_error_messages() )
+                                       )
+                               );
+                       else
+                               WP_CLI::success(
+                                       sprintf(
+                                               'Successfully imported file %s as attachment ID %d.',
+                                               $file_array['tmp_name'], $success
+                                       )
+                               );
+               }
+
+       }
+
+
        private function _process_regeneration( $id ) {
                $image = get_post( $id );
 
index f3822d3..d78f100 100755 (executable)
@@ -1,7 +1,7 @@
 # bash completion for the wp command
 
 _wp() {
-       local cur prev opts
+       local cur prev opts file_ops
 
        cur=${COMP_WORDS[COMP_CWORD]}
        prev=${COMP_WORDS[COMP_CWORD-1]}
@@ -12,7 +12,10 @@ _wp() {
                opts=$(wp --completions | grep ^$prev | cut -d ' ' -f 2- | tr '\n' ' ')
        fi
 
-       if [[ 'create' = $prev ]]; then
+       # An array of prev keywords that should get file completion
+       declare -A file_ops=([create]=1 [import]=1)
+
+       if [[ ${file_ops[$prev]} ]]; then
                COMPREPLY=( $(compgen -f "$cur") )
        else
                COMPREPLY=( $(compgen -W "$opts" -- $cur) )