Skip to content

Commit

Permalink
Refactored to use --porcelain=url instead of --show-url to allow …
Browse files Browse the repository at this point in the history
…the porcelain output to use additional fields. 'ID' (default) and 'url' currently supported.
  • Loading branch information
justinmaurerdotdev committed Aug 27, 2023
1 parent 9e95044 commit a7af018
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 13 deletions.
4 changes: 2 additions & 2 deletions features/media-import.feature
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ Feature: Manage WordPress attachments
| 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`
When I run `wp media import {CACHE_DIR}/large-image.jpg --porcelain=url`
Then STDOUT should contain:
"""
https://example.com/wp-content/uploads/
Expand All @@ -237,7 +237,7 @@ Feature: Manage WordPress attachments
| {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`
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/
Expand Down
27 changes: 16 additions & 11 deletions src/Media_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -208,11 +208,14 @@ 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_url]
* : If set, the URL of the file will be output on a second line of output instead of attachment ID
* [--porcelain[=<field>]]
* : Output just the selected field. Defaults to attachment ID.
* ---
* default: ID
* options:
* - ID
* - url
* ---
*
* ## EXAMPLES
*
Expand Down Expand Up @@ -415,11 +418,13 @@ public function import( $args, $assoc_args = array() ) {
}
}

if ( Utils\get_flag_value( $assoc_args, 'porcelain' ) ) {
WP_CLI::line( $success );
} elseif ( Utils\get_flag_value( $assoc_args, 'porcelain_url' ) ) {
$file_location = $this->get_real_attachment_url( $success );
WP_CLI::line( $file_location );
if ( $field = Utils\get_flag_value( $assoc_args, 'porcelain' ) ) {

Check warning on line 421 in src/Media_Command.php

View workflow job for this annotation

GitHub Actions / code-quality / PHPCS

Variable assignment found within a condition. Did you mean to do a comparison?

Check failure on line 421 in src/Media_Command.php

View workflow job for this annotation

GitHub Actions / code-quality / PHPCS

Assignments must be the first block of code on a line
if ( 'url' === strtolower( $field ) ) {
$file_location = $this->get_real_attachment_url( $success );
WP_CLI::line( $file_location );
} else {
WP_CLI::line( $success );
}
} else {
WP_CLI::log(
sprintf(
Expand All @@ -434,7 +439,7 @@ public function import( $args, $assoc_args = array() ) {
}

// Report the result of the operation
if ( ! Utils\get_flag_value( $assoc_args, 'porcelain' ) && ! Utils\get_flag_value( $assoc_args, 'porcelain_url' ) ) {
if ( ! Utils\get_flag_value( $assoc_args, 'porcelain' ) ) {
Utils\report_batch_operation_results( $noun, 'import', count( $args ), $successes, $errors );
} elseif ( $errors ) {
WP_CLI::halt( 1 );
Expand Down

0 comments on commit a7af018

Please sign in to comment.