From f25992520584fc05f1af589ac608aa04a9f8780f Mon Sep 17 00:00:00 2001 From: goldenapples Date: Thu, 28 Mar 2013 10:21:07 -0700 Subject: [PATCH] Rename arguments to make more logical sense Use the same field labels as are used by the admin media uploader, to avoid confusion: title / caption / alt / description; rather than the variable names used internally, where 'description' is really title and so on. --- man-src/media-import.txt | 24 ++++++++++++++++++++---- man/media-import.1 | 32 ++++++++++++++++++++++++++------ php/commands/media.php | 18 ++++++++++++++++-- 3 files changed, 62 insertions(+), 12 deletions(-) diff --git a/man-src/media-import.txt b/man-src/media-import.txt index d6192984..e937ada5 100644 --- a/man-src/media-import.txt +++ b/man-src/media-import.txt @@ -6,17 +6,33 @@ If file is recognized as a URL (for example, with a scheme of http or ftp), the file will be downloaded to a temp file before being sideloaded. -* `--post-id=` +* `--post_id=` - ID of the post to attach the imported files to. + ID of the post to attach the imported files to + +* `--title=` + + Attachment title (post title field) + +* `--caption=<caption>` + + Caption for attachent (post excerpt field) + +* `--alt=<alt_text>` + + Alt text for image (saved as post meta) * `--desc=<description>` - "Description" field (post title) of attachment post + "Description" field (post content) of attachment post ## EXAMPLES wp media import ~/Pictures/**/*.jpg - wp media import ~/Downloads/image.png --post_id=123 --desc="A downloaded picture" + wp media import ~/Downloads/image.png --post_id=123 --title="A downloaded picture" + + wp media import http://s.wordpress.org/style/images/wp-header-logo.png --title='The WordPress logo' --alt="Semantic personal publishing" + + diff --git a/man/media-import.1 b/man/media-import.1 index 9bce04bc..0af6e249 100644 --- a/man/media-import.1 +++ b/man/media-import.1 @@ -4,10 +4,10 @@ .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 +\fBwp\-media\-import\fR \- Sideload images from local file(s) or URL and import as attachments, optionally attached to a post . .SH "SYNOPSIS" -wp media import \fIfile\fR\.\.\. [\-\-post_id=\fIpost_id\fR] [\-\-desc=\fIdescription\fR] +wp media import \fIfile\fR\.\.\. [\-\-post_id=\fIpost_id\fR] [\-\-title=\fItitle\fR] [\-\-caption=\fIcaption\fR] [\-\-alt=\fIalt_text\fR] [\-\-desc=\fIdescription\fR] . .SH "OPTIONS" . @@ -18,16 +18,34 @@ wp media import \fIfile\fR\.\.\. [\-\-post_id=\fIpost_id\fR] [\-\-desc=\fIdescri Path to file or files to be imported\. Supports the glob(3) capabilities of the current shell\. If file is recognized as a URL (for example, with a scheme of http or ftp), the file will be downloaded to a temp file before being sideloaded\. . .IP "\(bu" 4 -\fB\-\-post\-id=<post_id>\fR +\fB\-\-post_id=<post_id>\fR . .IP -ID of the post to attach the imported files to\. +ID of the post to attach the imported files to +. +.IP "\(bu" 4 +\fB\-\-title=<title>\fR +. +.IP +Attachment title (post title field) +. +.IP "\(bu" 4 +\fB\-\-caption=<caption>\fR +. +.IP +Caption for attachent (post excerpt field) +. +.IP "\(bu" 4 +\fB\-\-alt=<alt_text>\fR +. +.IP +Alt text for image (saved as post meta) . .IP "\(bu" 4 \fB\-\-desc=<description>\fR . .IP -"Description" field (post title) of attachment post +"Description" field (post content) of attachment post . .IP "" 0 . @@ -37,7 +55,9 @@ ID of the post to attach the imported files to\. wp media import ~/Pictures/**/*\.jpg -wp media import ~/Downloads/image\.png \-\-post_id=123 \-\-desc="A downloaded picture" +wp media import ~/Downloads/image\.png \-\-post_id=123 \-\-title="A downloaded picture" + +wp media import http://s\.wordpress\.org/style/images/wp\-header\-logo\.png \-\-title=\'The WordPress logo\' \-\-alt="Semantic personal publishing" . .fi diff --git a/php/commands/media.php b/php/commands/media.php index 475972e4..be4eebf8 100644 --- a/php/commands/media.php +++ b/php/commands/media.php @@ -56,7 +56,7 @@ class Media_Command extends WP_CLI_Command { /** * Sideload images from local file(s) or URL and import as attachments, optionally attached to a post * - * @synopsis <file>... [--post_id=<post_id>] [--desc=<description>] + * @synopsis <file>... [--post_id=<post_id>] [--title=<title>] [--caption=<caption>] [--alt=<alt_text>] [--desc=<description>] */ function import( $args, $assoc_args = array() ) { @@ -64,6 +64,9 @@ class Media_Command extends WP_CLI_Command { $assoc_args, array( 'post_id' => 0, + 'title' => null, + 'caption' => null, + 'alt' => null, 'desc' => null ) ); @@ -88,7 +91,18 @@ class Media_Command extends WP_CLI_Command { 'tmp_name' => $file, 'name' => basename( $file ) ); - $success = media_handle_sideload( $file_array, $assoc_args['post_id'], $assoc_args['desc'] ); + + $post_array= array( + 'post_title' => $assoc_args['title'], + 'post_excerpt' => $assoc_args['caption'], + 'post_content' => $assoc_args['desc'] + ); + + $success = media_handle_sideload( $file_array, $assoc_args['post_id'], $assoc_args['title'], $post_array ); + + if ( !is_wp_error( $success ) && $assoc_args['alt'] ) + update_post_meta( $success, '_wp_attachment_image_alt', $assoc_args['alt'] ); + if ( is_wp_error( $success ) ) WP_CLI::error( sprintf( -- 2.11.0