Skip to content

Commit 2e6607d

Browse files
committed
Add URL rewriting support
1 parent e7d54c9 commit 2e6607d

File tree

2 files changed

+71
-3
lines changed

2 files changed

+71
-3
lines changed

features/import.feature

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,3 +355,48 @@ Feature: Import content.
355355
Warning:
356356
"""
357357
And the return code should be 1
358+
359+
@require-wp-5.2 @require-mysql
360+
Scenario: Rewrite URLs
361+
Given a WP install
362+
363+
When I run `wp option get home`
364+
Then save STDOUT as {HOME}
365+
366+
When I run `wp site empty --yes`
367+
And I run `wp post create --post_title='Post with URL' --post_content='<a href={HOME}>Click me</a>' --post_status='publish'`
368+
And I run `wp post list --post_type=any --format=csv --fields=post_content`
369+
Then STDOUT should contain:
370+
"""
371+
{HOME}
372+
"""
373+
374+
When I run `wp export`
375+
Then save STDOUT 'Writing to file %s' as {EXPORT_FILE}
376+
377+
When I run `wp site empty --yes`
378+
Then STDOUT should not be empty
379+
380+
When I run `wp post list --post_type=any --format=count`
381+
Then STDOUT should be:
382+
"""
383+
0
384+
"""
385+
386+
When I run `wp plugin install wordpress-importer --activate`
387+
Then STDERR should not contain:
388+
"""
389+
Warning:
390+
"""
391+
392+
When I run `wp option update home https://newsite.com/`
393+
And I run `wp option update siteurl https://newsite.com`
394+
When I run `wp import {EXPORT_FILE} --authors=skip --rewrite_urls`
395+
Then STDOUT should not be empty
396+
397+
When I run `wp post list --post_type=any --format=csv --fields=post_content`
398+
Then STDOUT should contain:
399+
"""
400+
https://newsite.com/
401+
"""
402+

src/Import_Command.php

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ class Import_Command extends WP_CLI_Command {
2525
* [--skip=<data-type>]
2626
* : Skip importing specific data. Supported options are: 'attachment' and 'image_resize' (skip time-consuming thumbnail generation).
2727
*
28+
* [--rewrite_urls]
29+
* : Change all imported URLs that currently link to the previous site so that they now link to this site
30+
* Requires WordPress Importer version 0.9.1 or newer.
31+
*
2832
* ## EXAMPLES
2933
*
3034
* # Import content from a WXR file
@@ -38,8 +42,9 @@ class Import_Command extends WP_CLI_Command {
3842
*/
3943
public function __invoke( $args, $assoc_args ) {
4044
$defaults = array(
41-
'authors' => null,
42-
'skip' => array(),
45+
'authors' => null,
46+
'skip' => array(),
47+
'rewrite_urls' => null,
4348
);
4449
$assoc_args = wp_parse_args( $assoc_args, $defaults );
4550

@@ -195,7 +200,25 @@ private function import_wxr( $file, $args ) {
195200
}
196201

197202
$GLOBALS['wpcli_import_current_file'] = basename( $file );
198-
$wp_import->import( $file );
203+
204+
$reflection = new \ReflectionMethod( $wp_import, 'import' );
205+
$number_of_arguments = $reflection->getNumberOfParameters();
206+
207+
if ( null !== $args['rewrite_urls'] && $number_of_arguments < 2 ) {
208+
WP_CLI::error( 'URL rewriting requires WordPress Importer version 0.9.1 or newer.' );
209+
}
210+
211+
if ( $number_of_arguments > 1 ) {
212+
$wp_import->import(
213+
$file,
214+
[
215+
'rewrite_urls' => $args['rewrite_urls'],
216+
]
217+
);
218+
} else {
219+
$wp_import->import( $file );
220+
}
221+
199222
$this->processed_posts += $wp_import->processed_posts;
200223

201224
return true;

0 commit comments

Comments
 (0)