WordPress.org

Make WordPress Core

Changeset 42030


Ignore:
Timestamp:
10/27/17 05:04:04 (4 weeks ago)
Author:
westonruter
Message:

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.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/widgets/class-wp-widget-media-image.php

    r41696 r42030  
    178178        } 
    179179        if ( $attachment ) { 
    180             $caption = $attachment->post_excerpt; 
    181             if ( $instance['caption'] ) { 
     180            $caption = ''; 
     181            if ( ! isset( $instance['caption'] ) ) { 
     182                $caption = $attachment->post_excerpt; 
     183            } elseif ( trim( $instance['caption'] ) ) { 
    182184                $caption = $instance['caption']; 
    183185            } 
  • trunk/tests/phpunit/tests/widgets/media-image-widget.php

    r41549 r42030  
    426426        $this->assertContains( 'target="_blank"', $output ); 
    427427 
    428         // Caption settings. 
     428        // Populate caption in attachment. 
    429429        wp_update_post( array( 
    430430            'ID' => $attachment_id, 
     
    432432        ) ); 
    433433 
    434         ob_start(); 
    435         $widget->render_media( array( 
    436             'attachment_id' => $attachment_id, 
    437         ) ); 
    438         $output = ob_get_clean(); 
    439  
     434        // If no caption is supplied, then the default is '', and so the caption will not be displayed. 
     435        ob_start(); 
     436        $widget->render_media( array( 
     437            'attachment_id' => $attachment_id, 
     438        ) ); 
     439        $output = ob_get_clean(); 
     440        $this->assertNotContains( 'wp-caption', $output ); 
     441        $this->assertNotContains( '<p class="wp-caption-text">', $output ); 
     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(); 
    440450        $this->assertContains( 'class="wp-caption alignnone"', $output ); 
    441451        $this->assertContains( '<p class="wp-caption-text">Default caption</p>', $output ); 
    442452 
     453        // If caption is provided, then it will be displayed. 
    443454        ob_start(); 
    444455        $widget->render_media( array( 
     
    447458        ) ); 
    448459        $output = ob_get_clean(); 
    449  
    450460        $this->assertContains( 'class="wp-caption alignnone"', $output ); 
    451461        $this->assertContains( '<p class="wp-caption-text">Custom caption</p>', $output ); 
Note: See TracChangeset for help on using the changeset viewer.