Skip to content

Commit 94f5ebb

Browse files
Media: Guard the image size lookup in image_constrain_size_for_editor().
This ensures that passing a size which is not a valid array key does not cause a PHP error. Developed in #13032. Follow-up to r63177. Props mukesh27, im3dabasia1, softglaze. Fixes #65842. git-svn-id: https://develop.svn.wordpress.org/trunk@64867 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 3f973bc commit 94f5ebb

2 files changed

Lines changed: 59 additions & 1 deletion

File tree

‎src/wp-includes/media.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ function image_constrain_size_for_editor( $width, $height, $size = 'medium', $co
114114
if ( (int) $content_width > 0 ) {
115115
$max_width = min( (int) $content_width, $max_width );
116116
}
117-
} elseif ( isset( $_wp_additional_image_sizes[ $size ] ) ) {
117+
} elseif ( ( is_string( $size ) || is_int( $size ) ) && isset( $_wp_additional_image_sizes[ $size ] ) ) {
118118
$max_width = (int) $_wp_additional_image_sizes[ $size ]['width'];
119119
$max_height = (int) $_wp_additional_image_sizes[ $size ]['height'];
120120
// Only in admin. Assume that theme authors know what they're doing.

‎tests/phpunit/tests/image/size.php‎

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,4 +208,62 @@ public function test_constrain_size_for_editor_full() {
208208

209209
$content_width = $_content_width;
210210
}
211+
212+
/**
213+
* Tests that an additional image size registered via add_image_size() is used
214+
* to constrain the dimensions.
215+
*
216+
* @ticket 65842
217+
*/
218+
public function test_constrain_size_for_editor_additional_image_size() {
219+
add_image_size( 'test-size', 300, 200 );
220+
221+
$out = image_constrain_size_for_editor( 600, 400, 'test-size' );
222+
223+
remove_image_size( 'test-size' );
224+
225+
$this->assertSame( array( 300, 200 ), $out );
226+
}
227+
228+
/**
229+
* Tests that a size which is not a valid array key does not cause a PHP error.
230+
*
231+
* A size that does not match a registered image size should fall through to
232+
* the unconstrained branch, without being used as an array offset.
233+
*
234+
* On PHP 8.5, using null as an array offset emits a deprecation notice, and
235+
* using an object or a float emits a TypeError or a deprecation notice
236+
* respectively. The test suite converts these into test failures.
237+
*
238+
* @ticket 65842
239+
*
240+
* @dataProvider data_constrain_size_for_editor_invalid_size
241+
*
242+
* @param mixed $size Requested image size.
243+
*/
244+
public function test_constrain_size_for_editor_invalid_size( $size ) {
245+
add_image_size( 'test-size', 300, 200 );
246+
247+
$out = image_constrain_size_for_editor( 600, 400, $size );
248+
249+
remove_image_size( 'test-size' );
250+
251+
$this->assertSame( array( 600, 400 ), $out );
252+
}
253+
254+
/**
255+
* Data provider.
256+
*
257+
* @return array[]
258+
*/
259+
public function data_constrain_size_for_editor_invalid_size() {
260+
return array(
261+
'null' => array( null ),
262+
'false' => array( false ),
263+
'empty string' => array( '' ),
264+
'integer zero' => array( 0 ),
265+
'a float' => array( 1.5 ),
266+
'an object' => array( new stdClass() ),
267+
);
268+
}
211269
}

0 commit comments

Comments
 (0)