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 {
|
| 177 | 177 | $attachment = get_post( $instance['attachment_id'] ); |
| 178 | 178 | } |
| 179 | 179 | if ( $attachment ) { |
| 180 | | $caption = $attachment->post_excerpt; |
| 181 | | if ( $instance['caption'] ) { |
| | 180 | $caption = ''; |
| | 181 | if ( trim( $instance['caption'] ) ) { |
| 182 | 182 | $caption = $instance['caption']; |
| | 183 | } elseif ( null === $instance['caption'] ) { |
| | 184 | $caption = $attachment->post_excerpt; |
| 183 | 185 | } |
| 184 | 186 | |
| 185 | 187 | $image_attributes = array( |
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 {
|
| 425 | 425 | $this->assertContains( '<a href="https://example.org"', $output ); |
| 426 | 426 | $this->assertContains( 'target="_blank"', $output ); |
| 427 | 427 | |
| 428 | | // Caption settings. |
| | 428 | // Populate caption in attachment. |
| 429 | 429 | wp_update_post( array( |
| 430 | 430 | 'ID' => $attachment_id, |
| 431 | 431 | 'post_excerpt' => 'Default caption', |
| 432 | 432 | ) ); |
| 433 | 433 | |
| | 434 | // If no caption is supplied, then the default is '', and so the caption will not be displayed. |
| 434 | 435 | ob_start(); |
| 435 | 436 | $widget->render_media( array( |
| 436 | 437 | 'attachment_id' => $attachment_id, |
| 437 | 438 | ) ); |
| 438 | 439 | $output = ob_get_clean(); |
| | 440 | $this->assertNotContains( 'wp-caption', $output ); |
| | 441 | $this->assertNotContains( '<p class="wp-caption-text">', $output ); |
| 439 | 442 | |
| | 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(); |
| 440 | 450 | $this->assertContains( 'class="wp-caption alignnone"', $output ); |
| 441 | 451 | $this->assertContains( '<p class="wp-caption-text">Default caption</p>', $output ); |
| 442 | 452 | |
| | 453 | // If caption is provided, then it will be displayed. |
| 443 | 454 | ob_start(); |
| 444 | 455 | $widget->render_media( array( |
| 445 | 456 | 'attachment_id' => $attachment_id, |
| 446 | 457 | 'caption' => 'Custom caption', |
| 447 | 458 | ) ); |
| 448 | 459 | $output = ob_get_clean(); |
| 449 | | |
| 450 | 460 | $this->assertContains( 'class="wp-caption alignnone"', $output ); |
| 451 | 461 | $this->assertContains( '<p class="wp-caption-text">Custom caption</p>', $output ); |
| 452 | 462 | } |