Widgets: Make sure image widgets with custom image size render captions.

Props kasparsd, JavierCasares, audrasjb.
Fixes #50160.

git-svn-id: https://develop.svn.wordpress.org/trunk@48557 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Sergey Biryukov 2020-07-21 23:12:22 +00:00
parent 3da487037d
commit f840dabcc6
2 changed files with 24 additions and 4 deletions

View File

@ -184,9 +184,11 @@ class WP_Widget_Media_Image extends WP_Widget_Media {
);
$attachment = null;
if ( $this->is_attachment_with_mime_type( $instance['attachment_id'], $this->widget_options['mime_type'] ) ) {
$attachment = get_post( $instance['attachment_id'] );
}
if ( $attachment ) {
$caption = '';
if ( ! isset( $instance['caption'] ) ) {
@ -208,16 +210,19 @@ class WP_Widget_Media_Image extends WP_Widget_Media {
}
$size = $instance['size'];
if ( 'custom' === $size || ! in_array( $size, array_merge( get_intermediate_image_sizes(), array( 'full' ) ), true ) ) {
$size = array( $instance['width'], $instance['height'] );
$size = array( $instance['width'], $instance['height'] );
$width = $instance['width'];
} else {
$caption_size = _wp_get_image_size_from_meta( $instance['size'], wp_get_attachment_metadata( $attachment->ID ) );
$width = empty( $caption_size[0] ) ? 0 : $caption_size[0];
}
$image_attributes['class'] .= sprintf( ' attachment-%1$s size-%1$s', is_array( $size ) ? join( 'x', $size ) : $size );
$image = wp_get_attachment_image( $attachment->ID, $size, false, $image_attributes );
$caption_size = _wp_get_image_size_from_meta( $instance['size'], wp_get_attachment_metadata( $attachment->ID ) );
$width = empty( $caption_size[0] ) ? 0 : $caption_size[0];
} else {
if ( empty( $instance['url'] ) ) {
return;

View File

@ -585,6 +585,21 @@ class Test_WP_Widget_Media_Image extends WP_UnitTestCase {
$output = ob_get_clean();
$this->assertContains( 'class="wp-caption alignnone"', $output );
$this->assertContains( '<p class="wp-caption-text">Custom caption</p>', $output );
// Attachments with custom sizes can render captions.
ob_start();
$widget->render_media(
array(
'attachment_id' => $attachment_id,
'size' => 'custom',
'width' => '300',
'height' => '200',
'caption' => 'Caption for an image with custom size',
)
);
$output = ob_get_clean();
$this->assertContains( 'style="width: 310px"', $output );
$this->assertContains( '<p class="wp-caption-text">Caption for an image with custom size</p>', $output );
}
/**