From 6bccb80d7619f47391a44df3f5d884e5986f68d7 Mon Sep 17 00:00:00 2001 From: goldenapples Date: Wed, 27 Mar 2013 15:03:45 -0700 Subject: [PATCH] Initial implementation of wp media import Introduces `wp media import` command to sideload images and import them as attachments. Needs tests, second opinion, etc. --- man-src/media-import.txt | 20 ++++++++++++++++++++ man/media-import.1 | 43 +++++++++++++++++++++++++++++++++++++++++++ php/commands/media.php | 39 +++++++++++++++++++++++++++++++++++++++ utils/wp-completion.bash | 7 +++++-- 4 files changed, 107 insertions(+), 2 deletions(-) create mode 100644 man-src/media-import.txt create mode 100644 man/media-import.1 diff --git a/man-src/media-import.txt b/man-src/media-import.txt new file mode 100644 index 00000000..ed910ff5 --- /dev/null +++ b/man-src/media-import.txt @@ -0,0 +1,20 @@ +## OPTIONS + +* ``: + + Path to file or files to be imported. Supports the glob(3) capabilities of the current shell. + +* `--post-id=` + + ID of the post to attach the imported files to. + +* `--desc=` + + "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 index 00000000..304529c6 --- /dev/null +++ b/man/media-import.1 @@ -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\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=\fR +. +.IP +ID of the post to attach the imported files to\. +. +.IP "\(bu" 4 +\fB\-\-desc=\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 + diff --git a/php/commands/media.php b/php/commands/media.php index 36c37ac9..2ac40ad8 100644 --- a/php/commands/media.php +++ b/php/commands/media.php @@ -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 ... [--post_id=] [--desc=] + */ + 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 ); diff --git a/utils/wp-completion.bash b/utils/wp-completion.bash index f3822d35..d78f1001 100755 --- a/utils/wp-completion.bash +++ b/utils/wp-completion.bash @@ -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) ) -- 2.11.0