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
This commit is contained in:
Peter Wilson
2023-02-21 01:47:57 +00:00
parent 2a753c1057
commit 072274690b
2 changed files with 15 additions and 0 deletions

View File

@@ -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, '/' );
}

View File

@@ -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' ),
);
}