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
This commit is contained in:
Scott Taylor 2015-08-22 16:38:09 +00:00
parent e76b48a26c
commit a70e0183e5
2 changed files with 31 additions and 3 deletions

View File

@ -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 );
}
/**

View File

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