Skip to content

Commit 538789b

Browse files
committed
Update WordPress Importer plugin from 0.9.2 to 0.9.3
1 parent c8042ac commit 538789b

6 files changed

Lines changed: 215 additions & 67 deletions

File tree

wp-content/plugins/wordpress-importer/class-wp-import.php

Lines changed: 60 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,11 @@ public function import( $file, $options = array() ) {
9999

100100
$this->options = apply_filters( 'wp_import_options', $options );
101101

102+
add_filter( 'import_post_meta_key', array( $this, 'is_valid_meta_key' ) );
103+
add_filter( 'http_request_timeout', array( &$this, 'bump_request_timeout' ) );
104+
105+
$this->import_start( $file );
106+
102107
/**
103108
* If URL rewriting was requested but the WP version is too old, report
104109
* an error and disable it.
@@ -117,11 +122,10 @@ public function import( $file, $options = array() ) {
117122
echo '<div class="error"><p><strong>' . __( 'URL rewriting requires WordPress 6.7 or newer. The import will continue without rewriting URLs.', 'wordpress-importer' ) . '</strong></p></div>';
118123
$this->options['rewrite_urls'] = false;
119124
}
120-
121-
add_filter( 'import_post_meta_key', array( $this, 'is_valid_meta_key' ) );
122-
add_filter( 'http_request_timeout', array( &$this, 'bump_request_timeout' ) );
123-
124-
$this->import_start( $file );
125+
// URL rewriting is only possible when we have the previous site base URL
126+
if ( $this->options['rewrite_urls'] && ! $this->base_url_parsed ) {
127+
$this->options['rewrite_urls'] = false;
128+
}
125129

126130
$this->get_author_mapping();
127131

@@ -794,7 +798,7 @@ public function process_posts() {
794798
'post_password' => $post['post_password'],
795799
);
796800

797-
if ( $this->options['rewrite_urls'] && $this->base_url_parsed ) {
801+
if ( $this->options['rewrite_urls'] ) {
798802
$url_mapping = array(
799803
$this->base_url_parsed->toString() => $this->site_url_parsed,
800804
);
@@ -1296,7 +1300,56 @@ public function fetch_remote_file( $url, $post ) {
12961300
'error' => false,
12971301
);
12981302

1299-
// keep track of the old and new urls so we can substitute them later
1303+
/**
1304+
* When URL rewriting is enabled, posts such as this one:
1305+
*
1306+
* <img src="https://example.com/subpath/wp-content/uploads/2008/06/canola2.jpg" />
1307+
*
1308+
* Are already stored as:
1309+
*
1310+
* <img src="https://example.org/wp-content/uploads/2008/06/canola2.jpg" />
1311+
*
1312+
* Therefore, we can't just remap the old URL to the new URL here. This substring
1313+
* is no longer present in the post:
1314+
*
1315+
* https://example.com/subpath/wp-content/uploads/2008/06/canola2.jpg
1316+
*
1317+
* We need to replace the base URL in the media file URL the same way as we did
1318+
* in the post content:
1319+
*
1320+
* https://example.org/wp-content/uploads/2008/06/canola2.jpg
1321+
*
1322+
* Only from there we can remap that URL to the new media files URL:
1323+
*
1324+
* https://example.org/wp-content/uploads/canola2.jpg"
1325+
* ^ there may be no 2008/06 on the target site.
1326+
*/
1327+
if ( $this->options['rewrite_urls'] ) {
1328+
$url = WPURL::replace_base_url(
1329+
array(
1330+
'url' => $url,
1331+
'old_base_url' => $this->base_url_parsed,
1332+
'new_base_url' => $this->site_url_parsed,
1333+
)
1334+
);
1335+
$post['guid'] = WPURL::replace_base_url(
1336+
array(
1337+
'url' => $post['guid'],
1338+
'old_base_url' => $this->base_url_parsed,
1339+
'new_base_url' => $this->site_url_parsed,
1340+
)
1341+
);
1342+
if ( isset( $headers['x-final-location'] ) ) {
1343+
$headers['x-final-location'] = WPURL::replace_base_url(
1344+
array(
1345+
'url' => $headers['x-final-location'],
1346+
'old_base_url' => $this->base_url_parsed,
1347+
'new_base_url' => $this->site_url_parsed,
1348+
)
1349+
);
1350+
}
1351+
}
1352+
13001353
$this->url_remap[ $url ] = $upload['url'];
13011354
$this->url_remap[ $post['guid'] ] = $upload['url']; // r13735, really needed?
13021355
// keep track of the destination if the remote url is redirected somewhere else

wp-content/plugins/wordpress-importer/php-toolkit/DataLiberation/BlockMarkup/class-blockmarkupurlprocessor.php

Lines changed: 28 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -309,70 +309,42 @@ public function set_url( $raw_url, $parsed_url ) {
309309
* by this WPURL_In_Text_Processor class so maybe the two do go hand in hand?
310310
*/
311311
public function replace_base_url( URL $to_url, ?URL $base_url = null ) {
312-
$updated_url = clone $this->get_parsed_url();
313-
314-
$updated_url->hostname = $to_url->hostname;
315-
$updated_url->protocol = $to_url->protocol;
316-
$updated_url->port = $to_url->port;
317-
318-
// Update the pathname if needed.
319-
$from_url = $this->get_parsed_url();
320-
$from_pathname = $from_url->pathname;
321-
$to_pathname = $to_url->pathname;
322-
323312
$base_url = $base_url ?? $this->base_url_object;
324-
if ( $base_url->pathname !== $to_pathname ) {
325-
$base_pathname_with_trailing_slash = rtrim( $base_url->pathname, '/' ) . '/';
326-
$decoded_matched_pathname = urldecode_n(
327-
$from_pathname,
328-
strlen( $base_pathname_with_trailing_slash )
329-
);
330-
$to_pathname_with_trailing_slash = rtrim( $to_pathname, '/' ) . '/';
331-
$remaining_pathname =
332-
substr(
333-
$decoded_matched_pathname,
334-
strlen( $base_pathname_with_trailing_slash )
335-
);
336-
337-
$updated_url->pathname = $to_pathname_with_trailing_slash . $remaining_pathname;
338-
}
339-
340-
/*
341-
* Stylistic choice – if the updated URL has no trailing slash,
342-
* do not add it to the new URL. The WHATWG URL parser will
343-
* add one automatically if the path is empty, so we have to
344-
* explicitly remove it.
345-
*/
346-
$new_raw_url = $updated_url->toString();
347-
if (
348-
'/' !== $from_url->pathname[ strlen( $from_url->pathname ) - 1 ] &&
349-
'/' !== $from_url->pathname &&
350-
'' === $from_url->search &&
351-
'' === $from_url->hash
352-
) {
353-
$new_raw_url = rtrim( $new_raw_url, '/' );
354-
}
355-
if ( ! $new_raw_url ) {
356-
// @TODO: When does this happen? Let's add the test coverage and
357-
// doubly verify the logic.
313+
if ( ! $base_url ) {
358314
return false;
359315
}
360316

361-
if ( ! $this->is_url_relative() ) {
362-
$this->set_url( $new_raw_url, $updated_url );
317+
$result = WPURL::replace_base_url(
318+
array(
319+
'url' => $this->get_parsed_url(),
320+
'old_base_url' => $base_url,
321+
'new_base_url' => $to_url,
322+
'raw_url' => $this->get_raw_url(),
323+
'is_relative' => $this->is_url_relative(),
324+
'return_array' => true,
325+
)
326+
);
363327

364-
return true;
328+
if ( false === $result ) {
329+
return false;
365330
}
366331

367-
$new_relative_url = $updated_url->pathname;
368-
if ( '' !== $updated_url->search ) {
369-
$new_relative_url .= $updated_url->search;
370-
}
371-
if ( '' !== $updated_url->hash ) {
372-
$new_relative_url .= $updated_url->hash;
373-
}
332+
if ( $result['was_relative'] ) {
333+
$new_relative_url = $result['relative_url'];
334+
if ( null === $new_relative_url ) {
335+
$new_relative_url = $result['url']->pathname;
336+
if ( '' !== $result['url']->search ) {
337+
$new_relative_url .= $result['url']->search;
338+
}
339+
if ( '' !== $result['url']->hash ) {
340+
$new_relative_url .= $result['url']->hash;
341+
}
342+
}
374343

375-
$this->set_url( $new_relative_url, $updated_url );
344+
$this->set_url( $new_relative_url, $result['url'] );
345+
} else {
346+
$this->set_url( $result['string'], $result['url'] );
347+
}
376348

377349
return true;
378350
}

wp-content/plugins/wordpress-importer/php-toolkit/DataLiberation/URL/class-wpurl.php

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,126 @@ public static function can_parse( $url, $base = null ) {
2525
return URL::canParse( $url, $base );
2626
}
2727

28+
/**
29+
* Replaces the base in a URL with a different base.
30+
*
31+
* A base is a protocol, host, and a path segment.
32+
*
33+
* Expected options:
34+
* - url (string|URL): The URL whose base should be replaced. Required.
35+
* - old_base_url (string|URL): The base URL currently associated with the URL. Required.
36+
* - new_base_url (string|URL): The base URL that should replace the existing one. Required.
37+
* - raw_url (string, optional): The original raw URL string. Used to detect relativity.
38+
* - is_relative (bool, optional): Whether the original URL was relative. Overrides raw_url detection.
39+
* - return_array (bool, optional): Whether to return the detailed array structure. Default false.
40+
*
41+
* @param array $options The options that control how the base URL is replaced.
42+
*
43+
* @return string|array|false Returns a string by default, or an array with keys 'url', 'string',
44+
* 'relative_url', and 'was_relative' when 'return_array' is truthy.
45+
* Returns false on failure.
46+
*/
47+
public static function replace_base_url( $options ) {
48+
if ( ! is_array( $options ) ) {
49+
return false;
50+
}
51+
52+
foreach ( array( 'url', 'old_base_url', 'new_base_url' ) as $required ) {
53+
if ( ! array_key_exists( $required, $options ) || null === $options[ $required ] ) {
54+
return false;
55+
}
56+
}
57+
58+
$old_base_url = self::parse( $options['old_base_url'] );
59+
$new_base_url = self::parse( $options['new_base_url'] );
60+
$url = self::parse( $options['url'], $old_base_url ? $old_base_url->toString() : null );
61+
62+
if ( false === $old_base_url || false === $new_base_url || false === $url ) {
63+
return false;
64+
}
65+
66+
$updated_url = clone $url;
67+
68+
$updated_url->hostname = $new_base_url->hostname;
69+
$updated_url->protocol = $new_base_url->protocol;
70+
$updated_url->port = $new_base_url->port;
71+
72+
$from_pathname = $url->pathname;
73+
$to_pathname = $new_base_url->pathname;
74+
$base_pathname = $old_base_url->pathname;
75+
76+
if ( $base_pathname !== $to_pathname ) {
77+
$base_pathname_with_trailing_slash = rtrim( $base_pathname, '/' ) . '/';
78+
$decoded_matched_pathname = urldecode_n(
79+
$from_pathname,
80+
strlen( $base_pathname_with_trailing_slash )
81+
);
82+
$to_pathname_with_trailing_slash = rtrim( $to_pathname, '/' ) . '/';
83+
$remaining_pathname = substr(
84+
$decoded_matched_pathname,
85+
strlen( $base_pathname_with_trailing_slash )
86+
);
87+
88+
$updated_url->pathname = $to_pathname_with_trailing_slash . $remaining_pathname;
89+
}
90+
91+
/*
92+
* Stylistic choice – if the updated URL has no trailing slash,
93+
* do not add it to the new URL. The WHATWG URL parser will
94+
* add one automatically if the path is empty, so we have to
95+
* explicitly remove it.
96+
*/
97+
$new_raw_url = $updated_url->toString();
98+
$should_trim_trailing_slash = (
99+
'' !== $from_pathname &&
100+
'/' !== substr( $from_pathname, -1 ) &&
101+
'/' !== $from_pathname &&
102+
'' === $url->search &&
103+
'' === $url->hash
104+
);
105+
if ( $should_trim_trailing_slash ) {
106+
$new_raw_url = rtrim( $new_raw_url, '/' );
107+
}
108+
if ( ! $new_raw_url ) {
109+
return false;
110+
}
111+
112+
$was_relative = null;
113+
if ( array_key_exists( 'is_relative', $options ) ) {
114+
$was_relative = $options['is_relative'];
115+
}
116+
if ( null === $was_relative && array_key_exists( 'raw_url', $options ) && is_string( $options['raw_url'] ) ) {
117+
$was_relative = ! self::can_parse( $options['raw_url'] );
118+
}
119+
if ( null === $was_relative ) {
120+
$was_relative = false;
121+
}
122+
123+
$relative_url = null;
124+
if ( $was_relative ) {
125+
$relative_url = $updated_url->pathname;
126+
if ( '' !== $updated_url->search ) {
127+
$relative_url .= $updated_url->search;
128+
}
129+
if ( '' !== $updated_url->hash ) {
130+
$relative_url .= $updated_url->hash;
131+
}
132+
}
133+
134+
$result = array(
135+
'url' => $updated_url,
136+
'string' => $new_raw_url,
137+
'relative_url' => $relative_url,
138+
'was_relative' => (bool) $was_relative,
139+
);
140+
141+
if ( empty( $options['return_array'] ) ) {
142+
return $result['string'];
143+
}
144+
145+
return $result;
146+
}
147+
28148
/**
29149
* Prepends a protocol to any matched URL without the double slash.
30150
*

wp-content/plugins/wordpress-importer/php-toolkit/DataLiberation/vendor-patched/rowbot/punycode/src/Punycode.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ public static function decode( string $input, ?int $outLength = null, array &$ca
374374
$oldi = $i;
375375
$w = 1;
376376

377-
for ( $k = self::BASE; /* no condition */; $k += self::BASE ) {
377+
for ( $k = self::BASE; ; $k += self::BASE ) {
378378
if ( $in >= $inputLength ) {
379379
throw new InvalidInputException();
380380
}
@@ -496,7 +496,7 @@ public static function encode( string $input, ?int $outLength = null, array $cas
496496
} elseif ( $codePoint === $n ) {
497497
$q = $delta;
498498

499-
for ( $k = self::BASE; /* no condition */; $k += self::BASE ) {
499+
for ( $k = self::BASE; ; $k += self::BASE ) {
500500
if ( $out >= $maxOut ) {
501501
throw new OutputSizeExceededException();
502502
}

wp-content/plugins/wordpress-importer/readme.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Tags: importer, wordpress
55
Requires at least: 5.2
66
Tested up to: 6.8
77
Requires PHP: 7.2
8-
Stable tag: 0.9.2
8+
Stable tag: 0.9.3
99
License: GPLv2 or later
1010
License URI: https://www.gnu.org/licenses/gpl-2.0.html
1111

@@ -40,6 +40,9 @@ If you would prefer to do things manually then follow these instructions:
4040

4141
== Changelog ==
4242

43+
= 0.9.3 =
44+
* Rewrite attachment URLs to the new URL structure
45+
4346
= 0.9.2 =
4447
* Rewrite site URLs in block attributes.
4548

wp-content/plugins/wordpress-importer/wordpress-importer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* Description: Import posts, pages, comments, custom fields, categories, tags and more from a WordPress export file.
77
* Author: wordpressdotorg
88
* Author URI: https://wordpress.org/
9-
* Version: 0.9.2
9+
* Version: 0.9.3
1010
* Requires at least: 5.2
1111
* Requires PHP: 7.2
1212
* Text Domain: wordpress-importer

0 commit comments

Comments
 (0)