Make WordPress Core


Ignore:
Timestamp:
03/02/2024 08:13:02 PM (11 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/image.php

    r57524 r57755  
    481481
    482482    return $image_meta;
     483}
     484
     485/**
     486 * Copy parent attachment properties to newly cropped image.
     487 *
     488 * @since 6.5.0
     489 *
     490 * @param string $cropped              Path to the cropped image file.
     491 * @param int    $parent_attachment_id Parent file Attachment ID.
     492 * @param string $context              Control calling the function.
     493 * @return array Properties of attachment.
     494 */
     495function wp_copy_parent_attachment_properties( $cropped, $parent_attachment_id, $context = '' ) {
     496    $parent          = get_post( $parent_attachment_id );
     497    $parent_url      = wp_get_attachment_url( $parent->ID );
     498    $parent_basename = wp_basename( $parent_url );
     499    $url             = str_replace( wp_basename( $parent_url ), wp_basename( $cropped ), $parent_url );
     500
     501    $size       = wp_getimagesize( $cropped );
     502    $image_type = $size ? $size['mime'] : 'image/jpeg';
     503
     504    $sanitized_post_title = sanitize_file_name( $parent->post_title );
     505    $use_original_title   = (
     506        ( '' !== trim( $parent->post_title ) ) &&
     507        /*
     508         * Check if the original image has a title other than the "filename" default,
     509         * meaning the image had a title when originally uploaded or its title was edited.
     510         */
     511        ( $parent_basename !== $sanitized_post_title ) &&
     512        ( pathinfo( $parent_basename, PATHINFO_FILENAME ) !== $sanitized_post_title )
     513    );
     514    $use_original_description = ( '' !== trim( $parent->post_content ) );
     515
     516    $attachment = array(
     517        'post_title'     => $use_original_title ? $parent->post_title : wp_basename( $cropped ),
     518        'post_content'   => $use_original_description ? $parent->post_content : $url,
     519        'post_mime_type' => $image_type,
     520        'guid'           => $url,
     521        'context'        => $context,
     522    );
     523
     524    // Copy the image caption attribute (post_excerpt field) from the original image.
     525    if ( '' !== trim( $parent->post_excerpt ) ) {
     526        $attachment['post_excerpt'] = $parent->post_excerpt;
     527    }
     528
     529    // Copy the image alt text attribute from the original image.
     530    if ( '' !== trim( $parent->_wp_attachment_image_alt ) ) {
     531        $attachment['meta_input'] = array(
     532            '_wp_attachment_image_alt' => wp_slash( $parent->_wp_attachment_image_alt ),
     533        );
     534    }
     535
     536    $attachment['post_parent'] = $parent_attachment_id;
     537
     538    return $attachment;
    483539}
    484540
Note: See TracChangeset for help on using the changeset viewer.