Skip to content

Commit aa6d3f6

Browse files
Add support for 'url' to --porcelain to output the URL instead of attachment ID (#182)
* Added `porcelain_url` flag. Is that the best name for that? Should output ONLY the URL, instead of attachment ID or verbose descriptions. * Removed debugging behat tags * Fixed closed paren spacing * Added `get_real_attachment_url()` method to account for '-scaled' image URLs being returned by `wp_get_attachment_url()`. Improved tests, including non-image file for thoroughness. * Refactored to use `--porcelain=url` instead of `--show-url` to allow the porcelain output to use additional fields. 'ID' (default) and 'url' currently supported. * Don't assign variable inside if condition. Renamed $field to $porcelain. * Improve `--porcelain` docs and add some validation --------- Co-authored-by: Daniel Bachhuber <[email protected]>
1 parent 82a8fe8 commit aa6d3f6

File tree

2 files changed

+97
-5
lines changed

2 files changed

+97
-5
lines changed

features/media-import.feature

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,4 +213,59 @@ Feature: Manage WordPress attachments
213213
"""
214214
Warning: Unable to import file 'gobbledygook.png'. Reason: File doesn't exist.
215215
"""
216-
And the return code should be 1
216+
And the return code should be 1
217+
218+
Scenario: Return upload URL after importing a single valid file
219+
Given download:
220+
| path | url |
221+
| {CACHE_DIR}/large-image.jpg | http://wp-cli.org/behat-data/large-image.jpg |
222+
223+
When I run `wp media import {CACHE_DIR}/large-image.jpg --porcelain=url`
224+
Then STDOUT should contain:
225+
"""
226+
https://example.com/wp-content/uploads/
227+
"""
228+
229+
And STDOUT should contain:
230+
"""
231+
/large-image.jpg
232+
"""
233+
234+
Scenario: Return upload URL after importing a multiple valid files
235+
Given download:
236+
| path | url |
237+
| {CACHE_DIR}/large-image.jpg | http://wp-cli.org/behat-data/large-image.jpg |
238+
| {CACHE_DIR}/audio-with-no-cover.mp3 | http://wp-cli.org/behat-data/audio-with-no-cover.mp3 |
239+
240+
When I run `wp media import 'http://wp-cli.org/behat-data/codeispoetry.png' {CACHE_DIR}/large-image.jpg {CACHE_DIR}/audio-with-no-cover.mp3 --porcelain=url`
241+
Then STDOUT should contain:
242+
"""
243+
https://example.com/wp-content/uploads/
244+
"""
245+
246+
Then STDOUT should contain:
247+
"""
248+
/large-image.jpg
249+
"""
250+
251+
And STDOUT should contain:
252+
"""
253+
/codeispoetry.png
254+
"""
255+
256+
And STDOUT should contain:
257+
"""
258+
/audio-with-no-cover.mp3
259+
"""
260+
261+
And STDOUT should not contain:
262+
"""
263+
Success:
264+
"""
265+
266+
Scenario: Errors when invalid --porcelain flag is applied.
267+
When I try `wp media import 'http://wp-cli.org/behat-data/codeispoetry.png' --porcelain=invalid`
268+
Then STDERR should be:
269+
"""
270+
Error: Invalid value for <porcelain>: invalid. Expected flag or 'url'.
271+
"""

src/Media_Command.php

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -208,8 +208,12 @@ public function regenerate( $args, $assoc_args = array() ) {
208208
* [--featured_image]
209209
* : If set, set the imported image as the Featured Image of the post it is attached to.
210210
*
211-
* [--porcelain]
212-
* : Output just the new attachment ID.
211+
* [--porcelain[=<field>]]
212+
* : Output a single field for each imported image. Defaults to attachment ID when used as flag.
213+
* ---
214+
* options:
215+
* - url
216+
* ---
213217
*
214218
* ## EXAMPLES
215219
*
@@ -264,6 +268,11 @@ public function import( $args, $assoc_args = array() ) {
264268
$noun = 'image';
265269
}
266270

271+
$porcelain = Utils\get_flag_value( $assoc_args, 'porcelain' );
272+
if ( is_string( $porcelain ) && ! in_array( $porcelain, array( 'url' ), true ) ) {
273+
WP_CLI::error( sprintf( 'Invalid value for <porcelain>: %s. Expected flag or \'url\'.', $porcelain ) );
274+
}
275+
267276
if ( isset( $assoc_args['post_id'] ) ) {
268277
if ( ! get_post( $assoc_args['post_id'] ) ) {
269278
WP_CLI::warning( 'Invalid --post_id' );
@@ -412,8 +421,13 @@ public function import( $args, $assoc_args = array() ) {
412421
}
413422
}
414423

415-
if ( Utils\get_flag_value( $assoc_args, 'porcelain' ) ) {
416-
WP_CLI::line( $success );
424+
if ( $porcelain ) {
425+
if ( 'url' === strtolower( $porcelain ) ) {
426+
$file_location = $this->get_real_attachment_url( $success );
427+
WP_CLI::line( $file_location );
428+
} else {
429+
WP_CLI::line( $success );
430+
}
417431
} else {
418432
WP_CLI::log(
419433
sprintf(
@@ -1224,4 +1238,27 @@ private function get_attached_file( $attachment_id ) {
12241238

12251239
return get_attached_file( $attachment_id );
12261240
}
1241+
1242+
/**
1243+
* Image-friendly alternative to wp_get_attachment_url(). Will return the full size URL of an image instead of the `-scaled` version.
1244+
*
1245+
* In WordPress 5.3, behavior changed to account for automatic resizing of
1246+
* big image files.
1247+
*
1248+
* @see https://core.trac.wordpress.org/ticket/47873
1249+
*
1250+
* @param int $attachment_id ID of the attachment to get the URL for.
1251+
* @return string|false URL of the attachment, or false if not found.
1252+
*/
1253+
private function get_real_attachment_url( $attachment_id ) {
1254+
if ( function_exists( 'wp_get_original_image_url' ) ) {
1255+
$url = wp_get_original_image_url( $attachment_id );
1256+
1257+
if ( false !== $url ) {
1258+
return $url;
1259+
}
1260+
}
1261+
1262+
return wp_get_attachment_url( $attachment_id );
1263+
}
12271264
}

0 commit comments

Comments
 (0)