From 8823432749dc29bc8c0f6dc85c83fb11f0836761 Mon Sep 17 00:00:00 2001 From: John Blackbourn Date: Mon, 28 Sep 2015 15:33:12 +0000 Subject: [PATCH] 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 --- src/wp-includes/post-thumbnail-template.php | 12 ++++++++--- tests/phpunit/tests/post/thumbnails.php | 23 ++++++++++++++++++--- 2 files changed, 29 insertions(+), 6 deletions(-) diff --git a/src/wp-includes/post-thumbnail-template.php b/src/wp-includes/post-thumbnail-template.php index a648a6f82c..4ae50f288f 100644 --- a/src/wp-includes/post-thumbnail-template.php +++ b/src/wp-includes/post-thumbnail-template.php @@ -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 ); + } } diff --git a/tests/phpunit/tests/post/thumbnails.php b/tests/phpunit/tests/post/thumbnails.php index 278eb13fd9..803c16c8c6 100644 --- a/tests/phpunit/tests/post/thumbnails.php +++ b/tests/phpunit/tests/post/thumbnails.php @@ -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 */