Improvements to `wp_make_link_relative()`.

* Support relative URL input.
* When the URL being made relative has another URL as a parameter, don't make the second URL relative.

Props voldemortensen.
Fixes #30373.

git-svn-id: https://develop.svn.wordpress.org/trunk@30383 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Boone Gorges 2014-11-19 01:52:57 +00:00
parent c4d33d3d75
commit d1e841c03b
2 changed files with 32 additions and 2 deletions

View File

@ -3239,7 +3239,7 @@ function tag_escape($tag_name) {
* @return string Absolute path.
*/
function wp_make_link_relative( $link ) {
return preg_replace( '|https?://[^/]+(/.*)|i', '$1', $link );
return preg_replace( '|^(https?:)?//[^/]+(/.*)|i', '$2', $link );
}
/**

View File

@ -256,4 +256,34 @@ class Tests_Link extends WP_UnitTestCase {
$this->assertEquals( $four, get_adjacent_post( true, array( $exclude ), true ) );
$this->assertEmpty( get_adjacent_post( false, array(), false ) );
}
}
public function test_wp_make_link_relative_with_http_scheme() {
$link = 'http://example.com/this-is-a-test-http-url/';
$relative_link = wp_make_link_relative( $link );
$this->assertEquals( '/this-is-a-test-http-url/', $relative_link );
}
public function test_wp_make_link_relative_with_https_scheme() {
$link = 'https://example.com/this-is-a-test-https-url/';
$relative_link = wp_make_link_relative( $link );
$this->assertEquals( '/this-is-a-test-https-url/', $relative_link );
}
/**
* @ticket 30373
*/
public function test_wp_make_link_relative_with_no_scheme() {
$link = '//example.com/this-is-a-test-schemeless-url/';
$relative_link = wp_make_link_relative( $link );
$this->assertEquals( '/this-is-a-test-schemeless-url/', $relative_link );
}
/**
* @ticket 30373
*/
public function test_wp_make_link_relative_should_retain_URL_param_that_is_also_a_URL() {
$link = 'https://example.com/this-is-a-test/?redirect=https://example.org/a-different-test-post/';
$relative_link = wp_make_link_relative( $link );
$this->assertEquals( '/this-is-a-test/?redirect=https://example.org/a-different-test-post/', $relative_link );
}
}