Skip to content

Commit

Permalink
Merge pull request #103 from jcomack/refactor-image-size-collection
Browse files Browse the repository at this point in the history
Refactored aspects of the image collection and determination of sizes
  • Loading branch information
schlessera authored Apr 21, 2019
2 parents 885b5e6 + a3a5e49 commit 68e2402
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 68 deletions.
20 changes: 14 additions & 6 deletions features/media-image-size.feature
Original file line number Diff line number Diff line change
@@ -1,18 +1,26 @@
Feature: List image sizes

@require-wp-4.8
Scenario: Basic usage
Given a WP install

When I run `wp media image-size`
Then STDOUT should be a table containing rows:
| name | width | height | crop | ratio |
| full | | | N/A | N/A |
| large | 1024 | 1024 | soft | N/A |
| name | width | height | crop | ratio |
| full | | | N/A | N/A |
| post-thumbnail | 1568 | 9999 | soft | N/A |
| large | 1024 | 1024 | soft | N/A |
| medium_large | 768 | 0 | soft | N/A |
| medium | 300 | 300 | soft | N/A |
| thumbnail | 150 | 150 | hard | 1:1 |
And STDERR should be empty

When I run `wp media image-size --skip-themes`
Then STDOUT should be a table containing rows:
| name | width | height | crop | ratio |
| full | | | N/A | N/A |
| large | 1024 | 1024 | soft | N/A |
| name | width | height | crop | ratio |
| full | | | N/A | N/A |
| large | 1024 | 1024 | soft | N/A |
| medium_large | 768 | 0 | soft | N/A |
| medium | 300 | 300 | soft | N/A |
| thumbnail | 150 | 150 | hard | 1:1 |
And STDERR should be empty
150 changes: 88 additions & 62 deletions src/Media_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,22 +124,14 @@ public function regenerate( $args, $assoc_args = array() ) {
$skip_delete = true;
}

$mime_types = array( 'image' );
$additional_mime_types = array();

if ( Utils\wp_version_compare( '4.7', '>=' ) ) {
$mime_types[] = 'application/pdf';
$additional_mime_types[] = 'application/pdf';
}
$query_args = array(
'post_type' => 'attachment',
'post__in' => $args,
'post_mime_type' => $mime_types,
'post_status' => 'any',
'posts_per_page' => - 1,
'fields' => 'ids',
);

$images = new WP_Query( $query_args );

$count = $images->post_count;
$images = $this->get_images( $args, $additional_mime_types );
$count = $images->post_count;

