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
This commit is contained in:
Weston Ruter 2017-10-27 05:04:04 +00:00
parent 49ec33ae71
commit 6eacf2f06b
2 changed files with 16 additions and 4 deletions

View File

@ -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'];
}

View File

@ -425,28 +425,38 @@ class Test_WP_Widget_Media_Image extends WP_UnitTestCase {
$this->assertContains( '<a href="https://example.org"', $output );
$this->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( '<p class="wp-caption-text">', $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( '<p class="wp-caption-text">Default caption</p>', $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( '<p class="wp-caption-text">Custom caption</p>', $output );
}