Make WordPress Core

Ticket #50675: 50675.diff

File 50675.diff, 2.4 KB (added by azaozz, 4 years ago)
  • src/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php

     
    541541                }
    542542
    543543                // Create new attachment post.
    544                 $attachment_post = array(
     544                $new_attachment_post = array(
    545545                        'post_mime_type' => $saved['mime-type'],
    546546                        'guid'           => $uploads['url'] . "/$filename",
    547547                        'post_title'     => $filename,
     
    548548                        'post_content'   => '',
    549549                );
    550550
    551                 $new_attachment_id = wp_insert_attachment( wp_slash( $attachment_post ), $saved['path'], 0, true );
     551                // Copy post_content and post_excerps from the edited image's attachment post.
     552                $attachment_post = get_post( $attachment_id );
    552553
     554                if ( $attachment_post ) {
     555                        $new_attachment_post['post_content'] = $attachment_post->post_content;
     556                        $new_attachment_post['post_excerpt'] = $attachment_post->post_excerpt;
     557                }
     558
     559                $new_attachment_id = wp_insert_attachment( wp_slash( $new_attachment_post ), $saved['path'], 0, true );
     560
    553561                if ( is_wp_error( $new_attachment_id ) ) {
    554562                        if ( 'db_update_error' === $new_attachment_id->get_error_code() ) {
    555563                                $new_attachment_id->add_data( array( 'status' => 500 ) );
     
    564572                $new_image_meta = wp_generate_attachment_metadata( $new_attachment_id, $saved['path'] );
    565573
    566574                // Copy the EXIF metadata from the original attachment if not generated for the edited image.
    567                 if ( ! empty( $image_meta['image_meta'] ) ) {
    568                         $empty_image_meta = true;
    569 
    570                         if ( isset( $new_image_meta['image_meta'] ) && is_array( $new_image_meta['image_meta'] ) ) {
    571                                 $empty_image_meta = empty( array_filter( array_values( $new_image_meta['image_meta'] ) ) );
     575                if ( isset( $image_meta['image_meta'] ) && isset( $new_image_meta['image_meta'] ) && is_array( $new_image_meta['image_meta'] ) ) {
     576                        // Merge but skip empty values.
     577                        foreach( (array) $image_meta['image_meta'] as $key => $value ) {
     578                                if ( empty( $new_image_meta['image_meta'][ $key ] ) && ! empty( $value ) ) {
     579                                        $new_image_meta['image_meta'][ $key ] = $value;
     580                                }
    572581                        }
    573 
    574                         if ( $empty_image_meta ) {
    575                                 $new_image_meta['image_meta'] = $image_meta['image_meta'];
    576                         }
    577582                }
    578583
    579584                // Reset orientation. At this point the image is edited and orientation is correct.