From af02f1c83132cc7dc6ea73fbc5926a3544f769aa Mon Sep 17 00:00:00 2001 From: Phil Banks Date: Wed, 21 Jun 2017 11:26:34 +0100 Subject: [PATCH 01/15] functional test for --import_only --- features/media-import.feature | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/features/media-import.feature b/features/media-import.feature index 058a31733..6cbdabf62 100644 --- a/features/media-import.feature +++ b/features/media-import.feature @@ -69,6 +69,24 @@ Feature: Manage WordPress attachments My fabulous caption """ + Scenario: Import a file as attachment from a local image and leave it in it's current location + 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 --import_only` + Then STDOUT should contain: + """ + Imported file + """ + And STDOUT should contain: + """ + Success: Imported 1 of 1 images. + """ + And the {CACHE_DIR}/large-image.jpg file should exist + And the return code should be 0 + + Scenario: Import a file and use its filename as the title Given download: | path | url | From 995dad7d3c9c2ddfc4ddc4584822047d83187e79 Mon Sep 17 00:00:00 2001 From: Phil Banks Date: Wed, 21 Jun 2017 12:10:12 +0100 Subject: [PATCH 02/15] add --import_only --- src/Media_Command.php | 46 +++++++++++++++++++++++++++++++++---------- 1 file changed, 36 insertions(+), 10 deletions(-) diff --git a/src/Media_Command.php b/src/Media_Command.php index d4889cfe1..63fd06daa 100644 --- a/src/Media_Command.php +++ b/src/Media_Command.php @@ -173,6 +173,9 @@ function regenerate( $args, $assoc_args = array() ) { * [--desc=] * : "Description" field (post content) of attachment post. * + * [--import_only] + * : If set, media files (local only) are imported to the library but not moved on disk. + * * [--featured_image] * : If set, set the imported image as the Featured Image of the post its attached to. * @@ -235,7 +238,11 @@ function import( $args, $assoc_args = array() ) { } $tempfile = $this->make_copy( $file ); } else { - $tempfile = download_url( $file ); + if ( \WP_CLI\Utils\get_flag_value( $assoc_args, 'import_only' ) ) { + $tempfile = $file; + } else { + $tempfile = $this->make_copy( $file ); + } if ( is_wp_error( $tempfile ) ) { WP_CLI::warning( sprintf( "Unable to import file '%s'. Reason: %s", @@ -278,15 +285,34 @@ function import( $args, $assoc_args = array() ) { $post_array['post_title'] = preg_replace( '/\.[^.]+$/', '', Utils\basename( $file ) ); } - // Deletes the temporary file. - $success = media_handle_sideload( $file_array, $assoc_args['post_id'], $assoc_args['title'], $post_array ); - if ( is_wp_error( $success ) ) { - WP_CLI::warning( sprintf( - "Unable to import file '%s'. Reason: %s", - $orig_filename, implode( ', ', $success->get_error_messages() ) - ) ); - $errors++; - continue; + if ( \WP_CLI\Utils\get_flag_value( $assoc_args, 'import_only' ) ) { + $wp_filetype = wp_check_filetype( $file, null ); + $post_array['post_mime_type'] = $wp_filetype['type']; + $post_array['post_status'] = 'inherit'; + + $success = wp_insert_attachment( $post_array, $file, $assoc_args['post_id'] ); + if ( ! is_wp_error( $success ) ) { + wp_update_attachment_metadata( $success, wp_generate_attachment_metadata( $success, $file ) ); + } + if ( is_wp_error( $success ) ) { + WP_CLI::warning( sprintf( + "Unable to insert file '%s'. Reason: %s", + $orig_filename, implode( ', ', $success->get_error_messages() ) + ) ); + $errors++; + continue; + } + } else { + // Deletes the temporary file. + $success = media_handle_sideload( $file_array, $assoc_args['post_id'], $assoc_args['title'], $post_array ); + if ( is_wp_error( $success ) ) { + WP_CLI::warning( sprintf( + "Unable to import file '%s'. Reason: %s", + $orig_filename, implode( ', ', $success->get_error_messages() ) + ) ); + $errors++; + continue; + } } // Set alt text. From a94eaba751a3767c5ae4c612bae331e844b63cd0 Mon Sep 17 00:00:00 2001 From: Phil Banks Date: Wed, 21 Jun 2017 12:40:39 +0100 Subject: [PATCH 03/15] fix broken remote file import --- src/Media_Command.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Media_Command.php b/src/Media_Command.php index 63fd06daa..5f63821c3 100644 --- a/src/Media_Command.php +++ b/src/Media_Command.php @@ -236,13 +236,13 @@ function import( $args, $assoc_args = array() ) { $errors++; continue; } - $tempfile = $this->make_copy( $file ); - } else { if ( \WP_CLI\Utils\get_flag_value( $assoc_args, 'import_only' ) ) { $tempfile = $file; } else { $tempfile = $this->make_copy( $file ); } + } else { + $tempfile = download_url( $file ); if ( is_wp_error( $tempfile ) ) { WP_CLI::warning( sprintf( "Unable to import file '%s'. Reason: %s", From 7201e90956c0366a81a7999b7ff2f938fb5013bf Mon Sep 17 00:00:00 2001 From: Phil Banks Date: Wed, 21 Jun 2017 14:13:15 +0100 Subject: [PATCH 04/15] update --import_only test --- features/media-import.feature | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/features/media-import.feature b/features/media-import.feature index 6cbdabf62..3385d67d9 100644 --- a/features/media-import.feature +++ b/features/media-import.feature @@ -81,7 +81,7 @@ Feature: Manage WordPress attachments """ And STDOUT should contain: """ - Success: Imported 1 of 1 images. + Success: Imported 1 of 1 """ And the {CACHE_DIR}/large-image.jpg file should exist And the return code should be 0 From eeb9836b9ecd74e72e188a8efdae83306e4ae935 Mon Sep 17 00:00:00 2001 From: Phil Banks Date: Wed, 21 Jun 2017 18:17:34 +0100 Subject: [PATCH 05/15] Revert "WPCS fixes" This reverts commit 3f27d46d8430ee105d2950770d452b531f3c81b1. --- src/Media_Command.php | 51 +++++++++++++++++++------------------------ 1 file changed, 23 insertions(+), 28 deletions(-) diff --git a/src/Media_Command.php b/src/Media_Command.php index 5f63821c3..ca085dbed 100644 --- a/src/Media_Command.php +++ b/src/Media_Command.php @@ -113,19 +113,20 @@ function regenerate( $args, $assoc_args = array() ) { 'post_mime_type' => $mime_types, 'post_status' => 'any', 'posts_per_page' => -1, - 'fields' => 'ids', + 'fields' => 'ids' ); $images = new WP_Query( $query_args ); $count = $images->post_count; - if ( ! $count ) { + if ( !$count ) { WP_CLI::warning( 'No images found.' ); return; } - WP_CLI::log( sprintf( 'Found %1$d %2$s to regenerate.', $count, _n( 'image', 'images', $count ) ) ); + WP_CLI::log( sprintf( 'Found %1$d %2$s to regenerate.', $count, + _n( 'image', 'images', $count ) ) ); if ( $image_size ) { $image_size_filters = $this->add_image_size_filters( $image_size ); @@ -217,8 +218,8 @@ function import( $args, $assoc_args = array() ) { ) ); if ( isset( $assoc_args['post_id'] ) ) { - if ( ! get_post( $assoc_args['post_id'] ) ) { - WP_CLI::warning( 'Invalid --post_id' ); + if ( !get_post( $assoc_args['post_id'] ) ) { + WP_CLI::warning( "Invalid --post_id" ); $assoc_args['post_id'] = false; } } else { @@ -231,7 +232,7 @@ function import( $args, $assoc_args = array() ) { $orig_filename = $file; if ( empty( $is_file_remote ) ) { - if ( ! file_exists( $file ) ) { + if ( !file_exists( $file ) ) { WP_CLI::warning( "Unable to import file '$file'. Reason: File doesn't exist." ); $errors++; continue; @@ -255,17 +256,17 @@ function import( $args, $assoc_args = array() ) { $file_array = array( 'tmp_name' => $tempfile, - 'name' => Utils\basename( $file ), + 'name' => Utils\basename( $file ) ); - $post_array = array( + $post_array= array( 'post_title' => $assoc_args['title'], 'post_excerpt' => $assoc_args['caption'], - 'post_content' => $assoc_args['desc'], + 'post_content' => $assoc_args['desc'] ); $post_array = wp_slash( $post_array ); - // use image exif/iptc data for title and caption defaults if possible. + // use image exif/iptc data for title and caption defaults if possible if ( empty( $post_array['post_title'] ) || empty( $post_array['post_excerpt'] ) ) { // @codingStandardsIgnoreStart $image_meta = @wp_read_image_metadata( $tempfile ); @@ -315,12 +316,12 @@ function import( $args, $assoc_args = array() ) { } } - // Set alt text. + // Set alt text if ( $assoc_args['alt'] ) { update_post_meta( $success, '_wp_attachment_image_alt', wp_slash( $assoc_args['alt'] ) ); } - // Set as featured image, if --post_id and --featured_image are set. + // Set as featured image, if --post_id and --featured_image are set if ( $assoc_args['post_id'] && \WP_CLI\Utils\get_flag_value( $assoc_args, 'featured_image' ) ) { update_post_meta( $assoc_args['post_id'], '_thumbnail_id', $success ); } @@ -328,9 +329,8 @@ function import( $args, $assoc_args = array() ) { $attachment_success_text = ''; if ( $assoc_args['post_id'] ) { $attachment_success_text = " and attached to post {$assoc_args['post_id']}"; - if ( \WP_CLI\Utils\get_flag_value( $assoc_args, 'featured_image' ) ) { + if ( \WP_CLI\Utils\get_flag_value( $assoc_args, 'featured_image' ) ) $attachment_success_text .= ' as featured image'; - } } if ( \WP_CLI\Utils\get_flag_value( $assoc_args, 'porcelain' ) ) { @@ -350,18 +350,16 @@ function import( $args, $assoc_args = array() ) { } } - // wp_tempnam() inexplicably forces a .tmp extension, which spoils MIME type detection. + // wp_tempnam() inexplicably forces a .tmp extension, which spoils MIME type detection private function make_copy( $path ) { $dir = get_temp_dir(); $filename = Utils\basename( $path ); - if ( empty( $filename ) ) { + if ( empty( $filename ) ) $filename = time(); - } $filename = $dir . wp_unique_filename( $dir, $filename ); - if ( ! copy( $path, $filename ) ) { + if ( !copy( $path, $filename ) ) WP_CLI::error( "Could not create temporary file for $path." ); - } return $filename; } @@ -372,7 +370,7 @@ private function process_regeneration( $id, $skip_delete, $only_missing, $image_ $att_desc = sprintf( '"%1$s" (ID %2$d)', get_the_title( $id ), $id ); $thumbnail_desc = $image_size ? sprintf( '"%s" thumbnail', $image_size ) : 'thumbnail'; - if ( false === $fullsizepath || ! file_exists( $fullsizepath ) ) { + if ( false === $fullsizepath || !file_exists( $fullsizepath ) ) { WP_CLI::warning( "Can't find $att_desc." ); return false; } @@ -434,19 +432,17 @@ private function remove_old_images( $att_id, $fullsizepath, $image_size ) { foreach ( $metadata['sizes'] as $size_info ) { $intermediate_path = $dir_path . $size_info['file']; - if ( $intermediate_path === $fullsizepath ) { + if ( $intermediate_path === $fullsizepath ) continue; - } - if ( file_exists( $intermediate_path ) ) { + if ( file_exists( $intermediate_path ) ) unlink( $intermediate_path ); - } } } private function needs_regeneration( $att_id, $fullsizepath, $is_pdf, $image_size ) { - $metadata = wp_get_attachment_metadata( $att_id ); + $metadata = wp_get_attachment_metadata($att_id); // Note that an attachment can have no sizes if it's on or below the thumbnail threshold. @@ -470,12 +466,11 @@ private function needs_regeneration( $att_id, $fullsizepath, $is_pdf, $image_siz $dir_path = dirname( $fullsizepath ) . '/'; // Check that the thumbnail files exist. - foreach ( $metadata['sizes'] as $size_info ) { + foreach( $metadata['sizes'] as $size_info ) { $intermediate_path = $dir_path . $size_info['file']; - if ( $intermediate_path === $fullsizepath ) { + if ( $intermediate_path === $fullsizepath ) continue; - } if ( ! file_exists( $intermediate_path ) ) { return true; From 422a202eab32aa622d6867e846385137677b8b27 Mon Sep 17 00:00:00 2001 From: Phil Banks Date: Wed, 21 Jun 2017 18:20:10 +0100 Subject: [PATCH 06/15] updated noun in test --- features/media-import.feature | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/features/media-import.feature b/features/media-import.feature index 3385d67d9..25a122768 100644 --- a/features/media-import.feature +++ b/features/media-import.feature @@ -81,7 +81,7 @@ Feature: Manage WordPress attachments """ And STDOUT should contain: """ - Success: Imported 1 of 1 + Success: Imported 1 of 1 items. """ And the {CACHE_DIR}/large-image.jpg file should exist And the return code should be 0 From e5ea370d9bc210cca6657b0304be7a9ea0a14f96 Mon Sep 17 00:00:00 2001 From: Phil Banks Date: Thu, 22 Jun 2017 18:43:20 +0100 Subject: [PATCH 07/15] rename to --skip-copy and improve test --- features/media-import.feature | 3 ++- src/Media_Command.php | 6 +++--- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/features/media-import.feature b/features/media-import.feature index 25a122768..bf1680004 100644 --- a/features/media-import.feature +++ b/features/media-import.feature @@ -74,7 +74,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 --import_only` + When I run `wp media import {CACHE_DIR}/large-image.jpg --skip-copy` Then STDOUT should contain: """ Imported file @@ -84,6 +84,7 @@ Feature: Manage WordPress attachments Success: Imported 1 of 1 items. """ And the {CACHE_DIR}/large-image.jpg file should exist + And the wp-content/uploads/large-image.jpg file should not exist And the return code should be 0 diff --git a/src/Media_Command.php b/src/Media_Command.php index ca085dbed..6a441b69c 100644 --- a/src/Media_Command.php +++ b/src/Media_Command.php @@ -174,7 +174,7 @@ function regenerate( $args, $assoc_args = array() ) { * [--desc=] * : "Description" field (post content) of attachment post. * - * [--import_only] + * [--skip-copy] * : If set, media files (local only) are imported to the library but not moved on disk. * * [--featured_image] @@ -237,7 +237,7 @@ function import( $args, $assoc_args = array() ) { $errors++; continue; } - if ( \WP_CLI\Utils\get_flag_value( $assoc_args, 'import_only' ) ) { + if ( \WP_CLI\Utils\get_flag_value( $assoc_args, 'skip-copy' ) ) { $tempfile = $file; } else { $tempfile = $this->make_copy( $file ); @@ -286,7 +286,7 @@ function import( $args, $assoc_args = array() ) { $post_array['post_title'] = preg_replace( '/\.[^.]+$/', '', Utils\basename( $file ) ); } - if ( \WP_CLI\Utils\get_flag_value( $assoc_args, 'import_only' ) ) { + if ( \WP_CLI\Utils\get_flag_value( $assoc_args, 'skip-copy' ) ) { $wp_filetype = wp_check_filetype( $file, null ); $post_array['post_mime_type'] = $wp_filetype['type']; $post_array['post_status'] = 'inherit'; From 6c763ecf000f99e9f7a9dbabe6ca285dda4abd94 Mon Sep 17 00:00:00 2001 From: Phil Banks Date: Mon, 26 Jun 2017 17:01:53 +0100 Subject: [PATCH 08/15] bail early on error --- src/Media_Command.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/Media_Command.php b/src/Media_Command.php index 6a441b69c..bc19dee1e 100644 --- a/src/Media_Command.php +++ b/src/Media_Command.php @@ -292,9 +292,6 @@ function import( $args, $assoc_args = array() ) { $post_array['post_status'] = 'inherit'; $success = wp_insert_attachment( $post_array, $file, $assoc_args['post_id'] ); - if ( ! is_wp_error( $success ) ) { - wp_update_attachment_metadata( $success, wp_generate_attachment_metadata( $success, $file ) ); - } if ( is_wp_error( $success ) ) { WP_CLI::warning( sprintf( "Unable to insert file '%s'. Reason: %s", @@ -303,6 +300,7 @@ function import( $args, $assoc_args = array() ) { $errors++; continue; } + wp_update_attachment_metadata( $success, wp_generate_attachment_metadata( $success, $file ) ); } else { // Deletes the temporary file. $success = media_handle_sideload( $file_array, $assoc_args['post_id'], $assoc_args['title'], $post_array ); From 27d1446b0c475c106282102709531d9483894e6c Mon Sep 17 00:00:00 2001 From: gitlost Date: Tue, 27 Jun 2017 11:24:48 +0100 Subject: [PATCH 09/15] Try ImageMagick 7.0.5-10 for PHP 7.1 - might be less flakey. --- bin/install-imagick.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/install-imagick.sh b/bin/install-imagick.sh index 71efb6713..39bf5cd0b 100644 --- a/bin/install-imagick.sh +++ b/bin/install-imagick.sh @@ -7,7 +7,7 @@ IMAGEMAGICK_VERSION='6.9.7-10' IMAGICK_VERSION='3.4.3' if [[ "$TRAVIS_PHP_VERSION" = '7.1' ]]; then - IMAGEMAGICK_VERSION='7.0.4-10' + IMAGEMAGICK_VERSION='7.0.5-10' fi # Based on http://stackoverflow.com/a/41138688/664741 From 95f1a08a1d65c2a1a616b0ae43cae9110cd46f7b Mon Sep 17 00:00:00 2001 From: gitlost Date: Tue, 27 Jun 2017 12:03:41 +0100 Subject: [PATCH 10/15] Try ImageMagick 6.5.3-10 for all - oldest supported by Imagick 3.4.3. --- bin/install-imagick.sh | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/bin/install-imagick.sh b/bin/install-imagick.sh index 39bf5cd0b..91b899130 100644 --- a/bin/install-imagick.sh +++ b/bin/install-imagick.sh @@ -3,13 +3,9 @@ set -ex # ImageMagick/Imagick versions to use. -IMAGEMAGICK_VERSION='6.9.7-10' +IMAGEMAGICK_VERSION='6.5.3-10' IMAGICK_VERSION='3.4.3' -if [[ "$TRAVIS_PHP_VERSION" = '7.1' ]]; then - IMAGEMAGICK_VERSION='7.0.5-10' -fi - # Based on http://stackoverflow.com/a/41138688/664741 install_imagemagick() { curl -O "https://www.imagemagick.org/download/releases/ImageMagick-$IMAGEMAGICK_VERSION.tar.xz" -f From 465fc55cf5cc475ab7b8872d9935895f061d47f9 Mon Sep 17 00:00:00 2001 From: gitlost Date: Tue, 27 Jun 2017 12:07:02 +0100 Subject: [PATCH 11/15] Ok try 6.5.4-10 - oldest actually available on download site. --- bin/install-imagick.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/install-imagick.sh b/bin/install-imagick.sh index 91b899130..fa9c692a1 100644 --- a/bin/install-imagick.sh +++ b/bin/install-imagick.sh @@ -3,7 +3,7 @@ set -ex # ImageMagick/Imagick versions to use. -IMAGEMAGICK_VERSION='6.5.3-10' +IMAGEMAGICK_VERSION='6.5.4-10' IMAGICK_VERSION='3.4.3' # Based on http://stackoverflow.com/a/41138688/664741 From 33e46f6d9399dfd21c737dedf4c19bcd5e770c00 Mon Sep 17 00:00:00 2001 From: gitlost Date: Tue, 27 Jun 2017 12:25:04 +0100 Subject: [PATCH 12/15] Try ImageMagick 7.0.3 for PHP 7.1. --- bin/install-imagick.sh | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/bin/install-imagick.sh b/bin/install-imagick.sh index fa9c692a1..46fa48538 100644 --- a/bin/install-imagick.sh +++ b/bin/install-imagick.sh @@ -6,6 +6,10 @@ set -ex IMAGEMAGICK_VERSION='6.5.4-10' IMAGICK_VERSION='3.4.3' +if [[ "$TRAVIS_PHP_VERSION" = 7.1 ]]; then + IMAGEMAGICK_VERSION='7.0.3-10' +fi + # Based on http://stackoverflow.com/a/41138688/664741 install_imagemagick() { curl -O "https://www.imagemagick.org/download/releases/ImageMagick-$IMAGEMAGICK_VERSION.tar.xz" -f From 4b36432b4333c8aee4774a2507688e7841ea6110 Mon Sep 17 00:00:00 2001 From: gitlost Date: Tue, 27 Jun 2017 13:13:57 +0100 Subject: [PATCH 13/15] Eenie meenie ImageMagick 5.5.5-10. --- bin/install-imagick.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/install-imagick.sh b/bin/install-imagick.sh index 46fa48538..0fb7eb526 100644 --- a/bin/install-imagick.sh +++ b/bin/install-imagick.sh @@ -3,7 +3,7 @@ set -ex # ImageMagick/Imagick versions to use. -IMAGEMAGICK_VERSION='6.5.4-10' +IMAGEMAGICK_VERSION='6.5.5-10' IMAGICK_VERSION='3.4.3' if [[ "$TRAVIS_PHP_VERSION" = 7.1 ]]; then From c89ce4cb4472f5b0417a3b62f82dec3c248db543 Mon Sep 17 00:00:00 2001 From: gitlost Date: Tue, 27 Jun 2017 13:29:55 +0100 Subject: [PATCH 14/15] Back to ImageMagick 6.9.7-10. --- bin/install-imagick.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/install-imagick.sh b/bin/install-imagick.sh index 0fb7eb526..95aa5b15e 100644 --- a/bin/install-imagick.sh +++ b/bin/install-imagick.sh @@ -3,7 +3,7 @@ set -ex # ImageMagick/Imagick versions to use. -IMAGEMAGICK_VERSION='6.5.5-10' +IMAGEMAGICK_VERSION='6.9.7-10' IMAGICK_VERSION='3.4.3' if [[ "$TRAVIS_PHP_VERSION" = 7.1 ]]; then From 95f91f636c031b1baf31f7e1a57b7c59b2fb5b1f Mon Sep 17 00:00:00 2001 From: gitlost Date: Tue, 27 Jun 2017 14:00:28 +0100 Subject: [PATCH 15/15] Ah shure why not 6.9.6-8. --- bin/install-imagick.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/install-imagick.sh b/bin/install-imagick.sh index 95aa5b15e..d20339d44 100644 --- a/bin/install-imagick.sh +++ b/bin/install-imagick.sh @@ -3,7 +3,7 @@ set -ex # ImageMagick/Imagick versions to use. -IMAGEMAGICK_VERSION='6.9.7-10' +IMAGEMAGICK_VERSION='6.9.6-8' IMAGICK_VERSION='3.4.3' if [[ "$TRAVIS_PHP_VERSION" = 7.1 ]]; then