From 88c274fad87b488cc064142b4110dd8ea65e2e0a Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Tue, 3 Nov 2015 21:35:17 +0000 Subject: [PATCH] Formatting: `wp_make_link_relative()` should return an empty string if no path is present in the link. Props bcworkz, MikeHansenMe, chriscct7, SergeyBiryukov. Fixes #26819. git-svn-id: https://develop.svn.wordpress.org/trunk@35497 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/formatting.php | 2 +- .../tests/formatting/WPMakeLinkRelative.php | 47 +++++++++++++++++++ tests/phpunit/tests/link.php | 30 ------------ 3 files changed, 48 insertions(+), 31 deletions(-) create mode 100644 tests/phpunit/tests/formatting/WPMakeLinkRelative.php diff --git a/src/wp-includes/formatting.php b/src/wp-includes/formatting.php index 7e76953267..71c5b04f0c 100644 --- a/src/wp-includes/formatting.php +++ b/src/wp-includes/formatting.php @@ -3621,7 +3621,7 @@ function tag_escape( $tag_name ) { * @return string Absolute path. */ function wp_make_link_relative( $link ) { - return preg_replace( '|^(https?:)?//[^/]+(/.*)|i', '$2', $link ); + return preg_replace( '|^(https?:)?//[^/]+(/?.*)|i', '$2', $link ); } /** diff --git a/tests/phpunit/tests/formatting/WPMakeLinkRelative.php b/tests/phpunit/tests/formatting/WPMakeLinkRelative.php new file mode 100644 index 0000000000..c87d24602e --- /dev/null +++ b/tests/phpunit/tests/formatting/WPMakeLinkRelative.php @@ -0,0 +1,47 @@ +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 ); + } + + /** + * @ticket 26819 + */ + function test_wp_make_link_relative_with_no_path() { + $link = 'http://example.com'; + $relative_link = wp_make_link_relative( $link ); + $this->assertEquals( '', $relative_link ); + } + +} \ No newline at end of file diff --git a/tests/phpunit/tests/link.php b/tests/phpunit/tests/link.php index 54c1bd0316..ebec6c1d2d 100644 --- a/tests/phpunit/tests/link.php +++ b/tests/phpunit/tests/link.php @@ -304,36 +304,6 @@ class Tests_Link extends WP_UnitTestCase { $this->assertEquals( $p3, $found->ID ); } - 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 ); - } - /** * @ticket 30910 */