Skip to content

Commit 84df5f0

Browse files
committed
Rewrite/Permalinks/Canonical: in url_to_postid(), call set_url_scheme() on the URL to combat mixed content issues and find posts cross-scheme.
Adds unit tests. Props swissspidy. Fixes #34144. git-svn-id: https://develop.svn.wordpress.org/trunk@34890 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 6507668 commit 84df5f0

2 files changed

Lines changed: 33 additions & 0 deletions

File tree

src/wp-includes/rewrite-functions.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,9 @@ function url_to_postid( $url ) {
339339
$url_split = explode('?', $url);
340340
$url = $url_split[0];
341341

342+
// Set the correct URL scheme.
343+
$url = set_url_scheme( $url );
344+
342345
// Add 'www.' if it is absent and should be there
343346
if ( false !== strpos(home_url(), '://www.') && false === strpos($url, '://www.') )
344347
$url = str_replace('://', '://www.', $url);

tests/phpunit/tests/rewrite.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,36 @@ function test_url_to_postid() {
9292
$this->assertEquals( $id, url_to_postid( get_permalink( $id ) ) );
9393
}
9494

95+
function test_url_to_postid_set_url_scheme_https_to_http() {
96+
$post_id = $this->factory->post->create();
97+
$permalink = get_permalink( $post_id );
98+
$this->assertEquals( $post_id, url_to_postid( set_url_scheme( $permalink, 'https' ) ) );
99+
100+
$post_id = $this->factory->post->create( array( 'post_type' => 'page' ) );
101+
$permalink = get_permalink( $post_id );
102+
$this->assertEquals( $post_id, url_to_postid( set_url_scheme( $permalink, 'https' ) ) );
103+
}
104+
105+
function test_url_to_postid_set_url_scheme_http_to_https() {
106+
// Save server data for cleanup
107+
$is_ssl = is_ssl();
108+
$http_host = $_SERVER['HTTP_HOST'];
109+
110+
$_SERVER['HTTPS'] = 'on';
111+
112+
$post_id = $this->factory->post->create();
113+
$permalink = get_permalink( $post_id );
114+
$this->assertEquals( $post_id, url_to_postid( set_url_scheme( $permalink, 'http' ) ) );
115+
116+
$post_id = $this->factory->post->create( array( 'post_type' => 'page' ) );
117+
$permalink = get_permalink( $post_id );
118+
$this->assertEquals( $post_id, url_to_postid( set_url_scheme( $permalink, 'http' ) ) );
119+
120+
// Cleanup.
121+
$_SERVER['HTTPS'] = $is_ssl ? 'on' : 'off';
122+
$_SERVER['HTTP_HOST'] = $http_host;
123+
}
124+
95125
function test_url_to_postid_custom_post_type() {
96126
delete_option( 'rewrite_rules' );
97127

0 commit comments

Comments
 (0)