From a7af018591112f622520d18eed652cd87b1e578e Mon Sep 17 00:00:00 2001 From: Justin Maurer Date: Sun, 27 Aug 2023 11:56:54 -0400 Subject: [PATCH] Refactored to use `--porcelain=url` instead of `--show-url` to allow the porcelain output to use additional fields. 'ID' (default) and 'url' currently supported. --- features/media-import.feature | 4 ++-- src/Media_Command.php | 27 ++++++++++++++++----------- 2 files changed, 18 insertions(+), 13 deletions(-) diff --git a/features/media-import.feature b/features/media-import.feature index 87421e1f..3863659b 100644 --- a/features/media-import.feature +++ b/features/media-import.feature @@ -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/ @@ -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/ diff --git a/src/Media_Command.php b/src/Media_Command.php index f54063a4..e88631e8 100644 --- a/src/Media_Command.php +++ b/src/Media_Command.php @@ -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[=]] + * : Output just the selected field. Defaults to attachment ID. + * --- + * default: ID + * options: + * - ID + * - url + * --- * * ## EXAMPLES * @@ -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' ) ) { + 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( @@ -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 );