From 6eacf2f06bcb6417a83b754b3453ec07befe8ff8 Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Fri, 27 Oct 2017 05:04:04 +0000 Subject: [PATCH] Widgets: Prevent showing underlying attachment excerpt as caption when empty caption value is supplied in Image widget. Allow underlying attachment to display if `caption` is `null`. Props miyauchi, westonruter. See #39993. Fixes #42350. git-svn-id: https://develop.svn.wordpress.org/trunk@42030 602fd350-edb4-49c9-b593-d223f7449a82 --- .../widgets/class-wp-widget-media-image.php | 6 ++++-- tests/phpunit/tests/widgets/media-image-widget.php | 14 ++++++++++++-- 2 files changed, 16 insertions(+), 4 deletions(-) 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 ); }