Make WordPress Core


Ignore:
Timestamp:
03/02/2024 08:13:02 PM (5 months ago)
Author:
joedolson
Message:

Media: Accessibility: Copy attachment properties on site icon crop.

Add parity between site icon, custom header, and default image crop behaviors. [53027] fixed a bug where alt text and caption were not copied on custom headers, but did not apply that change in any other context.

Deprecate the create_attachment_object method in the Wp_Site_Icon and Custom_Image_Header classes and replace that functionality with the new function wp_copy_parent_attachment_properties() to improve consistency.

Props afercia, rcreators, jorbin, joedolson, huzaifaalmesbah, shailu25, swissspidy, mukesh27.
Fixes #60524.

File:
1 edited

Legend:

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

    r57648 r57755  
    40364036
    40374037            /** This filter is documented in wp-admin/includes/class-custom-image-header.php */
    4038             $cropped    = apply_filters( 'wp_create_file_in_uploads', $cropped, $attachment_id ); // For replication.
    4039             $attachment = $wp_site_icon->create_attachment_object( $cropped, $attachment_id );
    4040             unset( $attachment['ID'] );
     4038            $cropped = apply_filters( 'wp_create_file_in_uploads', $cropped, $attachment_id ); // For replication.
     4039
     4040            // Copy attachment properties.
     4041            $attachment = wp_copy_parent_attachment_properties( $cropped, $attachment_id, $context );
    40414042
    40424043            // Update the attachment.
     
    40664067            $cropped = apply_filters( 'wp_create_file_in_uploads', $cropped, $attachment_id ); // For replication.
    40674068
    4068             $parent_url      = wp_get_attachment_url( $attachment_id );
    4069             $parent_basename = wp_basename( $parent_url );
    4070             $url             = str_replace( $parent_basename, wp_basename( $cropped ), $parent_url );
    4071 
    4072             $size       = wp_getimagesize( $cropped );
    4073             $image_type = ( $size ) ? $size['mime'] : 'image/jpeg';
    4074 
    4075             // Get the original image's post to pre-populate the cropped image.
    4076             $original_attachment  = get_post( $attachment_id );
    4077             $sanitized_post_title = sanitize_file_name( $original_attachment->post_title );
    4078             $use_original_title   = (
    4079                 ( '' !== trim( $original_attachment->post_title ) ) &&
    4080                 /*
    4081                  * Check if the original image has a title other than the "filename" default,
    4082                  * meaning the image had a title when originally uploaded or its title was edited.
    4083                  */
    4084                 ( $parent_basename !== $sanitized_post_title ) &&
    4085                 ( pathinfo( $parent_basename, PATHINFO_FILENAME ) !== $sanitized_post_title )
    4086             );
    4087             $use_original_description = ( '' !== trim( $original_attachment->post_content ) );
    4088 
    4089             $attachment = array(
    4090                 'post_title'     => $use_original_title ? $original_attachment->post_title : wp_basename( $cropped ),
    4091                 'post_content'   => $use_original_description ? $original_attachment->post_content : $url,
    4092                 'post_mime_type' => $image_type,
    4093                 'guid'           => $url,
    4094                 'context'        => $context,
    4095             );
    4096 
    4097             // Copy the image caption attribute (post_excerpt field) from the original image.
    4098             if ( '' !== trim( $original_attachment->post_excerpt ) ) {
    4099                 $attachment['post_excerpt'] = $original_attachment->post_excerpt;
    4100             }
    4101 
    4102             // Copy the image alt text attribute from the original image.
    4103             if ( '' !== trim( $original_attachment->_wp_attachment_image_alt ) ) {
    4104                 $attachment['meta_input'] = array(
    4105                     '_wp_attachment_image_alt' => wp_slash( $original_attachment->_wp_attachment_image_alt ),
    4106                 );
    4107             }
     4069            // Copy attachment properties.
     4070            $attachment = wp_copy_parent_attachment_properties( $cropped, $attachment_id, $context );
    41084071
    41094072            $attachment_id = wp_insert_attachment( $attachment, $cropped );
Note: See TracChangeset for help on using the changeset viewer.