From 1f54dd516ae64a3d20b585e8353b5cb7a20f7eb4 Mon Sep 17 00:00:00 2001 From: Justin Maurer Date: Thu, 24 Aug 2023 15:26:22 -0400 Subject: [PATCH 1/7] Added `porcelain_url` flag. Is that the best name for that? Should output ONLY the URL, instead of attachment ID or verbose descriptions. --- features/media-import.feature | 31 ++++++++++++++++++++++++++++++- src/Media_Command.php | 8 +++++++- 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/features/media-import.feature b/features/media-import.feature index 3df7f4da..85fc7424 100644 --- a/features/media-import.feature +++ b/features/media-import.feature @@ -213,4 +213,33 @@ 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 + + @justin + 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: + """ + /large-image.jpg + """ + + @justin + 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 | + + When I run `wp media import 'http://wp-cli.org/behat-data/codeispoetry.png' {CACHE_DIR}/large-image.jpg --porcelain_url` + Then STDOUT should contain: + """ + /large-image.jpg + """ + + And STDOUT should contain: + """ + /codeispoetry.png + """ diff --git a/src/Media_Command.php b/src/Media_Command.php index 582ebd55..70f15aa9 100644 --- a/src/Media_Command.php +++ b/src/Media_Command.php @@ -211,6 +211,9 @@ public function regenerate( $args, $assoc_args = array() ) { * [--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 + * * ## EXAMPLES * * # Import all jpgs in the current user's "Pictures" directory, not attached to any post. @@ -414,6 +417,9 @@ 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 = wp_get_original_image_url( $success ); + WP_CLI::line( $file_location ); } else { WP_CLI::log( sprintf( @@ -428,7 +434,7 @@ public function import( $args, $assoc_args = array() ) { } // Report the result of the operation - if ( ! Utils\get_flag_value( $assoc_args, 'porcelain' ) ) { + if ( ! Utils\get_flag_value( $assoc_args, 'porcelain' ) && ! Utils\get_flag_value( $assoc_args, 'porcelain_url' ) ) { Utils\report_batch_operation_results( $noun, 'import', count( $args ), $successes, $errors ); } elseif ( $errors ) { WP_CLI::halt( 1 ); From 370868fac61d3e44261a1ad59b74e449646198b3 Mon Sep 17 00:00:00 2001 From: Justin Maurer Date: Thu, 24 Aug 2023 15:35:16 -0400 Subject: [PATCH 2/7] Removed debugging behat tags --- features/media-import.feature | 2 -- 1 file changed, 2 deletions(-) diff --git a/features/media-import.feature b/features/media-import.feature index 85fc7424..87e1122d 100644 --- a/features/media-import.feature +++ b/features/media-import.feature @@ -215,7 +215,6 @@ Feature: Manage WordPress attachments """ And the return code should be 1 - @justin Scenario: Return upload URL after importing a single valid file Given download: | path | url | @@ -227,7 +226,6 @@ Feature: Manage WordPress attachments /large-image.jpg """ - @justin Scenario: Return upload URL after importing a multiple valid files Given download: | path | url | From cacabe66848485b3efdfc7790f4c0c84b3b3b372 Mon Sep 17 00:00:00 2001 From: Justin Maurer Date: Thu, 24 Aug 2023 15:53:03 -0400 Subject: [PATCH 3/7] Fixed closed paren spacing --- src/Media_Command.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Media_Command.php b/src/Media_Command.php index 70f15aa9..7d1214b8 100644 --- a/src/Media_Command.php +++ b/src/Media_Command.php @@ -417,7 +417,7 @@ 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') ) { + } elseif ( Utils\get_flag_value( $assoc_args, 'porcelain_url' ) ) { $file_location = wp_get_original_image_url( $success ); WP_CLI::line( $file_location ); } else { From 9e9504455df4d4f48b12d15fa978bea8a2776553 Mon Sep 17 00:00:00 2001 From: Justin Maurer Date: Thu, 24 Aug 2023 17:03:25 -0400 Subject: [PATCH 4/7] 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. --- features/media-import.feature | 27 ++++++++++++++++++++++++--- src/Media_Command.php | 25 ++++++++++++++++++++++++- 2 files changed, 48 insertions(+), 4 deletions(-) diff --git a/features/media-import.feature b/features/media-import.feature index 87e1122d..87421e1f 100644 --- a/features/media-import.feature +++ b/features/media-import.feature @@ -222,16 +222,27 @@ Feature: Manage WordPress attachments 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 | + | 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/ + """ - When I run `wp media import 'http://wp-cli.org/behat-data/codeispoetry.png' {CACHE_DIR}/large-image.jpg --porcelain_url` Then STDOUT should contain: """ /large-image.jpg @@ -241,3 +252,13 @@ Feature: Manage WordPress attachments """ /codeispoetry.png """ + + And STDOUT should contain: + """ + /audio-with-no-cover.mp3 + """ + + And STDOUT should not contain: + """ + Success: + """ diff --git a/src/Media_Command.php b/src/Media_Command.php index 7d1214b8..f54063a4 100644 --- a/src/Media_Command.php +++ b/src/Media_Command.php @@ -418,7 +418,7 @@ 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 = wp_get_original_image_url( $success ); + $file_location = $this->get_real_attachment_url( $success ); WP_CLI::line( $file_location ); } else { WP_CLI::log( @@ -1230,4 +1230,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 ); + } } From a7af018591112f622520d18eed652cd87b1e578e Mon Sep 17 00:00:00 2001 From: Justin Maurer Date: Sun, 27 Aug 2023 11:56:54 -0400 Subject: [PATCH 5/7] 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 ); From 0f965f1d3fd61c46235565dbdeee82439c27bf9d Mon Sep 17 00:00:00 2001 From: Justin Maurer Date: Sun, 27 Aug 2023 13:15:11 -0400 Subject: [PATCH 6/7] Don't assign variable inside if condition. Renamed $field to $porcelain. --- src/Media_Command.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/Media_Command.php b/src/Media_Command.php index e88631e8..4692fe01 100644 --- a/src/Media_Command.php +++ b/src/Media_Command.php @@ -418,8 +418,10 @@ public function import( $args, $assoc_args = array() ) { } } - if ( $field = Utils\get_flag_value( $assoc_args, 'porcelain' ) ) { - if ( 'url' === strtolower( $field ) ) { + $porcelain = Utils\get_flag_value( $assoc_args, 'porcelain' ); + + if ( $porcelain ) { + if ( 'url' === strtolower( $porcelain ) ) { $file_location = $this->get_real_attachment_url( $success ); WP_CLI::line( $file_location ); } else { From 0ca83604b988cc60ea991788f67dfb209efdafc3 Mon Sep 17 00:00:00 2001 From: Daniel Bachhuber Date: Mon, 28 Aug 2023 07:27:59 -0700 Subject: [PATCH 7/7] Improve `--porcelain` docs and add some validation --- features/media-import.feature | 7 +++++++ src/Media_Command.php | 11 ++++++----- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/features/media-import.feature b/features/media-import.feature index 3863659b..af17a2cb 100644 --- a/features/media-import.feature +++ b/features/media-import.feature @@ -262,3 +262,10 @@ Feature: Manage WordPress attachments """ 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 4692fe01..3fc9f254 100644 --- a/src/Media_Command.php +++ b/src/Media_Command.php @@ -209,11 +209,9 @@ public function regenerate( $args, $assoc_args = array() ) { * : If set, set the imported image as the Featured Image of the post it is attached to. * * [--porcelain[=]] - * : Output just the selected field. Defaults to attachment ID. + * : Output a single field for each imported image. Defaults to attachment ID when used as flag. * --- - * default: ID * options: - * - ID * - url * --- * @@ -270,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' ); @@ -418,8 +421,6 @@ public function import( $args, $assoc_args = array() ) { } } - $porcelain = Utils\get_flag_value( $assoc_args, 'porcelain' ); - if ( $porcelain ) { if ( 'url' === strtolower( $porcelain ) ) { $file_location = $this->get_real_attachment_url( $success );