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
2 changes: 1 addition & 1 deletion src/wp-includes/media.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ function image_constrain_size_for_editor( $width, $height, $size = 'medium', $co
if ( (int) $content_width > 0 ) {
$max_width = min( (int) $content_width, $max_width );
}
} elseif ( isset( $_wp_additional_image_sizes[ $size ] ) ) {
} elseif ( ( is_string( $size ) || is_int( $size ) ) && isset( $_wp_additional_image_sizes[ $size ] ) ) {
$max_width = (int) $_wp_additional_image_sizes[ $size ]['width'];
$max_height = (int) $_wp_additional_image_sizes[ $size ]['height'];
// Only in admin. Assume that theme authors know what they're doing.
Expand Down
58 changes: 58 additions & 0 deletions tests/phpunit/tests/image/size.php
Original file line number Diff line number Diff line change
Expand Up @@ -208,4 +208,62 @@ public function test_constrain_size_for_editor_full() {

$content_width = $_content_width;
}

/**
* Tests that an additional image size registered via add_image_size() is used
* to constrain the dimensions.
*
* @ticket 65842
*/
public function test_constrain_size_for_editor_additional_image_size() {
add_image_size( 'test-size', 300, 200 );

$out = image_constrain_size_for_editor( 600, 400, 'test-size' );

remove_image_size( 'test-size' );

$this->assertSame( array( 300, 200 ), $out );
}

/**
* Tests that a size which is not a valid array key does not cause a PHP error.
*
* A size that does not match a registered image size should fall through to
* the unconstrained branch, without being used as an array offset.
*
* On PHP 8.5, using null as an array offset emits a deprecation notice, and
* using an object or a float emits a TypeError or a deprecation notice
* respectively. The test suite converts these into test failures.
*
* @ticket 65842
*
* @dataProvider data_constrain_size_for_editor_invalid_size
*
* @param mixed $size Requested image size.
*/
public function test_constrain_size_for_editor_invalid_size( $size ) {
add_image_size( 'test-size', 300, 200 );

$out = image_constrain_size_for_editor( 600, 400, $size );

remove_image_size( 'test-size' );

$this->assertSame( array( 600, 400 ), $out );
}

/**
* Data provider.
*
* @return array[]
*/
public function data_constrain_size_for_editor_invalid_size() {
return array(
'null' => array( null ),
'false' => array( false ),
'empty string' => array( '' ),
'integer zero' => array( 0 ),
'a float' => array( 1.5 ),
'an object' => array( new stdClass() ),
);
}
}
Loading