From a036114a4d52fec920ea2dc1c268c9d99f34355d Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Sun, 18 Jan 2015 20:32:50 +0000 Subject: [PATCH] attachment_url_to_postid() should always return an integer. props nathan_dawson, ashfame. fixes #31044. git-svn-id: https://develop.svn.wordpress.org/trunk@31239 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/media.php | 7 +++---- tests/phpunit/tests/media.php | 9 +++++++++ 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/src/wp-includes/media.php b/src/wp-includes/media.php index 6a1bddbefc..506c86c5ee 100644 --- a/src/wp-includes/media.php +++ b/src/wp-includes/media.php @@ -3295,7 +3295,7 @@ function wp_maybe_generate_attachment_metadata( $attachment ) { * @global wpdb $wpdb WordPress database abstraction object. * * @param string $url The URL to resolve. - * @return int The found post ID. + * @return int The found post ID, or 0 on failure. */ function attachment_url_to_postid( $url ) { global $wpdb; @@ -3312,9 +3312,8 @@ function attachment_url_to_postid( $url ) { $path ); $post_id = $wpdb->get_var( $sql ); - if ( ! empty( $post_id ) ) { - return (int) $post_id; - } + + return (int) $post_id; } /** diff --git a/tests/phpunit/tests/media.php b/tests/phpunit/tests/media.php index f0960d7536..9f0a131503 100644 --- a/tests/phpunit/tests/media.php +++ b/tests/phpunit/tests/media.php @@ -501,6 +501,15 @@ VIDEO; return $dir; } + /** + * @ticket 31044 + */ + function test_attachment_url_to_postid_with_empty_url() { + $post_id = attachment_url_to_postid( '' ); + $this->assertInternalType( 'int', $post_id ); + $this->assertEquals( 0, $post_id ); + } + function test_wp_check_filetype() { $url = 'http://example.com/testFile.mp4?autoplay=true&otherstuff=false'; $filetype = wp_check_filetype( $url );