Make WordPress Core

Ticket #50711: 50711-extended.diff

File 50711-extended.diff, 4.0 KB (added by azaozz, 5 years ago)
  • src/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php

     
    484484                        );
    485485                }
    486486
    487                 if ( 0 !== $rotate ) {
    488                         $result = $image_editor->rotate( $rotate );
     487                // Create the attachment post now. This will allow retrying of the image post-processing
     488                // if the server is busy at the moment.
    489489
    490                         if ( is_wp_error( $result ) ) {
    491                                 return new WP_Error(
    492                                         'rest_image_rotation_failed',
    493                                         __( 'Unable to rotate this image.' ),
    494                                         array( 'status' => 500 )
    495                                 );
    496                         }
    497                 }
    498 
    499                 if ( $crop ) {
    500                         $size = $image_editor->get_size();
    501 
    502                         $crop_x = round( ( $size['width'] * floatval( $request['x'] ) ) / 100.0 );
    503                         $crop_y = round( ( $size['height'] * floatval( $request['y'] ) ) / 100.0 );
    504                         $width  = round( ( $size['width'] * floatval( $request['width'] ) ) / 100.0 );
    505                         $height = round( ( $size['height'] * floatval( $request['height'] ) ) / 100.0 );
    506 
    507                         $result = $image_editor->crop( $crop_x, $crop_y, $width, $height );
    508 
    509                         if ( is_wp_error( $result ) ) {
    510                                 return new WP_Error(
    511                                         'rest_image_crop_failed',
    512                                         __( 'Unable to crop this image.' ),
    513                                         array( 'status' => 500 )
    514                                 );
    515                         }
    516                 }
    517 
    518490                // Calculate the file name.
    519491                $image_ext  = pathinfo( $image_file, PATHINFO_EXTENSION );
    520492                $image_name = wp_basename( $image_file, ".{$image_ext}" );
     
    537509                // Make the file name unique in the (new) upload directory.
    538510                $filename = wp_unique_filename( $uploads['path'], $filename );
    539511
    540                 // Save to disk.
    541                 $saved = $image_editor->save( $uploads['path'] . "/$filename" );
    542 
    543                 if ( is_wp_error( $saved ) ) {
    544                         return $saved;
    545                 }
    546 
    547512                // Create new attachment post.
    548513                $new_attachment_post = array(
    549                         'post_mime_type' => $saved['mime-type'],
     514                        'post_mime_type' => $mime_type,
    550515                        'guid'           => $uploads['url'] . "/$filename",
    551516                        'post_title'     => $image_name,
    552517                        'post_content'   => '',
     
    581546                        update_post_meta( $new_attachment_id, '_wp_attachment_image_alt', wp_slash( $image_alt ) );
    582547                }
    583548
     549                if ( defined( 'REST_REQUEST' ) && REST_REQUEST ) {
     550                        // Set a custom header with the attachment_id.
     551                        // Used by the browser/client to resume creating image sub-sizes after a PHP fatal error.
     552                        header( 'X-WP-Upload-Attachment-ID: ' . $new_attachment_id );
     553                }
     554
     555                // Edit the image.
     556                if ( 0 !== $rotate ) {
     557                        $result = $image_editor->rotate( $rotate );
     558
     559                        if ( is_wp_error( $result ) ) {
     560                                return new WP_Error(
     561                                        'rest_image_rotation_failed',
     562                                        __( 'Unable to rotate this image.' ),
     563                                        array( 'status' => 500 )
     564                                );
     565                        }
     566                }
     567
     568                if ( $crop ) {
     569                        $size = $image_editor->get_size();
     570
     571                        $crop_x = round( ( $size['width'] * floatval( $request['x'] ) ) / 100.0 );
     572                        $crop_y = round( ( $size['height'] * floatval( $request['y'] ) ) / 100.0 );
     573                        $width  = round( ( $size['width'] * floatval( $request['width'] ) ) / 100.0 );
     574                        $height = round( ( $size['height'] * floatval( $request['height'] ) ) / 100.0 );
     575
     576                        $result = $image_editor->crop( $crop_x, $crop_y, $width, $height );
     577
     578                        if ( is_wp_error( $result ) ) {
     579                                return new WP_Error(
     580                                        'rest_image_crop_failed',
     581                                        __( 'Unable to crop this image.' ),
     582                                        array( 'status' => 500 )
     583                                );
     584                        }
     585                }
     586
     587                // Save to disk.
     588                $saved = $image_editor->save( $uploads['path'] . "/$filename" );
     589
     590                if ( is_wp_error( $saved ) ) {
     591                        return $saved;
     592                }
     593
     594                // Ensure the new attachment mime type matches the image mime type as returned by $image_editor->save().
     595                if ( $mime_type !== $saved['mime-type'] ) {
     596                        wp_update_post(
     597                                array(
     598                                        'ID'             => $new_attachment_id,
     599                                        'post_mime_type' => $saved['mime-type'],
     600                                )
     601                        );
     602                }
     603
    584604                // Generate image sub-sizes and meta.
    585605                $new_image_meta = wp_generate_attachment_metadata( $new_attachment_id, $saved['path'] );
    586606