Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions components/DataLiberation/Tests/RewriteUrlsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,30 @@ public static function provider_test_wp_rewrite_urls() {
'https://🚀-science.com/science',
'https://science.wordpress.org',
),
'Fragment-only reference in an HTML attribute is left alone' => array(
'<a href="#section">Jump</a>',
'<a href="#section">Jump</a>',
'https://legacy-blog.com',
'https://modern-webstore.org',
),
'Bare hash in an HTML attribute is left alone' => array(
'<a href="#">Placeholder</a>',
'<a href="#">Placeholder</a>',
'https://legacy-blog.com',
'https://modern-webstore.org',
),
'Fragment-only reference in a block attribute is left alone' => array(
'<!-- wp:button {"url":"#login"} -->',
'<!-- wp:button {"url":"#login"} -->',
'https://legacy-blog.com',
'https://modern-webstore.org',
),
'Absolute URL carrying a fragment is still rewritten and keeps its fragment' => array(
'<a href="https://legacy-blog.com/page#section">Deep link</a>',
'<a href="https://modern-webstore.org/page#section">Deep link</a>',
'https://legacy-blog.com',
'https://modern-webstore.org',
),
);
}

Expand Down
15 changes: 14 additions & 1 deletion components/DataLiberation/URL/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,20 @@ function wp_rewrite_urls( $options ) {
while ( $p->next_url() ) {
$token_type = $p->get_token_type();
$raw_url = $p->get_raw_url();
$cache_key = $mapping_cache_key . "\0" . $token_type . "\0" . $raw_url;

/*
* Leave fragment-only references alone. A URL like `#section` points
* within the document that contains it, so there is no origin to
* migrate. Resolving it against the base URL makes it look like a
* child of the site being imported from, and rewriting it then turns
* an in-page anchor into a link somewhere else entirely:
* `#section` becomes `/#section`, and a bare `#` becomes `/`.
*/
if ( is_string( $raw_url ) && 0 === strpos( $raw_url, '#' ) ) {
continue;
}

$cache_key = $mapping_cache_key . "\0" . $token_type . "\0" . $raw_url;

$cached = $rewrite_cache->get( $cache_key );
if ( null !== $cached ) {
Expand Down
Loading