Make WordPress Core

Changeset 57755


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

Location:
trunk
Files:
1 added
6 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 );
  • trunk/src/wp-admin/includes/class-custom-image-header.php

    r57364 r57755  
    10781078        $cropped = apply_filters( 'wp_create_file_in_uploads', $cropped, $attachment_id ); // For replication.
    10791079
    1080         $attachment = $this->create_attachment_object( $cropped, $attachment_id );
     1080        $attachment = wp_copy_parent_attachment_properties( $cropped, $attachment_id, 'custom-header' );
    10811081
    10821082        if ( ! empty( $_POST['create-new-attachment'] ) ) {
     
    13151315     *
    13161316     * @since 3.9.0
     1317     * @deprecated 6.5.0
    13171318     *
    13181319     * @param string $cropped              Cropped image URL.
     
    13211322     */
    13221323    final public function create_attachment_object( $cropped, $parent_attachment_id ) {
     1324        _deprecated_function( __METHOD__, '6.5.0', 'wp_copy_parent_attachment_properties()' );
    13231325        $parent     = get_post( $parent_attachment_id );
    13241326        $parent_url = wp_get_attachment_url( $parent->ID );
     
    14221424        $cropped = apply_filters( 'wp_create_file_in_uploads', $cropped, $attachment_id ); // For replication.
    14231425
    1424         $attachment = $this->create_attachment_object( $cropped, $attachment_id );
     1426        $attachment = wp_copy_parent_attachment_properties( $cropped, $attachment_id, 'custom-header' );
    14251427
    14261428        $previous = $this->get_previous_crop( $attachment );
  • trunk/src/wp-admin/includes/class-wp-site-icon.php

    r55678 r57755  
    7979     *
    8080     * @since 4.3.0
     81     * @deprecated 6.5.0
    8182     *
    8283     * @param string $cropped              Cropped image URL.
     
    8586     */
    8687    public function create_attachment_object( $cropped, $parent_attachment_id ) {
     88        _deprecated_function( __METHOD__, '6.5.0', 'wp_copy_parent_attachment_properties()' );
     89
    8790        $parent     = get_post( $parent_attachment_id );
    8891        $parent_url = wp_get_attachment_url( $parent->ID );
  • 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
  • trunk/tests/phpunit/tests/image/header.php

    r56548 r57755  
    109109    }
    110110
    111     public function test_create_attachment_object() {
    112         $id = wp_insert_attachment(
    113             array(
    114                 'post_status' => 'publish',
    115                 'post_title'  => 'foo.png',
    116                 'post_type'   => 'post',
    117                 'guid'        => 'http://localhost/foo.png',
    118             )
    119         );
    120 
    121         $cropped = 'foo-cropped.png';
    122 
    123         $object = $this->custom_image_header->create_attachment_object( $cropped, $id );
    124         $this->assertSame( 'foo-cropped.png', $object['post_title'] );
    125         $this->assertSame( 'http://localhost/' . $cropped, $object['guid'] );
    126         $this->assertSame( 'custom-header', $object['context'] );
    127         $this->assertSame( 'image/jpeg', $object['post_mime_type'] );
    128     }
    129 
    130111    public function test_insert_cropped_attachment() {
    131112        $id = wp_insert_attachment(
     
    139120
    140121        $cropped = 'foo-cropped.png';
    141         $object  = $this->custom_image_header->create_attachment_object( $cropped, $id );
     122        $object  = wp_copy_parent_attachment_properties( $cropped, $id, 'custom-header' );
    142123
    143124        $cropped_id = $this->custom_image_header->insert_attachment( $object, $cropped );
     
    162143        // Create inital crop object.
    163144        $cropped_1 = 'foo-cropped-1.png';
    164         $object    = $this->custom_image_header->create_attachment_object( $cropped_1, $id );
     145        $object    = wp_copy_parent_attachment_properties( $cropped_1, $id, 'custom-header' );
    165146
    166147        // Ensure no previous crop exists.
     
    176157        // Create second crop.
    177158        $cropped_2 = 'foo-cropped-2.png';
    178         $object    = $this->custom_image_header->create_attachment_object( $cropped_2, $id );
     159        $object    = wp_copy_parent_attachment_properties( $cropped_2, $id );
    179160
    180161        // Test that a previous crop is found.
  • trunk/tests/phpunit/tests/image/siteIcon.php

    r52010 r57755  
    9999    }
    100100
    101     public function test_create_attachment_object() {
    102         $attachment_id = $this->insert_attachment();
    103         $parent_url    = get_post( $attachment_id )->guid;
    104         $cropped       = str_replace( wp_basename( $parent_url ), 'cropped-test-image.jpg', $parent_url );
    105 
    106         $object = $this->wp_site_icon->create_attachment_object( $cropped, $attachment_id );
    107 
    108         $this->assertSame( $object['post_title'], 'cropped-test-image.jpg' );
    109         $this->assertSame( $object['context'], 'site-icon' );
    110         $this->assertSame( $object['post_mime_type'], 'image/jpeg' );
    111         $this->assertSame( $object['post_content'], $cropped );
    112         $this->assertSame( $object['guid'], $cropped );
    113     }
    114 
    115101    public function test_insert_cropped_attachment() {
    116102        $attachment_id = $this->insert_attachment();
     
    118104        $cropped       = str_replace( wp_basename( $parent_url ), 'cropped-test-image.jpg', $parent_url );
    119105
    120         $object     = $this->wp_site_icon->create_attachment_object( $cropped, $attachment_id );
     106        $object     = wp_copy_parent_attachment_properties( $cropped, $attachment_id, 'site-icon' );
    121107        $cropped_id = $this->wp_site_icon->insert_attachment( $object, $cropped );
    122108
Note: See TracChangeset for help on using the changeset viewer.