Make WordPress Core

Ticket #42350: 42350.2.diff

File 42350.2.diff, 2.5 KB (added by westonruter, 7 years ago)
  • src/wp-includes/widgets/class-wp-widget-media-image.php

    diff --git src/wp-includes/widgets/class-wp-widget-media-image.php src/wp-includes/widgets/class-wp-widget-media-image.php
    index 780ceacad5..cc24f0e56c 100644
    class WP_Widget_Media_Image extends WP_Widget_Media { 
    177177                        $attachment = get_post( $instance['attachment_id'] );
    178178                }
    179179                if ( $attachment ) {
    180                         $caption = $attachment->post_excerpt;
    181                         if ( $instance['caption'] ) {
     180                        $caption = '';
     181                        if ( trim( $instance['caption'] ) ) {
    182182                                $caption = $instance['caption'];
     183                        } elseif ( null === $instance['caption'] ) {
     184                                $caption = $attachment->post_excerpt;
    183185                        }
    184186
    185187                        $image_attributes = array(
  • tests/phpunit/tests/widgets/media-image-widget.php

    diff --git tests/phpunit/tests/widgets/media-image-widget.php tests/phpunit/tests/widgets/media-image-widget.php
    index 408123bef7..e68399229d 100644
    class Test_WP_Widget_Media_Image extends WP_UnitTestCase { 
    425425                $this->assertContains( '<a href="https://example.org"', $output );
    426426                $this->assertContains( 'target="_blank"', $output );
    427427
    428                 // Caption settings.
     428                // Populate caption in attachment.
    429429                wp_update_post( array(
    430430                        'ID' => $attachment_id,
    431431                        'post_excerpt' => 'Default caption',
    432432                ) );
    433433
     434                // If no caption is supplied, then the default is '', and so the caption will not be displayed.
    434435                ob_start();
    435436                $widget->render_media( array(
    436437                        'attachment_id' => $attachment_id,
    437438                ) );
    438439                $output = ob_get_clean();
     440                $this->assertNotContains( 'wp-caption', $output );
     441                $this->assertNotContains( '<p class="wp-caption-text">', $output );
    439442
     443                // If the caption is explicitly null, then the caption of the underlying attachment will be displayed.
     444                ob_start();
     445                $widget->render_media( array(
     446                        'attachment_id' => $attachment_id,
     447                        'caption' => null,
     448                ) );
     449                $output = ob_get_clean();
    440450                $this->assertContains( 'class="wp-caption alignnone"', $output );
    441451                $this->assertContains( '<p class="wp-caption-text">Default caption</p>', $output );
    442452
     453                // If caption is provided, then it will be displayed.
    443454                ob_start();
    444455                $widget->render_media( array(
    445456                        'attachment_id' => $attachment_id,
    446457                        'caption' => 'Custom caption',
    447458                ) );
    448459                $output = ob_get_clean();
    449 
    450460                $this->assertContains( 'class="wp-caption alignnone"', $output );
    451461                $this->assertContains( '<p class="wp-caption-text">Custom caption</p>', $output );
    452462        }