--- /dev/null
+## 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"
--- /dev/null
+.\" 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
+
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 );
# 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]}
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) )