diff --git a/src/wp-includes/rewrite-functions.php b/src/wp-includes/rewrite-functions.php index 9cfb89cd2a..8dfc1189c6 100644 --- a/src/wp-includes/rewrite-functions.php +++ b/src/wp-includes/rewrite-functions.php @@ -339,6 +339,9 @@ function url_to_postid( $url ) { $url_split = explode('?', $url); $url = $url_split[0]; + // Set the correct URL scheme. + $url = set_url_scheme( $url ); + // Add 'www.' if it is absent and should be there if ( false !== strpos(home_url(), '://www.') && false === strpos($url, '://www.') ) $url = str_replace('://', '://www.', $url); diff --git a/tests/phpunit/tests/rewrite.php b/tests/phpunit/tests/rewrite.php index 80f2e9a986..601b4cd528 100644 --- a/tests/phpunit/tests/rewrite.php +++ b/tests/phpunit/tests/rewrite.php @@ -92,6 +92,36 @@ class Tests_Rewrite extends WP_UnitTestCase { $this->assertEquals( $id, url_to_postid( get_permalink( $id ) ) ); } + function test_url_to_postid_set_url_scheme_https_to_http() { + $post_id = $this->factory->post->create(); + $permalink = get_permalink( $post_id ); + $this->assertEquals( $post_id, url_to_postid( set_url_scheme( $permalink, 'https' ) ) ); + + $post_id = $this->factory->post->create( array( 'post_type' => 'page' ) ); + $permalink = get_permalink( $post_id ); + $this->assertEquals( $post_id, url_to_postid( set_url_scheme( $permalink, 'https' ) ) ); + } + + function test_url_to_postid_set_url_scheme_http_to_https() { + // Save server data for cleanup + $is_ssl = is_ssl(); + $http_host = $_SERVER['HTTP_HOST']; + + $_SERVER['HTTPS'] = 'on'; + + $post_id = $this->factory->post->create(); + $permalink = get_permalink( $post_id ); + $this->assertEquals( $post_id, url_to_postid( set_url_scheme( $permalink, 'http' ) ) ); + + $post_id = $this->factory->post->create( array( 'post_type' => 'page' ) ); + $permalink = get_permalink( $post_id ); + $this->assertEquals( $post_id, url_to_postid( set_url_scheme( $permalink, 'http' ) ) ); + + // Cleanup. + $_SERVER['HTTPS'] = $is_ssl ? 'on' : 'off'; + $_SERVER['HTTP_HOST'] = $http_host; + } + function test_url_to_postid_custom_post_type() { delete_option( 'rewrite_rules' );