Skip to content

Commit 210cccf

Browse files
authored
Merge pull request #192 from ernilambar/116-fix-image-size
2 parents 0a79826 + 38cf73a commit 210cccf

File tree

2 files changed

+120
-41
lines changed

2 files changed

+120
-41
lines changed

features/media-image-size.feature

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,53 @@ Feature: List image sizes
2828
| thumbnail | 150 | 150 | hard | 1:1 |
2929
And STDERR should be empty
3030

31+
@require-wp-5.3
32+
Scenario: Basic usage with plugin which registers custom image size
33+
Given a WP install
34+
# Differing themes can have differing default image sizes. Let's stick to one.
35+
And I try `wp theme install twentynineteen --activate`
36+
And a wp-content/plugins/foo-bar.php file:
37+
"""
38+
<?php
39+
/**
40+
* Plugin Name: Foo Bar
41+
* Plugin URI: https://example.com
42+
* Description: Custom plugin.
43+
* Version: 0.1.0
44+
* Author: John Doe
45+
* Author URI: https://johndoe.com/
46+
* License: GPL-2.0+
47+
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
48+
*/
49+
50+
add_action(
51+
'after_setup_theme',
52+
function () {
53+
add_image_size( 'foo-bar-thumb', 50, 50, true );
54+
}
55+
);
56+
"""
57+
And I try `wp plugin activate foo-bar.php`
58+
59+
When I run `wp media image-size`
60+
Then STDOUT should be a table containing rows:
61+
| name | width | height | crop | ratio |
62+
| full | | | N/A | N/A |
63+
| 2048x2048 | 2048 | 2048 | soft | N/A |
64+
| post-thumbnail | 1568 | 9999 | soft | N/A |
65+
| large | 1024 | 1024 | soft | N/A |
66+
| medium_large | 768 | 0 | soft | N/A |
67+
| medium | 300 | 300 | soft | N/A |
68+
| thumbnail | 150 | 150 | hard | 1:1 |
69+
| foo-bar-thumb | 50 | 50 | hard | 1:1 |
70+
And STDERR should be empty
71+
72+
When I run `wp media image-size --format=csv`
73+
Then STDOUT should not contain:
74+
"""
75+
,0,0,
76+
"""
77+
3178
# Behavior changed with WordPress 5.3+, so we're adding separate tests for previous versions.
3279
# Change that impacts this:
3380
# https://core.trac.wordpress.org/ticket/43524

src/Media_Command.php

Lines changed: 73 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -960,60 +960,92 @@ private function get_images( $args = array(), $additional_mime_types = array() )
960960
return new WP_Query( $query_args );
961961
}
962962

