From 072274690b52cfa0c5e5da763e70a7af24f00da7 Mon Sep 17 00:00:00 2001 From: Peter Wilson Date: Tue, 21 Feb 2023 01:47:57 +0000 Subject: [PATCH] HTTP API: Add fragment support to `WP_Http::make_absolute_url()`. Modifies `WP_Http::make_absolute_url()` to prevent it from dropping URL fragments, this in turn fixes the same issue for `links_add_base_url()`. Props costdev, sergeybiryukov, dshanske, schlessera, jrf, desrosj, dd32. Fixes #56231. git-svn-id: https://develop.svn.wordpress.org/trunk@55370 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/class-wp-http.php | 5 +++++ tests/phpunit/tests/http/http.php | 10 ++++++++++ 2 files changed, 15 insertions(+) diff --git a/src/wp-includes/class-wp-http.php b/src/wp-includes/class-wp-http.php index 6f4fa47200..12a56b0a0a 100644 --- a/src/wp-includes/class-wp-http.php +++ b/src/wp-includes/class-wp-http.php @@ -1013,6 +1013,11 @@ class WP_Http { $path .= '?' . $relative_url_parts['query']; } + // Add the fragment. + if ( ! empty( $relative_url_parts['fragment'] ) ) { + $path .= '#' . $relative_url_parts['fragment']; + } + return $absolute_path . '/' . ltrim( $path, '/' ); } diff --git a/tests/phpunit/tests/http/http.php b/tests/phpunit/tests/http/http.php index ea27d7b0d7..dfc9fd6518 100644 --- a/tests/phpunit/tests/http/http.php +++ b/tests/phpunit/tests/http/http.php @@ -9,6 +9,9 @@ class Tests_HTTP_HTTP extends WP_UnitTestCase { const FULL_TEST_URL = 'http://username:password@host.name:9090/path?arg1=value1&arg2=value2#anchor'; /** + * @ticket 20434 + * @ticket 56231 + * * @dataProvider make_absolute_url_testcases * * @covers WP_Http::make_absolute_url @@ -66,6 +69,13 @@ class Tests_HTTP_HTTP extends WP_UnitTestCase { // Schemeless URL's (not valid in HTTP Headers, but may be used elsewhere). array( '//example.com/sub/', 'https://example.net', 'https://example.com/sub/' ), + + // URLs with fragments. + array( '/path#frag', 'http://example.org/', 'http://example.org/path#frag' ), + array( '/path/#frag', 'http://example.org/', 'http://example.org/path/#frag' ), + array( '/path#frag&ment=1', 'http://example.org/', 'http://example.org/path#frag&ment=1' ), + array( '/path?query=string#frag', 'http://example.org/', 'http://example.org/path?query=string#frag' ), + array( '/path?query=string%23frag', 'http://example.org/', 'http://example.org/path?query=string%23frag' ), ); }