Skip to content
Merged
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
45 changes: 45 additions & 0 deletions features/import.feature
Original file line number Diff line number Diff line change
Expand Up @@ -355,3 +355,48 @@ Feature: Import content.
Warning:
"""
And the return code should be 1

@require-wp-5.2 @require-mysql
Scenario: Rewrite URLs
Given a WP install

When I run `wp option get home`
Then save STDOUT as {HOME}

When I run `wp site empty --yes`
And I run `wp post create --post_title='Post with URL' --post_content='<a href={HOME}>Click me</a>' --post_status='publish'`
And I run `wp post list --post_type=any --format=csv --fields=post_content`
Then STDOUT should contain:
"""
{HOME}
"""

When I run `wp export`
Then save STDOUT 'Writing to file %s' as {EXPORT_FILE}

When I run `wp site empty --yes`
Then STDOUT should not be empty

When I run `wp post list --post_type=any --format=count`
Then STDOUT should be:
"""
0
"""

When I run `wp plugin install wordpress-importer --activate`
Then STDERR should not contain:
"""
Warning:
"""

When I run `wp option update home https://newsite.com/`
And I run `wp option update siteurl https://newsite.com`
And I run `wp import {EXPORT_FILE} --authors=skip --rewrite_urls`
Then STDOUT should not be empty

When I run `wp post list --post_type=any --format=csv --fields=post_content`
Then STDOUT should contain:
"""
https://newsite.com/
"""

29 changes: 26 additions & 3 deletions src/Import_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ class Import_Command extends WP_CLI_Command {
* [--skip=<data-type>]
* : Skip importing specific data. Supported options are: 'attachment' and 'image_resize' (skip time-consuming thumbnail generation).
*
* [--rewrite_urls]
* : Change all imported URLs that currently link to the previous site so that they now link to this site
* Requires WordPress Importer version 0.9.1 or newer.
*
* ## EXAMPLES
*
* # Import content from a WXR file
Expand All @@ -38,8 +42,9 @@ class Import_Command extends WP_CLI_Command {
*/
public function __invoke( $args, $assoc_args ) {
$defaults = array(
'authors' => null,
'skip' => array(),
'authors' => null,
'skip' => array(),
'rewrite_urls' => null,
);
$assoc_args = wp_parse_args( $assoc_args, $defaults );

Expand Down Expand Up @@ -195,7 +200,25 @@ private function import_wxr( $file, $args ) {
}

$GLOBALS['wpcli_import_current_file'] = basename( $file );
$wp_import->import( $file );

$reflection = new \ReflectionMethod( $wp_import, 'import' );
$number_of_arguments = $reflection->getNumberOfParameters();

if ( null !== $args['rewrite_urls'] && $number_of_arguments < 2 ) {
WP_CLI::error( 'URL rewriting requires WordPress Importer version 0.9.1 or newer.' );
}

if ( $number_of_arguments > 1 ) {
$wp_import->import(
$file,
[
'rewrite_urls' => $args['rewrite_urls'],
]
);
} else {
$wp_import->import( $file );
}

$this->processed_posts += $wp_import->processed_posts;

return true;
Expand Down