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
This commit is contained in:
parent
6507668f4e
commit
84df5f067c
@ -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);
|
||||
|
@ -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' );
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user