From 1af126304fac921ca0674878dfdd2e56ec9d3ea2 Mon Sep 17 00:00:00 2001 From: Adam Silverstein Date: Wed, 4 Mar 2026 06:58:44 +0000 Subject: [PATCH] REST API: Add 'scaled' to sideload route image_size enum MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fix an issue where sideloaded images with a ‘-scaled’ suffix would respond with an error. When users upload a very large image in the editor, the client-side media processing sideloads a scaled version of that image with a ‘-scaled’ suffix. Props adamsilverstein, huzaifaalmesbah, westonruter. Fixes #64737. Built from https://develop.svn.wordpress.org/trunk@61809 git-svn-id: http://core.svn.wordpress.org/trunk@61109 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- .../class-wp-rest-attachments-controller.php | 50 +++++++++++++++++-- wp-includes/version.php | 2 +- 2 files changed, 48 insertions(+), 4 deletions(-) diff --git a/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php b/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php index 1a135ba546..848b4e56d7 100644 --- a/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php +++ b/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php @@ -70,6 +70,8 @@ class WP_REST_Attachments_Controller extends WP_REST_Posts_Controller { $valid_image_sizes[] = 'original'; // Used for PDF thumbnails. $valid_image_sizes[] = 'full'; + // Client-side big image threshold: sideload the scaled version. + $valid_image_sizes[] = 'scaled'; register_rest_route( $this->namespace, @@ -2053,6 +2055,48 @@ class WP_REST_Attachments_Controller extends WP_REST_Posts_Controller { if ( 'original' === $image_size ) { $metadata['original_image'] = wp_basename( $path ); + } elseif ( 'scaled' === $image_size ) { + // The current attached file is the original; record it as original_image. + $current_file = get_attached_file( $attachment_id, true ); + + if ( ! $current_file ) { + return new WP_Error( + 'rest_sideload_no_attached_file', + __( 'Unable to retrieve the attached file for this attachment.' ), + array( 'status' => 404 ) + ); + } + + $metadata['original_image'] = wp_basename( $current_file ); + + // Validate the scaled image before updating the attached file. + $size = wp_getimagesize( $path ); + $filesize = wp_filesize( $path ); + + if ( ! $size || ! $filesize ) { + return new WP_Error( + 'rest_sideload_invalid_image', + __( 'Unable to read the scaled image file.' ), + array( 'status' => 500 ) + ); + } + + // Update the attached file to point to the scaled version. + if ( + get_attached_file( $attachment_id, true ) !== $path && + ! update_attached_file( $attachment_id, $path ) + ) { + return new WP_Error( + 'rest_sideload_update_attached_file_failed', + __( 'Unable to update the attached file for this attachment.' ), + array( 'status' => 500 ) + ); + } + + $metadata['width'] = $size[0]; + $metadata['height'] = $size[1]; + $metadata['filesize'] = $filesize; + $metadata['file'] = _wp_relative_upload_path( $path ); } else { $metadata['sizes'] = $metadata['sizes'] ?? array(); @@ -2110,7 +2154,7 @@ class WP_REST_Attachments_Controller extends WP_REST_Posts_Controller { * @return string Filtered file name. */ private static function filter_wp_unique_filename( $filename, $dir, $number, $attachment_filename ) { - if ( empty( $number ) || ! $attachment_filename ) { + if ( ! is_int( $number ) || ! $attachment_filename ) { return $filename; } @@ -2123,8 +2167,8 @@ class WP_REST_Attachments_Controller extends WP_REST_Posts_Controller { } $matches = array(); - if ( preg_match( '/(.*)(-\d+x\d+)-' . $number . '$/', $name, $matches ) ) { - $filename_without_suffix = $matches[1] . $matches[2] . ".$ext"; + if ( preg_match( '/(.*)-(\d+x\d+|scaled)-' . $number . '$/', $name, $matches ) ) { + $filename_without_suffix = $matches[1] . '-' . $matches[2] . ".$ext"; if ( $matches[1] === $orig_name && ! file_exists( "$dir/$filename_without_suffix" ) ) { return $filename_without_suffix; } diff --git a/wp-includes/version.php b/wp-includes/version.php index a269dae076..18b56f461f 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -16,7 +16,7 @@ * * @global string $wp_version */ -$wp_version = '7.0-beta2-61808'; +$wp_version = '7.0-beta2-61809'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.