Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for 'url' to --porcelain to output the URL instead of attachment ID #182

Merged
57 changes: 56 additions & 1 deletion features/media-import.feature
Original file line number Diff line number Diff line change
Expand Up @@ -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
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 <porcelain>: invalid. Expected flag or 'url'.
"""
45 changes: 41 additions & 4 deletions src/Media_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -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[=<field>]]
* : Output a single field for each imported image. Defaults to attachment ID when used as flag.
* ---
* options:
* - url
* ---
*
* ## EXAMPLES
*
Expand Down Expand Up @@ -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 <porcelain>: %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' );
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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 );
}
}