Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions bin/install-imagick.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
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
IMAGEMAGICK_VERSION='7.0.4-10'
if [[ "$TRAVIS_PHP_VERSION" = 7.1 ]]; then
IMAGEMAGICK_VERSION='7.0.3-10'
fi

# Based on http://stackoverflow.com/a/41138688/664741
Expand Down
19 changes: 19 additions & 0 deletions features/media-import.feature
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,25 @@ 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 --skip-copy`
Then STDOUT should contain:
"""
Imported file
"""
And STDOUT should contain:
"""
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


Scenario: Import a file and use its filename as the title
Given download:
| path | url |
Expand Down
95 changes: 57 additions & 38 deletions src/Media_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down Expand Up @@ -173,6 +174,9 @@ function regenerate( $args, $assoc_args = array() ) {
* [--desc=<description>]
* : "Description" field (post content) of attachment post.
*
* [--skip-copy]
* : 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.
*
Expand Down Expand Up @@ -222,8 +226,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 {
Expand All @@ -236,12 +240,16 @@ 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;
}
$tempfile = $this->make_copy( $file );
if ( \WP_CLI\Utils\get_flag_value( $assoc_args, 'skip-copy' ) ) {
$tempfile = $file;
} else {
$tempfile = $this->make_copy( $file );
}
} else {
$tempfile = download_url( $file );
if ( is_wp_error( $tempfile ) ) {
Expand All @@ -256,17 +264,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 );
Expand All @@ -286,33 +294,49 @@ 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, 'skip-copy' ) ) {
$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_CLI::warning( sprintf(
"Unable to insert file '%s'. Reason: %s",
$orig_filename, implode( ', ', $success->get_error_messages() )
) );
$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 );
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.
// 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 );
}

$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' ) ) {
Expand All @@ -334,18 +358,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;
}
Expand All @@ -356,7 +378,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;
}
Expand Down Expand Up @@ -418,19 +440,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.

Expand All @@ -454,12 +474,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;
Expand Down