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


git-svn-id: https://develop.svn.wordpress.org/trunk@34663 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
John Blackbourn 2015-09-28 15:33:12 +00:00
parent 7fce79698a
commit 8823432749
2 changed files with 29 additions and 6 deletions

View File

@ -182,8 +182,11 @@ function get_the_post_thumbnail( $post = null, $size = 'post-thumbnail', $attr =
* @return string|false Post thumbnail URL or false if no URL is available.
*/
function get_the_post_thumbnail_url( $post = null, $size = 'post-thumbnail' ) {
$image = wp_get_attachment_image_url( get_post_thumbnail_id( $post ), $size );
return isset( $image ) ? $image : false;
$post_thumbnail_id = get_post_thumbnail_id( $post );
if ( ! $post_thumbnail_id ) {
return false;
}
return wp_get_attachment_image_url( $post_thumbnail_id, $size );
}
/**
@ -195,5 +198,8 @@ function get_the_post_thumbnail_url( $post = null, $size = 'post-thumbnail' ) {
* array of height and width dimensions. Default 'post-thumbnail'.
*/
function the_post_thumbnail_url( $size = 'post-thumbnail' ) {
echo get_the_post_thumbnail_url( null, $size );
$url = get_the_post_thumbnail_url( null, $size );
if ( $url ) {
echo esc_url( $url );
}
}

View File

@ -119,12 +119,13 @@ class Tests_Post_Thumbnail_Template extends WP_UnitTestCase {
* @ticket 33070
*/
function test_get_the_post_thumbnail_url() {
$this->assertEquals( '', get_the_post_thumbnail_url() );
$this->assertEquals( '', get_the_post_thumbnail_url( $this->post ) );
$this->assertFalse( has_post_thumbnail( $this->post ) );
$this->assertFalse( get_the_post_thumbnail_url() );
$this->assertFalse( get_the_post_thumbnail_url( $this->post ) );
set_post_thumbnail( $this->post, $this->attachment_id );
$this->assertEquals( '', get_the_post_thumbnail_url() );
$this->assertFalse( get_the_post_thumbnail_url() );
$this->assertEquals( wp_get_attachment_url( $this->attachment_id ), get_the_post_thumbnail_url( $this->post ) );
$GLOBALS['post'] = $this->post;
@ -132,6 +133,22 @@ class Tests_Post_Thumbnail_Template extends WP_UnitTestCase {
$this->assertEquals( wp_get_attachment_url( $this->attachment_id ), get_the_post_thumbnail_url() );
}
/**
* @ticket 33070
*/
function test_get_the_post_thumbnail_url_with_invalid_post() {
$post = $this->factory->post->create_and_get();
set_post_thumbnail( $post, $this->attachment_id );
$this->assertNotFalse( get_the_post_thumbnail_url( $post->ID ) );
$deleted = wp_delete_post( $post->ID, true );
$this->assertNotEmpty( $deleted );
$this->assertFalse( get_the_post_thumbnail_url( $post->ID ) );
}
/**
* @ticket 33070
*/