From a70e0183e53c8585816f4f7fcd30f41c07d6b21d Mon Sep 17 00:00:00 2001 From: Scott Taylor Date: Sat, 22 Aug 2015 16:38:09 +0000 Subject: [PATCH] Ensure that `attachment_url_to_postid()` matches cross-scheme when front-end and back-end schemes are different. Adds unit test. Props welcher. Fixes #33109. git-svn-id: https://develop.svn.wordpress.org/trunk@33705 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/media.php | 12 +++++++++--- tests/phpunit/tests/media.php | 22 ++++++++++++++++++++++ 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/src/wp-includes/media.php b/src/wp-includes/media.php index 9ec6e47c43..2c426bf729 100644 --- a/src/wp-includes/media.php +++ b/src/wp-includes/media.php @@ -3431,6 +3431,14 @@ function attachment_url_to_postid( $url ) { $dir = wp_upload_dir(); $path = $url; + $site_url = parse_url( $dir['url'] ); + $image_path = parse_url( $path ); + + //force the protocols to match if needed + if ( isset( $image_path['scheme'] ) && ( $image_path['scheme'] !== $site_url['scheme'] ) ) { + $path = str_replace( $image_path['scheme'], $site_url['scheme'], $path ); + } + if ( 0 === strpos( $path, $dir['baseurl'] . '/' ) ) { $path = substr( $path, strlen( $dir['baseurl'] . '/' ) ); } @@ -3449,9 +3457,7 @@ function attachment_url_to_postid( $url ) { * @param int|null $post_id The post_id (if any) found by the function. * @param string $url The URL being looked up. */ - $post_id = apply_filters( 'attachment_url_to_postid', $post_id, $url ); - - return (int) $post_id; + return (int) apply_filters( 'attachment_url_to_postid', $post_id, $url ); } /** diff --git a/tests/phpunit/tests/media.php b/tests/phpunit/tests/media.php index 68ac7eebc8..34f12813e5 100644 --- a/tests/phpunit/tests/media.php +++ b/tests/phpunit/tests/media.php @@ -533,6 +533,28 @@ VIDEO; $image_url = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . $image_path; $this->assertEquals( $attachment_id, attachment_url_to_postid( $image_url ) ); + } + + function test_attachment_url_to_postid_schemes() { + $image_path = '2014/11/' . $this->img_name; + $attachment_id = $this->factory->attachment->create_object( $image_path, 0, array( + 'post_mime_type' => 'image/jpeg', + 'post_type' => 'attachment', + ) ); + + /** + * @ticket 33109 Testing protocols not matching + */ + $image_url = 'https://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . $image_path; + $this->assertEquals( $attachment_id, attachment_url_to_postid( $image_url ) ); + } + + function test_attachment_url_to_postid_filtered() { + $image_path = '2014/11/' . $this->img_name; + $attachment_id = $this->factory->attachment->create_object( $image_path, 0, array( + 'post_mime_type' => 'image/jpeg', + 'post_type' => 'attachment', + ) ); add_filter( 'upload_dir', array( $this, '_upload_dir' ) ); $image_url = 'http://192.168.1.20.com/wp-content/uploads/' . $image_path;