diff --git a/src/wp-includes/widgets/class-wp-widget-media-image.php b/src/wp-includes/widgets/class-wp-widget-media-image.php index 780ceacad5..4ebc42d66e 100644 --- a/src/wp-includes/widgets/class-wp-widget-media-image.php +++ b/src/wp-includes/widgets/class-wp-widget-media-image.php @@ -177,8 +177,10 @@ class WP_Widget_Media_Image extends WP_Widget_Media { $attachment = get_post( $instance['attachment_id'] ); } if ( $attachment ) { - $caption = $attachment->post_excerpt; - if ( $instance['caption'] ) { + $caption = ''; + if ( ! isset( $instance['caption'] ) ) { + $caption = $attachment->post_excerpt; + } elseif ( trim( $instance['caption'] ) ) { $caption = $instance['caption']; } diff --git a/tests/phpunit/tests/widgets/media-image-widget.php b/tests/phpunit/tests/widgets/media-image-widget.php index 408123bef7..e68399229d 100644 --- a/tests/phpunit/tests/widgets/media-image-widget.php +++ b/tests/phpunit/tests/widgets/media-image-widget.php @@ -425,28 +425,38 @@ class Test_WP_Widget_Media_Image extends WP_UnitTestCase { $this->assertContains( 'assertContains( 'target="_blank"', $output ); - // Caption settings. + // Populate caption in attachment. wp_update_post( array( 'ID' => $attachment_id, 'post_excerpt' => 'Default caption', ) ); + // If no caption is supplied, then the default is '', and so the caption will not be displayed. ob_start(); $widget->render_media( array( 'attachment_id' => $attachment_id, ) ); $output = ob_get_clean(); + $this->assertNotContains( 'wp-caption', $output ); + $this->assertNotContains( '

', $output ); + // If the caption is explicitly null, then the caption of the underlying attachment will be displayed. + ob_start(); + $widget->render_media( array( + 'attachment_id' => $attachment_id, + 'caption' => null, + ) ); + $output = ob_get_clean(); $this->assertContains( 'class="wp-caption alignnone"', $output ); $this->assertContains( '

Default caption

', $output ); + // If caption is provided, then it will be displayed. ob_start(); $widget->render_media( array( 'attachment_id' => $attachment_id, 'caption' => 'Custom caption', ) ); $output = ob_get_clean(); - $this->assertContains( 'class="wp-caption alignnone"', $output ); $this->assertContains( '

Custom caption

', $output ); }