diff --git a/features/media-import.feature b/features/media-import.feature index 3df7f4da..af17a2cb 100644 --- a/features/media-import.feature +++ b/features/media-import.feature @@ -213,4 +213,59 @@ Feature: Manage WordPress attachments """ Warning: Unable to import file 'gobbledygook.png'. Reason: File doesn't exist. """ - And the return code should be 1 \ No newline at end of file + And the return code should be 1 + + Scenario: Return upload URL after importing a single valid file + Given download: + | path | url | + | {CACHE_DIR}/large-image.jpg | http://wp-cli.org/behat-data/large-image.jpg | + + When I run `wp media import {CACHE_DIR}/large-image.jpg --porcelain=url` + Then STDOUT should contain: + """ + https://example.com/wp-content/uploads/ + """ + + And STDOUT should contain: + """ + /large-image.jpg + """ + + Scenario: Return upload URL after importing a multiple valid files + Given download: + | path | url | + | {CACHE_DIR}/large-image.jpg | http://wp-cli.org/behat-data/large-image.jpg | + | {CACHE_DIR}/audio-with-no-cover.mp3 | http://wp-cli.org/behat-data/audio-with-no-cover.mp3 | + + 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` + Then STDOUT should contain: + """ + https://example.com/wp-content/uploads/ + """ + + Then STDOUT should contain: + """ + /large-image.jpg + """ + + And STDOUT should contain: + """ + /codeispoetry.png + """ + + And STDOUT should contain: + """ + /audio-with-no-cover.mp3 + """ + + And STDOUT should not contain: + """ + Success: + """ + + Scenario: Errors when invalid --porcelain flag is applied. + When I try `wp media import 'http://wp-cli.org/behat-data/codeispoetry.png' --porcelain=invalid` + Then STDERR should be: + """ + Error: Invalid value for : invalid. Expected flag or 'url'. + """ \ No newline at end of file diff --git a/src/Media_Command.php b/src/Media_Command.php index 582ebd55..3fc9f254 100644 --- a/src/Media_Command.php +++ b/src/Media_Command.php @@ -208,8 +208,12 @@ public function regenerate( $args, $assoc_args = array() ) { * [--featured_image] * : If set, set the imported image as the Featured Image of the post it is attached to. * - * [--porcelain] - * : Output just the new attachment ID. + * [--porcelain[=]] + * : Output a single field for each imported image. Defaults to attachment ID when used as flag. + * --- + * options: + * - url + * --- * * ## EXAMPLES * @@ -264,6 +268,11 @@ public function import( $args, $assoc_args = array() ) { $noun = 'image'; } + $porcelain = Utils\get_flag_value( $assoc_args, 'porcelain' ); + if ( is_string( $porcelain ) && ! in_array( $porcelain, array( 'url' ), true ) ) { + WP_CLI::error( sprintf( 'Invalid value for : %s. Expected flag or \'url\'.', $porcelain ) ); + } + if ( isset( $assoc_args['post_id'] ) ) { if ( ! get_post( $assoc_args['post_id'] ) ) { WP_CLI::warning( 'Invalid --post_id' ); @@ -412,8 +421,13 @@ public function import( $args, $assoc_args = array() ) { } } - if ( Utils\get_flag_value( $assoc_args, 'porcelain' ) ) { - WP_CLI::line( $success ); + if ( $porcelain ) { + if ( 'url' === strtolower( $porcelain ) ) { + $file_location = $this->get_real_attachment_url( $success ); + WP_CLI::line( $file_location ); + } else { + WP_CLI::line( $success ); + } } else { WP_CLI::log( sprintf( @@ -1224,4 +1238,27 @@ private function get_attached_file( $attachment_id ) { return get_attached_file( $attachment_id ); } + + /** + * Image-friendly alternative to wp_get_attachment_url(). Will return the full size URL of an image instead of the `-scaled` version. + * + * In WordPress 5.3, behavior changed to account for automatic resizing of + * big image files. + * + * @see https://core.trac.wordpress.org/ticket/47873 + * + * @param int $attachment_id ID of the attachment to get the URL for. + * @return string|false URL of the attachment, or false if not found. + */ + private function get_real_attachment_url( $attachment_id ) { + if ( function_exists( 'wp_get_original_image_url' ) ) { + $url = wp_get_original_image_url( $attachment_id ); + + if ( false !== $url ) { + return $url; + } + } + + return wp_get_attachment_url( $attachment_id ); + } }