diff --git a/features/import.feature b/features/import.feature index 369f374e..9dc099a4 100644 --- a/features/import.feature +++ b/features/import.feature @@ -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='Click me' --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/ + """ + diff --git a/src/Import_Command.php b/src/Import_Command.php index 74378de3..72cefe0f 100644 --- a/src/Import_Command.php +++ b/src/Import_Command.php @@ -25,6 +25,10 @@ class Import_Command extends WP_CLI_Command { * [--skip=] * : 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 @@ -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 ); @@ -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;