Skip to content

Commit 8e6f617

Browse files
Copilotswissspidy
andcommitted
Add --importer argument to specify alternative importer class
Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
1 parent 581419d commit 8e6f617

2 files changed

Lines changed: 70 additions & 1 deletion

File tree

‎features/import.feature‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,3 +400,53 @@ Feature: Import content.
400400
https://newsite.com/
401401
"""
402402

403+
@require-wp-5.2 @require-mysql
404+
Scenario: Specifying a non-existent importer class produces an error
405+
Given a WP install
406+
And I run `wp plugin install wordpress-importer --activate`
407+
408+
When I run `wp export`
409+
Then save STDOUT 'Writing to file %s' as {EXPORT_FILE}
410+
411+
When I try `wp import {EXPORT_FILE} --authors=skip --importer=NonExistentImporterClass`
412+
Then STDERR should contain:
413+
"""
414+
Error: Importer class 'NonExistentImporterClass' does not exist.
415+
"""
416+
And the return code should be 1
417+
418+
@require-wp-5.2 @require-mysql
419+
Scenario: Specifying an importer class that is not a subclass of WP_Import produces an error
420+
Given a WP install
421+
And I run `wp plugin install wordpress-importer --activate`
422+
423+
When I run `wp export`
424+
Then save STDOUT 'Writing to file %s' as {EXPORT_FILE}
425+
426+
When I try `wp import {EXPORT_FILE} --authors=skip --importer=WP_CLI_Command`
427+
Then STDERR should contain:
428+
"""
429+
Error: Importer class 'WP_CLI_Command' must be a subclass of WP_Import.
430+
"""
431+
And the return code should be 1
432+
433+
@require-wp-5.2 @require-mysql
434+
Scenario: Specifying a valid custom importer subclass succeeds
435+
Given a WP install
436+
And I run `wp plugin install wordpress-importer --activate`
437+
And a wp-content/mu-plugins/custom-importer.php file:
438+
"""
439+
<?php
440+
class My_Custom_WP_Import extends WP_Import {}
441+
"""
442+
443+
When I run `wp export`
444+
Then save STDOUT 'Writing to file %s' as {EXPORT_FILE}
445+
446+
When I run `wp site empty --yes`
447+
Then STDOUT should not be empty
448+
449+
When I run `wp import {EXPORT_FILE} --authors=skip --importer=My_Custom_WP_Import`
450+
Then STDOUT should not be empty
451+
And STDERR should be empty
452+

‎src/Import_Command.php‎

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ class Import_Command extends WP_CLI_Command {
2929
* : Change all imported URLs that currently link to the previous site so that they now link to this site
3030
* Requires WordPress Importer version 0.9.1 or newer.
3131
*
32+
* [--importer=<importer>]
33+
* : Use a custom importer class instead of the default WP_Import. The class must exist and be a subclass of WP_Import.
34+
*
3235
* ## EXAMPLES
3336
*
3437
* # Import content from a WXR file
@@ -45,6 +48,7 @@ public function __invoke( $args, $assoc_args ) {
4548
'authors' => null,
4649
'skip' => [],
4750
'rewrite_urls' => null,
51+
'importer' => 'WP_Import',
4852
);
4953
$assoc_args = wp_parse_args( $assoc_args, $defaults );
5054

@@ -57,6 +61,19 @@ public function __invoke( $args, $assoc_args ) {
5761
WP_CLI::error( $importer );
5862
}
5963

64+
$importer_class = $assoc_args['importer'];
65+
if ( 'WP_Import' !== $importer_class ) {
66+
if ( ! preg_match( '/^[a-zA-Z_\x80-\xff][a-zA-Z0-9_\x80-\xff\\\\]*$/', $importer_class ) ) {
67+
WP_CLI::error( "Importer class '$importer_class' is not a valid class name." );
68+
}
69+
if ( ! class_exists( $importer_class ) ) {
70+
WP_CLI::error( "Importer class '$importer_class' does not exist." );
71+
}
72+
if ( ! is_subclass_of( $importer_class, 'WP_Import' ) ) {
73+
WP_CLI::error( "Importer class '$importer_class' must be a subclass of WP_Import." );
74+
}
75+
}
76+
6077
$this->add_wxr_filters();
6178

6279
WP_CLI::log( 'Starting the import process...' );
@@ -117,7 +134,9 @@ public function __invoke( $args, $assoc_args ) {
117134
*/
118135
private function import_wxr( $file, $args ) {
119136

120-
$wp_import = new WP_Import();
137+
$importer_class = $args['importer'];
138+
/** @var WP_Import $wp_import */
139+
$wp_import = new $importer_class();
121140
$wp_import->processed_posts = $this->processed_posts;
122141
$import_data = $wp_import->parse( $file );
123142

0 commit comments

Comments
 (0)