Make WordPress Core

Changeset 48510


Ignore:
Timestamp:
07/19/2020 09:17:55 PM (4 years ago)
Author:
azaozz
Message:

Media: Migrate the data from the attachment post of the parent image when saving an edited image. Copy:

  • post_title,
  • post_content (image description),
  • post_excerpt (image caption as saved in the DB),
  • _wp_attachment_image_alt meta (alt text for the img tag as saved in the DB).

Props spacedmonkey, joedolson, TimothyBlynJacobs, azaozz.
Fixes #50675.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php

    r48498 r48510  
    546546
    547547        // Create new attachment post.
    548         $attachment_post = array(
     548        $new_attachment_post = array(
    549549            'post_mime_type' => $saved['mime-type'],
    550550            'guid'           => $uploads['url'] . "/$filename",
    551             'post_title'     => $filename,
     551            'post_title'     => $image_name,
    552552            'post_content'   => '',
    553553        );
    554554
    555         $new_attachment_id = wp_insert_attachment( wp_slash( $attachment_post ), $saved['path'], 0, true );
     555        // Copy post_content, post_excerpt, and post_title from the edited image's attachment post.
     556        $attachment_post = get_post( $attachment_id );
     557
     558        if ( $attachment_post ) {
     559            $new_attachment_post['post_content'] = $attachment_post->post_content;
     560            $new_attachment_post['post_excerpt'] = $attachment_post->post_excerpt;
     561            $new_attachment_post['post_title']   = $attachment_post->post_title;
     562        }
     563
     564        $new_attachment_id = wp_insert_attachment( wp_slash( $new_attachment_post ), $saved['path'], 0, true );
    556565
    557566        if ( is_wp_error( $new_attachment_id ) ) {
     
    565574        }
    566575
     576        // Copy the image alt text from the edited image.
     577        $image_alt = get_post_meta( $attachment_id, '_wp_attachment_image_alt', true );
     578
     579        if ( ! empty( $image_alt ) ) {
     580            // update_post_meta() expects slashed.
     581            update_post_meta( $new_attachment_id, '_wp_attachment_image_alt', wp_slash( $image_alt ) );
     582        }
     583
    567584        // Generate image sub-sizes and meta.
    568585        $new_image_meta = wp_generate_attachment_metadata( $new_attachment_id, $saved['path'] );
    569586
    570587        // Copy the EXIF metadata from the original attachment if not generated for the edited image.
    571         if ( ! empty( $image_meta['image_meta'] ) ) {
    572             $empty_image_meta = true;
    573 
    574             if ( isset( $new_image_meta['image_meta'] ) && is_array( $new_image_meta['image_meta'] ) ) {
    575                 $empty_image_meta = empty( array_filter( array_values( $new_image_meta['image_meta'] ) ) );
    576             }
    577 
    578             if ( $empty_image_meta ) {
    579                 $new_image_meta['image_meta'] = $image_meta['image_meta'];
     588        if ( isset( $image_meta['image_meta'] ) && isset( $new_image_meta['image_meta'] ) && is_array( $new_image_meta['image_meta'] ) ) {
     589            // Merge but skip empty values.
     590            foreach ( (array) $image_meta['image_meta'] as $key => $value ) {
     591                if ( empty( $new_image_meta['image_meta'][ $key ] ) && ! empty( $value ) ) {
     592                    $new_image_meta['image_meta'][ $key ] = $value;
     593                }
    580594            }
    581595        }
Note: See TracChangeset for help on using the changeset viewer.