Skip to content

Commit 2cb2454

Browse files
REST API: Refactor dimension validation in sideload endpoint.
Move the positive-dimensions check to the top of validate_image_dimensions() to apply it universally and eliminate duplication. Reorder sprintf args in the dimension mismatch message to match display order, reducing cognitive load. Props apermo. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 6a95756 commit 2cb2454

1 file changed

Lines changed: 15 additions & 22 deletions

File tree

src/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1980,6 +1980,15 @@ public function sideload_item_permissions_check( $request ) {
19801980
* @return true|WP_Error True if valid, WP_Error if invalid.
19811981
*/
19821982
private function validate_image_dimensions( int $width, int $height, string $image_size, int $attachment_id ) {
1983+
// All image sizes require positive dimensions.
1984+
if ( $width <= 0 || $height <= 0 ) {
1985+
return new WP_Error(
1986+
'rest_upload_invalid_dimensions',
1987+
__( 'Uploaded image must have positive dimensions.' ),
1988+
array( 'status' => 400 )
1989+
);
1990+
}
1991+
19831992
// 'original' size: should match original attachment dimensions.
19841993
if ( 'original' === $image_size ) {
19851994
$metadata = wp_get_attachment_metadata( $attachment_id, true );
@@ -1991,12 +2000,12 @@ private function validate_image_dimensions( int $width, int $height, string $ima
19912000
return new WP_Error(
19922001
'rest_upload_dimension_mismatch',
19932002
sprintf(
1994-
/* translators: 1: Expected width, 2: expected height, 3: actual width, 4: actual height. */
1995-
__( 'Uploaded image dimensions (%3$dx%4$d) do not match original image dimensions (%1$dx%2$d).' ),
1996-
$expected_width,
1997-
$expected_height,
2003+
/* translators: 1: Actual width, 2: actual height, 3: expected width, 4: expected height. */
2004+
__( 'Uploaded image dimensions (%1$dx%2$d) do not match original image dimensions (%3$dx%4$d).' ),
19982005
$width,
1999-
$height
2006+
$height,
2007+
$expected_width,
2008+
$expected_height
20002009
),
20012010
array( 'status' => 400 )
20022011
);
@@ -2005,15 +2014,8 @@ private function validate_image_dimensions( int $width, int $height, string $ima
20052014
return true;
20062015
}
20072016

2008-
// 'full' size (PDF thumbnails) and 'scaled': dimensions must be positive.
2017+
// 'full' size (PDF thumbnails) and 'scaled': no further constraints.
20092018
if ( 'full' === $image_size || 'scaled' === $image_size ) {
2010-
if ( $width <= 0 || $height <= 0 ) {
2011-
return new WP_Error(
2012-
'rest_upload_invalid_dimensions',
2013-
__( 'Uploaded image must have positive dimensions.' ),
2014-
array( 'status' => 400 )
2015-
);
2016-
}
20172019
return true;
20182020
}
20192021

@@ -2032,15 +2034,6 @@ private function validate_image_dimensions( int $width, int $height, string $ima
20322034
$max_width = (int) $size_data['width'];
20332035
$max_height = (int) $size_data['height'];
20342036

2035-
// Dimensions must be positive.
2036-
if ( $width <= 0 || $height <= 0 ) {
2037-
return new WP_Error(
2038-
'rest_upload_invalid_dimensions',
2039-
__( 'Uploaded image must have positive dimensions.' ),
2040-
array( 'status' => 400 )
2041-
);
2042-
}
2043-
20442037
// Validate dimensions don't exceed the registered size maximums.
20452038
// Allow 1px tolerance for rounding differences.
20462039
$tolerance = 1;

0 commit comments

Comments
 (0)