Skip to content
Draft
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
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,40 @@ public function next_url_in_current_token() {
}
}

/**
* Rewrites all supported URLs in the current text node through the native
* batch URL base rewriter.
*
* This only handles ASCII text nodes. Non-ASCII text still falls through to
* the structured PHP path to preserve the broader URL parsing behavior.
*/
public function rewrite_current_text_node_url_bases( $compact_mapping ) {
if (
'#text' !== $this->get_token_type() ||
! function_exists( 'wp_native_apis_rewrite_text_url_bases' ) ||
( defined( 'WP_NATIVE_APIS_DISABLE_DEFAULTS' ) && WP_NATIVE_APIS_DISABLE_DEFAULTS )
) {
return false;
}

$text = $this->get_modifiable_text();
if ( preg_match( '/[^\x00-\x7F]/', $text ) ) {
return false;
}

$updated_text = wp_native_apis_rewrite_text_url_bases(
$text,
$this->base_url_string,
$compact_mapping
);

if ( $updated_text === $text ) {
return false;
}

return $this->set_modifiable_text( $updated_text );
}

private function next_url_in_text_node() {
if ( '#text' !== $this->get_token_type() ) {
return false;
Expand Down
83 changes: 47 additions & 36 deletions components/DataLiberation/URL/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,50 +66,61 @@ function wp_rewrite_urls( $options ) {
}
$mapping_cache_key = sha1( implode( "\0", $mapping_key_parts ) );

$compact_mapping = '';
foreach ( $options['url-mapping'] as $from_url_string => $to_url_string ) {
$compact_mapping .= $from_url_string . "\x1f" . $to_url_string . "\x1e";
}

$p = new BlockMarkupUrlProcessor( $options['block_markup'], $options['base_url'] );
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;

$cached = $rewrite_cache->get( $cache_key );
if ( null !== $cached ) {
if ( false !== $cached ) {
$p->set_url( $cached['raw_url'], $cached['parsed_url'] );
}
while ( $p->next_token() ) {
if ( $p->rewrite_current_text_node_url_bases( $compact_mapping ) ) {
continue;
}

$parsed_url = $p->get_parsed_url();
$converted = false;
foreach ( $url_mapping as $mapping ) {
if ( is_child_url_of( $parsed_url, $mapping['from_url'] ) ) {
$converted = WPURL::replace_base_url(
$parsed_url,
array(
'old_base_url' => $base_url_object,
'new_base_url' => $mapping['to_url'],
'raw_url' => $raw_url,
'is_relative' => (
'#text' !== $token_type &&
! WPURL::can_parse( $raw_url )
),
)
while ( $p->next_url_in_current_token() ) {
$token_type = $p->get_token_type();
$raw_url = $p->get_raw_url();
$cache_key = $mapping_cache_key . "\0" . $token_type . "\0" . $raw_url;

$cached = $rewrite_cache->get( $cache_key );
if ( null !== $cached ) {
if ( false !== $cached ) {
$p->set_url( $cached['raw_url'], $cached['parsed_url'] );
}
continue;
}

$parsed_url = $p->get_parsed_url();
$converted = false;
foreach ( $url_mapping as $mapping ) {
if ( is_child_url_of( $parsed_url, $mapping['from_url'] ) ) {
$converted = WPURL::replace_base_url(
$parsed_url,
array(
'old_base_url' => $base_url_object,
'new_base_url' => $mapping['to_url'],
'raw_url' => $raw_url,
'is_relative' => (
'#text' !== $token_type &&
! WPURL::can_parse( $raw_url )
),
)
);
break;
}
}

$cache_value = false;
if ( false !== $converted ) {
$cache_value = array(
'raw_url' => (string) $converted,
'parsed_url' => $converted->new_url,
);
break;
$p->set_url( $cache_value['raw_url'], $cache_value['parsed_url'] );
}
}

$cache_value = false;
if ( false !== $converted ) {
$cache_value = array(
'raw_url' => (string) $converted,
'parsed_url' => $converted->new_url,
);
$p->set_url( $cache_value['raw_url'], $cache_value['parsed_url'] );
$rewrite_cache->set( $cache_key, $cache_value );
}

$rewrite_cache->set( $cache_key, $cache_value );
}

return $p->get_updated_html();
Expand Down
Loading