@@ -70,6 +70,8 @@ public function register_routes() {
7070 $ valid_image_sizes [] = 'original ' ;
7171 // Used for PDF thumbnails.
7272 $ valid_image_sizes [] = 'full ' ;
73+ // Client-side big image threshold: sideload the scaled version.
74+ $ valid_image_sizes [] = 'scaled ' ;
7375
7476 register_rest_route (
7577 $ this ->namespace ,
@@ -2053,6 +2055,48 @@ public function sideload_item( WP_REST_Request $request ) {
20532055
20542056 if ( 'original ' === $ image_size ) {
20552057 $ metadata ['original_image ' ] = wp_basename ( $ path );
2058+ } elseif ( 'scaled ' === $ image_size ) {
2059+ // The current attached file is the original; record it as original_image.
2060+ $ current_file = get_attached_file ( $ attachment_id , true );
2061+
2062+ if ( ! $ current_file ) {
2063+ return new WP_Error (
2064+ 'rest_sideload_no_attached_file ' ,
2065+ __ ( 'Unable to retrieve the attached file for this attachment. ' ),
2066+ array ( 'status ' => 404 )
2067+ );
2068+ }
2069+
2070+ $ metadata ['original_image ' ] = wp_basename ( $ current_file );
2071+
2072+ // Validate the scaled image before updating the attached file.
2073+ $ size = wp_getimagesize ( $ path );
2074+ $ filesize = wp_filesize ( $ path );
2075+
2076+ if ( ! $ size || ! $ filesize ) {
2077+ return new WP_Error (
2078+ 'rest_sideload_invalid_image ' ,
2079+ __ ( 'Unable to read the scaled image file. ' ),
2080+ array ( 'status ' => 500 )
2081+ );
2082+ }
2083+
2084+ // Update the attached file to point to the scaled version.
2085+ if (
2086+ get_attached_file ( $ attachment_id , true ) !== $ path &&
2087+ ! update_attached_file ( $ attachment_id , $ path )
2088+ ) {
2089+ return new WP_Error (
2090+ 'rest_sideload_update_attached_file_failed ' ,
2091+ __ ( 'Unable to update the attached file for this attachment. ' ),
2092+ array ( 'status ' => 500 )
2093+ );
2094+ }
2095+
2096+ $ metadata ['width ' ] = $ size [0 ];
2097+ $ metadata ['height ' ] = $ size [1 ];
2098+ $ metadata ['filesize ' ] = $ filesize ;
2099+ $ metadata ['file ' ] = _wp_relative_upload_path ( $ path );
20562100 } else {
20572101 $ metadata ['sizes ' ] = $ metadata ['sizes ' ] ?? array ();
20582102
@@ -2110,7 +2154,7 @@ public function sideload_item( WP_REST_Request $request ) {
21102154 * @return string Filtered file name.
21112155 */
21122156 private static function filter_wp_unique_filename ( $ filename , $ dir , $ number , $ attachment_filename ) {
2113- if ( empty ( $ number ) || ! $ attachment_filename ) {
2157+ if ( ! is_int ( $ number ) || ! $ attachment_filename ) {
21142158 return $ filename ;
21152159 }
21162160
@@ -2123,8 +2167,8 @@ private static function filter_wp_unique_filename( $filename, $dir, $number, $at
21232167 }
21242168
21252169 $ matches = array ();
2126- if ( preg_match ( '/(.*)(- \d+x\d+)- ' . $ number . '$/ ' , $ name , $ matches ) ) {
2127- $ filename_without_suffix = $ matches [1 ] . $ matches [2 ] . ". $ ext " ;
2170+ if ( preg_match ( '/(.*)-( \d+x\d+|scaled )- ' . $ number . '$/ ' , $ name , $ matches ) ) {
2171+ $ filename_without_suffix = $ matches [1 ] . ' - ' . $ matches [2 ] . ". $ ext " ;
21282172 if ( $ matches [1 ] === $ orig_name && ! file_exists ( "$ dir/ $ filename_without_suffix " ) ) {
21292173 return $ filename_without_suffix ;
21302174 }
0 commit comments