if ( ! $count ) {
WP_CLI::warning( 'No images found.' );
Expand All @@ -162,12 +154,12 @@ public function regenerate( $args, $assoc_args = array() ) {
$successes = 0;
$errors = 0;
$skips = 0;
foreach ( $images->posts as $id ) {
foreach ( $images->posts as $post ) {
$number++;
if ( 0 === $number % self::WP_CLEAR_OBJECT_CACHE_INTERVAL ) {
Utils\wp_clear_object_cache();
}
$this->process_regeneration( $id, $skip_delete, $only_missing, $image_size, $number . '/' . $count, $successes, $errors, $skips );
$this->process_regeneration( $post->ID, $skip_delete, $only_missing, $image_size, $number . '/' . $count, $successes, $errors, $skips );
}

if ( $image_size ) {
Expand Down Expand Up @@ -484,59 +476,15 @@ public function import( $args, $assoc_args = array() ) {
* @subcommand image-size
*/
public function image_size( $args, $assoc_args ) {
global $_wp_additional_image_sizes;

$soft_ratio_text = 'N/A';

$assoc_args = array_merge(
array(
'fields' => 'name,width,height,crop,ratio',
),
$assoc_args
);

$sizes = array(
array(
'name' => 'large',
'width' => intval( get_option( 'large_size_w' ) ),
'height' => intval( get_option( 'large_size_h' ) ),
'crop' => false !== get_option( 'large_crop' ) ? 'hard' : 'soft',
'ratio' => false !== get_option( 'large_crop' ) ? $this->get_ratio( intval( get_option( 'large_size_w' ) ), intval( get_option( 'large_size_h' ) ) ) : $soft_ratio_text,
),
array(
'name' => 'medium_large',
'width' => intval( get_option( 'medium_large_size_w' ) ),
'height' => intval( get_option( 'medium_large_size_h' ) ),
'crop' => false !== get_option( 'medium_large_crop' ) ? 'hard' : 'soft',
'ratio' => false !== get_option( 'medium_large_crop' ) ? $this->get_ratio( intval( get_option( 'medium_large_size_w' ) ), intval( get_option( 'medium_large_size_h' ) ) ) : $soft_ratio_text,
),
array(
'name' => 'medium',
'width' => intval( get_option( 'medium_size_w' ) ),
'height' => intval( get_option( 'medium_size_h' ) ),
'crop' => false !== get_option( 'medium_crop' ) ? 'hard' : 'soft',
'ratio' => false !== get_option( 'medium_crop' ) ? $this->get_ratio( intval( get_option( 'medium_size_w' ) ), intval( get_option( 'medium_size_h' ) ) ) : $soft_ratio_text,
),
array(
'name' => 'thumbnail',
'width' => intval( get_option( 'thumbnail_size_w' ) ),
'height' => intval( get_option( 'thumbnail_size_h' ) ),
'crop' => false !== get_option( 'thumbnail_crop' ) ? 'hard' : 'soft',
'ratio' => false !== get_option( 'thumbnail_crop' ) ? $this->get_ratio( intval( get_option( 'thumbnail_size_w' ) ), intval( get_option( 'thumbnail_size_h' ) ) ) : $soft_ratio_text,
),
);
if ( is_array( $_wp_additional_image_sizes ) ) {
foreach ( $_wp_additional_image_sizes as $size => $size_args ) {
$crop = filter_var( $size_args['crop'], FILTER_VALIDATE_BOOLEAN );
$sizes[] = array(
'name' => $size,
'width' => $size_args['width'],
'height' => $size_args['height'],
'crop' => empty( $crop ) || is_array( $size_args['crop'] ) ? 'soft' : 'hard',
'ratio' => empty( $crop ) || is_array( $size_args['crop'] ) ? $soft_ratio_text : $this->get_ratio( $size_args['width'], $size_args['height'] ),
);
}
}
$sizes = $this->get_registered_image_sizes();

usort(
$sizes,
function( $a, $b ) {
Expand All @@ -553,7 +501,7 @@ function( $a, $b ) {
'width' => '',
'height' => '',
'crop' => 'N/A',
'ratio' => $soft_ratio_text,
'ratio' => 'N/A',
)
);
WP_CLI\Utils\format_items( $assoc_args['format'], $sizes, explode( ',', $assoc_args['fields'] ) );
Expand Down Expand Up @@ -942,4 +890,82 @@ private function update_attachment_metadata_for_image_size( $id, $new_metadata,
return false;
}

/**
* Get images from the installation.
*
* @param array $args The query arguments to use. Optional.
* @param array $additional_mime_types The additional mime types to search for. Optional.
*
* @return WP_Query The query result.
*/
private function get_images( $args = array(), $additional_mime_types = array() ) {
$mime_types = array_merge( array( 'image' ), $additional_mime_types );

$query_args = array(
'post_type' => 'attachment',
'post__in' => $args,
'post_mime_type' => $mime_types,
'post_status' => 'any',
'posts_per_page' => -1,
);

return new WP_Query( $query_args );
}

/**
* Get the metadata for the passed intermediate image size.
*
* @param string $size The image size to get the metadata for.
*
* @return array The image size metadata.
*/
private function get_intermediate_size_metadata( $size ) {
$width = intval( get_option( "{$size}_size_w" ) );
$height = intval( get_option( "{$size}_size_h" ) );
$crop = get_option( "{$size}_crop" );

return array(
'name' => $size,
'width' => $width,
'height' => $height,
'crop' => false !== $crop ? 'hard' : 'soft',
'ratio' => false !== $crop ? $this->get_ratio( $width, $height ) : 'N/A',
);
}

/**
* Get all the registered image sizes along with their dimensions.
*
* @global array $_wp_additional_image_sizes The additional image sizes to parse.
*
* @link https://wordpress.stackexchange.com/a/251602 Original solution.
*
* @return array $image_sizes The image sizes
*/
private function get_registered_image_sizes() {
global $_wp_additional_image_sizes;

$image_sizes = array();
$default_image_sizes = get_intermediate_image_sizes();

foreach ( $default_image_sizes as $size ) {
$image_sizes[] = $this->get_intermediate_size_metadata( $size );
}

if ( is_array( $_wp_additional_image_sizes ) ) {
foreach ( $_wp_additional_image_sizes as $size => $size_args ) {
$crop = filter_var( $size_args['crop'], FILTER_VALIDATE_BOOLEAN );
$image_sizes[] = array(
'name' => $size,
'width' => $size_args['width'],
'height' => $size_args['height'],
'crop' => empty( $crop ) || is_array( $size_args['crop'] ) ? 'soft' : 'hard',
'ratio' => empty( $crop ) || is_array( $size_args['crop'] ) ? 'N/A' : $this->get_ratio( $size_args['width'], $size_args['height'] ),
);
}
}

return $image_sizes;
}

}

0 comments on commit 68e2402

Please sign in to comment.