WordPress.org

Make WordPress Core

Changeset 34663


Ignore:
Timestamp:
09/28/2015 03:33:12 PM (4 years ago)
Author:
johnbillion
Message:

Update get_the_post_thumbnail_url() so it returns false on failure, to bring it inline with wp_get_attachment_image_url(). Correct and introduce new tests.

Fixes #33070

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/post-thumbnail-template.php

    r34480 r34663  
    183183 */
    184184function get_the_post_thumbnail_url( $post = null, $size = 'post-thumbnail' ) {
    185     $image = wp_get_attachment_image_url( get_post_thumbnail_id( $post ), $size );
    186     return isset( $image ) ? $image : false;
     185    $post_thumbnail_id = get_post_thumbnail_id( $post );
     186    if ( ! $post_thumbnail_id ) {
     187        return false;
     188    }
     189    return wp_get_attachment_image_url( $post_thumbnail_id, $size );
    187190}
    188191
     
    196199 */
    197200function the_post_thumbnail_url( $size = 'post-thumbnail' ) {
    198     echo get_the_post_thumbnail_url( null, $size );
    199 }
     201    $url = get_the_post_thumbnail_url( null, $size );
     202    if ( $url ) {
     203        echo esc_url( $url );
     204    }
     205}
  • trunk/tests/phpunit/tests/post/thumbnails.php

    r34633 r34663  
    120120     */
    121121    function test_get_the_post_thumbnail_url() {
    122         $this->assertEquals( '', get_the_post_thumbnail_url() );
    123         $this->assertEquals( '', get_the_post_thumbnail_url( $this->post ) );
     122        $this->assertFalse( has_post_thumbnail( $this->post ) );
     123        $this->assertFalse( get_the_post_thumbnail_url() );
     124        $this->assertFalse( get_the_post_thumbnail_url( $this->post ) );
    124125
    125126        set_post_thumbnail( $this->post, $this->attachment_id );
    126127
    127         $this->assertEquals( '', get_the_post_thumbnail_url() );
     128        $this->assertFalse( get_the_post_thumbnail_url() );
    128129        $this->assertEquals( wp_get_attachment_url( $this->attachment_id ), get_the_post_thumbnail_url( $this->post ) );
    129130
     
    131132
    132133        $this->assertEquals( wp_get_attachment_url( $this->attachment_id ), get_the_post_thumbnail_url() );
     134    }
     135
     136    /**
     137     * @ticket 33070
     138     */
     139    function test_get_the_post_thumbnail_url_with_invalid_post() {
     140        $post = $this->factory->post->create_and_get();
     141
     142        set_post_thumbnail( $post, $this->attachment_id );
     143
     144        $this->assertNotFalse( get_the_post_thumbnail_url( $post->ID ) );
     145
     146        $deleted = wp_delete_post( $post->ID, true );
     147        $this->assertNotEmpty( $deleted );
     148
     149        $this->assertFalse( get_the_post_thumbnail_url( $post->ID ) );
    133150    }
    134151
Note: See TracChangeset for help on using the changeset viewer.