From 84df5f067c9c7a8b932b98f5b8b3c8ef484fd30a Mon Sep 17 00:00:00 2001 From: Scott Taylor Date: Wed, 7 Oct 2015 01:06:58 +0000 Subject: [PATCH] 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 --- src/wp-includes/rewrite-functions.php | 3 +++ tests/phpunit/tests/rewrite.php | 30 +++++++++++++++++++++++++++ 2 files changed, 33 insertions(+) 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' );