Skip to content

Commit

Permalink
Add support for 'url' to --porcelain to output the URL instead of a…
Browse files Browse the repository at this point in the history
…ttachment 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]>
  • Loading branch information
justinmaurerdotdev and danielbachhuber authored Aug 28, 2023
1 parent 82a8fe8 commit aa6d3f6
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 5 deletions.
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 );
}
}

0 comments on commit aa6d3f6

Please sign in to comment.