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
58 changes: 58 additions & 0 deletions features/import.feature
Original file line number Diff line number Diff line change
Expand Up @@ -400,3 +400,61 @@ Feature: Import content.
https://newsite.com/
"""

@require-wp-5.2 @require-mysql
Scenario: Specifying a non-existent importer class produces an error
Given a WP install
And I run `wp plugin install wordpress-importer --activate`

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

When I try `wp import {EXPORT_FILE} --authors=skip --importer=NonExistentImporterClass`
Then STDERR should contain:
"""
Error: Importer class 'NonExistentImporterClass' does not exist.
"""
And the return code should be 1

@require-wp-5.2 @require-mysql
Scenario: Specifying an importer class that is not a subclass of WP_Import produces an error
Given a WP install
And I run `wp plugin install wordpress-importer --activate`

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

When I try `wp import {EXPORT_FILE} --authors=skip --importer=WP_CLI_Command`
Then STDERR should contain:
"""
Error: Importer class 'WP_CLI_Command' must be a subclass of WP_Import.
"""
And the return code should be 1

@require-wp-5.2 @require-mysql
Scenario: Specifying a valid custom importer subclass succeeds
Given a WP install
And I run `wp plugin install wordpress-importer --activate`
And a wp-content/mu-plugins/custom-importer.php file:
"""
<?php
if ( ! class_exists( 'WP_Importer' ) ) {
require_once ABSPATH . 'wp-admin/includes/class-wp-importer.php';
}

if ( ! class_exists( 'WP_Import' ) ) {
require_once WP_PLUGIN_DIR . '/wordpress-importer/class-wp-import.php';
}

class My_Custom_WP_Import extends WP_Import {}
"""

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 import {EXPORT_FILE} --authors=skip --importer=My_Custom_WP_Import`
Then STDOUT should not be empty
And STDERR should be empty

18 changes: 17 additions & 1 deletion src/Import_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ class Import_Command extends WP_CLI_Command {
* : 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.
*
* [--importer=<importer>]
* : Use a custom importer class instead of the default WP_Import. The class must exist and be a subclass of WP_Import.
*
* ## EXAMPLES
*
* # Import content from a WXR file
Expand All @@ -45,6 +48,7 @@ public function __invoke( $args, $assoc_args ) {
'authors' => null,
'skip' => [],
'rewrite_urls' => null,
'importer' => 'WP_Import',
);
$assoc_args = wp_parse_args( $assoc_args, $defaults );

Expand All @@ -57,6 +61,16 @@ public function __invoke( $args, $assoc_args ) {
WP_CLI::error( $importer );
}

$importer_class = $assoc_args['importer'];
if ( 'WP_Import' !== $importer_class ) {
if ( ! class_exists( $importer_class, false ) ) {
WP_CLI::error( "Importer class '$importer_class' does not exist." );
}
if ( ! is_subclass_of( $importer_class, 'WP_Import' ) ) {
WP_CLI::error( "Importer class '$importer_class' must be a subclass of WP_Import." );
}
}

$this->add_wxr_filters();

WP_CLI::log( 'Starting the import process...' );
Expand Down Expand Up @@ -117,7 +131,9 @@ public function __invoke( $args, $assoc_args ) {
*/
private function import_wxr( $file, $args ) {

$wp_import = new WP_Import();
$importer_class = $args['importer'];
/** @var WP_Import $wp_import */
$wp_import = new $importer_class();
$wp_import->processed_posts = $this->processed_posts;
$import_data = $wp_import->parse( $file );

Expand Down
Loading