WordPress.org

Make WordPress Core

Changeset 48557


Ignore:
Timestamp:
07/21/2020 11:12:22 PM (4 months ago)
Author:
SergeyBiryukov
Message:

Widgets: Make sure image widgets with custom image size render captions.

Props kasparsd, JavierCasares, audrasjb.
Fixes #50160.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/widgets/class-wp-widget-media-image.php

    r47055 r48557  
    185185
    186186        $attachment = null;
     187
    187188        if ( $this->is_attachment_with_mime_type( $instance['attachment_id'], $this->widget_options['mime_type'] ) ) {
    188189            $attachment = get_post( $instance['attachment_id'] );
    189190        }
     191
    190192        if ( $attachment ) {
    191193            $caption = '';
     
    209211
    210212            $size = $instance['size'];
     213
    211214            if ( 'custom' === $size || ! in_array( $size, array_merge( get_intermediate_image_sizes(), array( 'full' ) ), true ) ) {
    212                 $size = array( $instance['width'], $instance['height'] );
    213             }
     215                $size  = array( $instance['width'], $instance['height'] );
     216                $width = $instance['width'];
     217            } else {
     218                $caption_size = _wp_get_image_size_from_meta( $instance['size'], wp_get_attachment_metadata( $attachment->ID ) );
     219                $width        = empty( $caption_size[0] ) ? 0 : $caption_size[0];
     220            }
     221
    214222            $image_attributes['class'] .= sprintf( ' attachment-%1$s size-%1$s', is_array( $size ) ? join( 'x', $size ) : $size );
    215223
    216224            $image = wp_get_attachment_image( $attachment->ID, $size, false, $image_attributes );
    217 
    218             $caption_size = _wp_get_image_size_from_meta( $instance['size'], wp_get_attachment_metadata( $attachment->ID ) );
    219             $width        = empty( $caption_size[0] ) ? 0 : $caption_size[0];
    220225
    221226        } else {
  • trunk/tests/phpunit/tests/widgets/media-image-widget.php

    r48464 r48557  
    586586        $this->assertContains( 'class="wp-caption alignnone"', $output );
    587587        $this->assertContains( '<p class="wp-caption-text">Custom caption</p>', $output );
     588
     589        // Attachments with custom sizes can render captions.
     590        ob_start();
     591        $widget->render_media(
     592            array(
     593                'attachment_id' => $attachment_id,
     594                'size'          => 'custom',
     595                'width'         => '300',
     596                'height'        => '200',
     597                'caption'       => 'Caption for an image with custom size',
     598            )
     599        );
     600        $output = ob_get_clean();
     601        $this->assertContains( 'style="width: 310px"', $output );
     602        $this->assertContains( '<p class="wp-caption-text">Caption for an image with custom size</p>', $output );
    588603    }
    589604
Note: See TracChangeset for help on using the changeset viewer.