Make WordPress Core


Ignore:
Timestamp:
03/29/2022 09:46:09 PM (2 years ago)
Author:
joedolson
Message:

Media: Preserve attachment properties on cropping custom logo.

Migrate the alternative text, title, description, and caption of an image over to the cropped copy of the image after cropping. Ensure that characteristics added to an image prior to cropping are not lost.

Props flixos90, Clorith, afercia, antonvlasenko, ironprogrammer, hellofromTonya.
Fixes #37750.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/includes/ajax-actions.php

    r52978 r53027  
    39713971            $cropped = apply_filters( 'wp_create_file_in_uploads', $cropped, $attachment_id ); // For replication.
    39723972
    3973             $parent_url = wp_get_attachment_url( $attachment_id );
    3974             $url        = str_replace( wp_basename( $parent_url ), wp_basename( $cropped ), $parent_url );
     3973            $parent_url      = wp_get_attachment_url( $attachment_id );
     3974            $parent_basename = wp_basename( $parent_url );
     3975            $url             = str_replace( $parent_basename, wp_basename( $cropped ), $parent_url );
    39753976
    39763977            $size       = wp_getimagesize( $cropped );
    39773978            $image_type = ( $size ) ? $size['mime'] : 'image/jpeg';
    39783979
     3980            // Get the original image's post to pre-populate the cropped image.
     3981            $original_attachment      = get_post( $attachment_id );
     3982            $sanitized_post_title     = sanitize_file_name( $original_attachment->post_title );
     3983            $use_original_title       = (
     3984                ( '' !== trim( $original_attachment->post_title ) ) &&
     3985                /*
     3986                 * Check if the original image has a title other than the "filename" default,
     3987                 * meaning the image had a title when originally uploaded or its title was edited.
     3988                 */
     3989                ( $parent_basename !== $sanitized_post_title ) &&
     3990                ( pathinfo( $parent_basename, PATHINFO_FILENAME ) !== $sanitized_post_title )
     3991            );
     3992            $use_original_description = ( '' !== trim( $original_attachment->post_content ) );
     3993
    39793994            $object = array(
    3980                 'post_title'     => wp_basename( $cropped ),
    3981                 'post_content'   => $url,
     3995                'post_title'     => $use_original_title ? $original_attachment->post_title : wp_basename( $cropped ),
     3996                'post_content'   => $use_original_description ? $original_attachment->post_content : $url,
    39823997                'post_mime_type' => $image_type,
    39833998                'guid'           => $url,
    39843999                'context'        => $context,
    39854000            );
     4001
     4002            // Copy the image caption attribute (post_excerpt field) from the original image.
     4003            if ( '' !== trim( $original_attachment->post_excerpt ) ) {
     4004                $object['post_excerpt'] = $original_attachment->post_excerpt;
     4005            }
     4006
     4007            // Copy the image alt text attribute from the original image.
     4008            if ( '' !== trim( $original_attachment->_wp_attachment_image_alt ) ) {
     4009                $object['meta_input'] = array(
     4010                    '_wp_attachment_image_alt' => wp_slash( $original_attachment->_wp_attachment_image_alt ),
     4011                );
     4012            }
    39864013
    39874014            $attachment_id = wp_insert_attachment( $object, $cropped );
Note: See TracChangeset for help on using the changeset viewer.