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 );