Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reinstate ghostscript/imagick install; fix bmp name in regenerate test. #69

Merged
merged 4 commits into from Feb 25, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions .travis.yml
Expand Up @@ -47,6 +47,8 @@ before_install:
else
echo "xdebug.ini does not exist"
fi
- bash bin/install-imagick.sh
- php -m

install:
- composer require wp-cli/wp-cli:dev-master
Expand All @@ -58,3 +60,8 @@ before_script:

script:
- bash bin/test.sh

addons:
apt:
packages:
- ghostscript
9 changes: 0 additions & 9 deletions bin/install-ghostscript.sh

This file was deleted.

18 changes: 15 additions & 3 deletions features/media-regenerate.feature
Expand Up @@ -1233,7 +1233,7 @@ Feature: Regenerate WordPress attachments
Scenario: Regenerate image uploaded with no sizes metadata
Given download:
| path | url |
| {CACHE_DIR}/white-200-square.bmp | http://wp-cli.org/behat-data/white-200-square.bmp |
| {CACHE_DIR}/white-160-square.bmp | http://wp-cli.org/behat-data/white-160-square.bmp |
And a wp-content/mu-plugins/media-settings.php file:
"""
<?php
Expand All @@ -1245,6 +1245,10 @@ Feature: Regenerate WordPress attachments
}
return $image_editors;
} );
// Enable BMP as displayable image (for WP < 4.0).
add_filter( 'file_is_displayable_image', function ( $result, $path ) {
return $result ? $result : false !== strpos( $path, '.bmp' );
}, 10, 2 );
"""
And I run `wp option update uploads_use_yearmonth_folders 0`

Expand All @@ -1267,9 +1271,9 @@ Feature: Regenerate WordPress attachments
"""
Success: Regenerated 0 of 1 images (1 skipped).
"""
And STDERR should contain:
And STDERR should not contain:
"""
Warning: No editor could be selected. (ID {BMP_ATTACHMENT_ID})
Warning: No editor could be selected.
"""
And the wp-content/uploads/white-160-square-150x150.bmp file should not exist

Expand Down Expand Up @@ -1464,6 +1468,10 @@ Feature: Regenerate WordPress attachments
"""
Error: Only regenerated 2 of 4 images (1 failed, 1 skipped).
"""
And STDERR should not contain:
"""
Warning: No editor could be selected.
"""
And the return code should be 1

# Make minimal pdf fail.
Expand Down Expand Up @@ -1513,4 +1521,8 @@ Feature: Regenerate WordPress attachments
"""
Error: Only regenerated 1 of 4 images (2 failed, 1 skipped).
"""
And STDERR should not contain:
"""
Warning: No editor could be selected.
"""
And the return code should be 1
44 changes: 14 additions & 30 deletions src/Media_Command.php
Expand Up @@ -654,34 +654,15 @@ private function needs_regeneration( $att_id, $fullsizepath, $is_pdf, $image_siz
// Assume not skipping.
$skip_it = false;

// Note: zero-length string returned if no metadata, for instance if PDF or non-standard image (eg an SVG).
$metadata = wp_get_attachment_metadata($att_id);

if ( ! is_array( $metadata ) ) {
if ( $is_pdf ) {
$editor = wp_get_image_editor( $fullsizepath );
$no_pdf_editor = is_wp_error( $editor );
unset( $editor );
if ( $no_pdf_editor ) {
// No PDF thumbnail generation available, so skip.
$skip_it = true;
return false;
}
// Assume it may be possible to regenerate the PDF thumbnails and allow processing to continue and possibly fail.
return true;
}
// Assume it's not a standard image (eg an SVG) and skip.
$skip_it = true;
return false;
}

// Note that an attachment can have no sizes if it's on or below the thumbnail threshold.

// Check whether there's new sizes or they've changed.
// Check whether there's new sizes or they've changed. Note that an attachment can have no sizes if it's on or below the thumbnail threshold.
$image_sizes = $this->get_intermediate_image_sizes_for_attachment( $fullsizepath, $is_pdf, $metadata );
if ( is_wp_error( $image_sizes ) ) {
if ( 'image_no_editor' === $image_sizes->get_error_code() ) {
// Warn unless PDF.
if ( ! $is_pdf ) {
// Warn unless PDF or non-standard image.
if ( ! $is_pdf && is_array( $metadata ) && ! empty( $metadata['sizes'] ) ) {
WP_CLI::warning( sprintf( '%s (ID %d)', $image_sizes->get_error_message(), $att_id ) );
}
$skip_it = true;
Expand All @@ -692,6 +673,14 @@ private function needs_regeneration( $att_id, $fullsizepath, $is_pdf, $image_siz
return true;
}

// If uploaded when applicable image editor such as Imagick unavailable, the metadata or sizes metadata may not exist.
if ( ! is_array( $metadata ) ) {
$metadata = array();
}
if ( ! isset( $metadata['sizes'] ) || ! is_array( $metadata['sizes'] ) ) {
$metadata['sizes'] = array();
}

if ( $image_size ) {
if ( empty( $image_sizes[ $image_size ] ) ) {
return false;
Expand All @@ -702,12 +691,7 @@ private function needs_regeneration( $att_id, $fullsizepath, $is_pdf, $image_siz
$metadata['sizes'] = array( $image_size => $metadata['sizes'][ $image_size ] );
}

// In certain situations, eg if file uploaded when applicable image editor such as Imagick unavailable, the sizes metadata may not exist.
if ( ! isset( $metadata['sizes'] ) ) {
$metadata['sizes'] = array();
}

if ( $this->image_sizes_differ( $image_sizes, (array) $metadata['sizes'] ) ) {
if ( $this->image_sizes_differ( $image_sizes, $metadata['sizes'] ) ) {
return true;
}

Expand Down Expand Up @@ -822,7 +806,7 @@ private function get_intermediate_sizes( $is_pdf, $metadata ) {
}
}

if ( ! $is_pdf ) {
if ( ! $is_pdf && is_array( $metadata ) ) {
$sizes = apply_filters( 'intermediate_image_sizes_advanced', $sizes, $metadata );
}

Expand Down
5 changes: 5 additions & 0 deletions travis-append.yml
@@ -0,0 +1,5 @@

addons:
apt:
packages:
- ghostscript
2 changes: 2 additions & 0 deletions travis-before_install-append.yml
@@ -0,0 +1,2 @@
- bash bin/install-imagick.sh
- php -m