963-
/**
964-
* Get the metadata for the passed intermediate image size.
965-
*
966-
* @param string $size The image size to get the metadata for.
967-
*
968-
* @return array The image size metadata.
969-
*/
970-
private function get_intermediate_size_metadata( $size ) {
971-
$width = intval( get_option( "{$size}_size_w" ) );
972-
$height = intval( get_option( "{$size}_size_h" ) );
973-
$crop = get_option( "{$size}_crop" );
974-
975-
return array(
976-
'name' => $size,
977-
'width' => $width,
978-
'height' => $height,
979-
'crop' => false !== $crop ? 'hard' : 'soft',
980-
'ratio' => false !== $crop ? $this->get_ratio( $width, $height ) : 'N/A',
981-
);
982-
}
983-
984963
/**
985964
* Get all the registered image sizes along with their dimensions.
986965
*
987-
* @global array $_wp_additional_image_sizes The additional image sizes to parse.
988-
*
989-
* @link https://wordpress.stackexchange.com/a/251602 Original solution.
990-
*
991966
* @return array $image_sizes The image sizes
992967
*/
993968
private function get_registered_image_sizes() {
994-
global $_wp_additional_image_sizes;
969+
$image_sizes = array();
995970

996-
$image_sizes = array();
997-
$default_image_sizes = get_intermediate_image_sizes();
971+
$all_sizes = $this->wp_get_registered_image_subsizes();
998972

999-
foreach ( $default_image_sizes as $size ) {
1000-
$image_sizes[] = $this->get_intermediate_size_metadata( $size );
973+
foreach ( $all_sizes as $size => $size_args ) {
974+
$crop = filter_var( $size_args['crop'], FILTER_VALIDATE_BOOLEAN );
975+
976+
$image_sizes[] = array(
977+
'name' => $size,
978+
'width' => $size_args['width'],
979+
'height' => $size_args['height'],
980+
'crop' => empty( $crop ) || is_array( $size_args['crop'] ) ? 'soft' : 'hard',
981+
'ratio' => empty( $crop ) || is_array( $size_args['crop'] ) ? 'N/A' : $this->get_ratio( $size_args['width'], $size_args['height'] ),
982+
);
1001983
}
1002984

1003-
if ( is_array( $_wp_additional_image_sizes ) ) {
1004-
foreach ( $_wp_additional_image_sizes as $size => $size_args ) {
1005-
$crop = filter_var( $size_args['crop'], FILTER_VALIDATE_BOOLEAN );
1006-
$image_sizes[] = array(
1007-
'name' => $size,
1008-
'width' => $size_args['width'],
1009-
'height' => $size_args['height'],
1010-
'crop' => empty( $crop ) || is_array( $size_args['crop'] ) ? 'soft' : 'hard',
1011-
'ratio' => empty( $crop ) || is_array( $size_args['crop'] ) ? 'N/A' : $this->get_ratio( $size_args['width'], $size_args['height'] ),
1012-
);
985+
return $image_sizes;
986+
}
987+
988+
/**
989+
* Returns a normalized list of all currently registered image sub-sizes.
990+
*
991+
* If exists, uses output of wp_get_registered_image_subsizes() function (introduced in WP 5.3).
992+
* Definition of this method is modified version of core function wp_get_registered_image_subsizes().
993+
*
994+
* @global array $_wp_additional_image_sizes
995+
*
996+
* @return array[] Associative array of arrays of image sub-size information, keyed by image size name.
997+
*/
998+
private function wp_get_registered_image_subsizes() {
999+
if ( Utils\wp_version_compare( '5.3', '>=' ) ) {
1000+
return wp_get_registered_image_subsizes();
1001+
}
1002+
1003+
global $_wp_additional_image_sizes;
1004+
1005+
$additional_sizes = $_wp_additional_image_sizes ? $_wp_additional_image_sizes : array();
1006+
1007+
$all_sizes = array();
1008+
1009+
foreach ( get_intermediate_image_sizes() as $size_name ) {
1010+
$size_data = array(
1011+
'width' => 0,
1012+
'height' => 0,
1013+
'crop' => false,
1014+
);
1015+
1016+
if ( isset( $additional_sizes[ $size_name ]['width'] ) ) {
1017+
// For sizes added by plugins and themes.
1018+
$size_data['width'] = (int) $additional_sizes[ $size_name ]['width'];
1019+
} else {
1020+
// For default sizes set in options.
1021+
$size_data['width'] = (int) get_option( "{$size_name}_size_w" );
10131022
}
1023+
1024+
if ( isset( $additional_sizes[ $size_name ]['height'] ) ) {
1025+
$size_data['height'] = (int) $additional_sizes[ $size_name ]['height'];
1026+
} else {
1027+
$size_data['height'] = (int) get_option( "{$size_name}_size_h" );
1028+
}
1029+
1030+
if ( empty( $size_data['width'] ) && empty( $size_data['height'] ) ) {
1031+
// This size isn't set.
1032+
continue;
1033+
}
1034+
1035+
if ( isset( $additional_sizes[ $size_name ]['crop'] ) ) {
1036+
$size_data['crop'] = $additional_sizes[ $size_name ]['crop'];
1037+
} else {
1038+
$size_data['crop'] = get_option( "{$size_name}_crop" );
1039+
}
1040+
1041+
if ( ! is_array( $size_data['crop'] ) || empty( $size_data['crop'] ) ) {
1042+
$size_data['crop'] = (bool) $size_data['crop'];
1043+
}
1044+
1045+
$all_sizes[ $size_name ] = $size_data;
10141046
}
10151047

1016-
return $image_sizes;
1048+
return $all_sizes;
10171049
}
10181050

10191051
/**

0 commit comments

Comments
 (0)