Widgets: Omit attributes from an Image widget's link when they are empty.

Props subrataemfluence, Nenad Obradovic, westonruter.
See #39993.
Fixes #41919.


git-svn-id: https://develop.svn.wordpress.org/trunk@41549 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Weston Ruter 2017-09-20 19:44:13 +00:00
parent 6d341ef5e5
commit ebb578b2d9
2 changed files with 19 additions and 15 deletions

View File

@ -240,14 +240,20 @@ class WP_Widget_Media_Image extends WP_Widget_Media {
} }
if ( $url ) { if ( $url ) {
$image = sprintf( $link = sprintf( '<a href="%s"', esc_url( $url ) );
'<a href="%1$s" class="%2$s" rel="%3$s" target="%4$s">%5$s</a>', if ( ! empty( $instance['link_classes'] ) ) {
esc_url( $url ), $link .= sprintf( ' class="%s"', esc_attr( $instance['link_classes'] ) );
esc_attr( $instance['link_classes'] ), }
esc_attr( $instance['link_rel'] ), if ( ! empty( $instance['link_rel'] ) ) {
! empty( $instance['link_target_blank'] ) ? '_blank' : '', $link .= sprintf( ' rel="%s"', esc_attr( $instance['link_rel'] ) );
$image }
); if ( ! empty( $instance['link_target_blank'] ) ) {
$link .= ' target="_blank"';
}
$link .= '>';
$link .= $image;
$link .= '</a>';
$image = $link;
} }
if ( $caption ) { if ( $caption ) {

View File

@ -393,12 +393,10 @@ class Test_WP_Widget_Media_Image extends WP_UnitTestCase {
$link = '<a href="' . wp_get_attachment_url( $attachment_id ) . '"'; $link = '<a href="' . wp_get_attachment_url( $attachment_id ) . '"';
$this->assertContains( $link, $output ); $this->assertContains( $link, $output );
$link .= ' class=""'; $this->assertTrue( (bool) preg_match( '#<a href.*?>#', $output, $matches ) );
$this->assertContains( $link, $output ); $this->assertNotContains( ' class="', $matches[0] );
$link .= ' rel=""'; $this->assertNotContains( ' rel="', $matches[0] );
$this->assertContains( $link, $output ); $this->assertNotContains( ' target="', $matches[0] );
$link .= ' target=""';
$this->assertContains( $link, $output );
ob_start(); ob_start();
$widget->render_media( array( $widget->render_media( array(
@ -413,7 +411,7 @@ class Test_WP_Widget_Media_Image extends WP_UnitTestCase {
$this->assertContains( '<a href="' . get_attachment_link( $attachment_id ) . '"', $output ); $this->assertContains( '<a href="' . get_attachment_link( $attachment_id ) . '"', $output );
$this->assertContains( 'class="custom-link-class"', $output ); $this->assertContains( 'class="custom-link-class"', $output );
$this->assertContains( 'rel="attachment"', $output ); $this->assertContains( 'rel="attachment"', $output );
$this->assertContains( 'target=""', $output ); $this->assertNotContains( 'target=""', $output );
ob_start(); ob_start();
$widget->render_media( array( $widget->render_media( array(