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
7 changes: 7 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,18 @@
"wp-cli/wp-cli": "^2.12"
},
"require-dev": {
"wordpress/wordpress-importer": "^0.8",
"wp-cli/entity-command": "^1.3 || ^2",
"wp-cli/export-command": "^1 || ^2",
"wp-cli/extension-command": "^1.2 || ^2",
"wp-cli/wp-cli-tests": "^5"
},
"repositories": [
{
"type": "vcs",
"url": "https://github.com/wordpress/wordpress-importer"
}
],
"config": {
"process-timeout": 7200,
"sort-packages": true,
Expand Down
15 changes: 15 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
parameters:
level: 9
paths:
- src
- import-command.php
scanDirectories:
- vendor/wp-cli/wp-cli/php
- vendor/wordpress/wordpress-importer/src
scanFiles:
- vendor/php-stubs/wordpress-stubs/wordpress-stubs.php
treatPhpDocTypesAsCertain: false
ignoreErrors:
- identifier: missingType.property
- identifier: missingType.parameter
- identifier: missingType.return
36 changes: 29 additions & 7 deletions src/Import_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

class Import_Command extends WP_CLI_Command {

private $blog_users = array();

public $processed_posts = array();

/**
Expand Down Expand Up @@ -113,9 +115,6 @@ private function import_wxr( $file, $args ) {
$wp_import = new WP_Import();
$wp_import->processed_posts = $this->processed_posts;
$import_data = $wp_import->parse( $file );
if ( is_wp_error( $import_data ) ) {
return $import_data;
}

// Prepare the data to be used in process_author_mapping();
$wp_import->get_authors_from_import( $import_data );
Expand Down Expand Up @@ -147,6 +146,10 @@ private function import_wxr( $file, $args ) {
$author_data[] = $author;
}

/**
* @var array<\WP_User> $author_data
*/

// Build the author mapping
$author_mapping = $this->process_author_mapping( $args['authors'], $author_data );
if ( is_wp_error( $author_mapping ) ) {
Expand Down Expand Up @@ -256,6 +259,7 @@ function ( $post_id ) {
}

if ( 0 === ( $wpcli_import_counts['current_post'] % 500 ) ) {
// @phpstan-ignore function.deprecated
WP_CLI\Utils\wp_clear_object_cache();
WP_CLI::log( '-- Cleared object cache.' );
}
Expand Down Expand Up @@ -323,9 +327,9 @@ private function is_importer_available() {
/**
* Processes how the authors should be mapped
*
* @param string $authors_arg The `--author` argument originally passed to command
* @param array $author_data An array of WP_User-esque author objects
* @return array|WP_Error $author_mapping Author mapping array if successful, WP_Error if something bad happened
* @param string $authors_arg The `--author` argument originally passed to command
* @param array<\WP_User> $author_data An array of WP_User-esque author objects
* @return array<\WP_User>|WP_Error Author mapping array if successful, WP_Error if something bad happened
*/
private function process_author_mapping( $authors_arg, $author_data ) {

Expand Down Expand Up @@ -360,6 +364,9 @@ private function read_author_mapping_file( $file ) {
$author_mapping = array();

foreach ( new \WP_CLI\Iterators\CSV( $file ) as $i => $author ) {
/**
* @var array<string, \WP_User> $author
*/
if ( ! array_key_exists( 'old_user_login', $author ) || ! array_key_exists( 'new_user_login', $author ) ) {
return new WP_Error( 'invalid-author-mapping', "Author mapping file isn't properly formatted." );
}
Expand All @@ -386,7 +393,15 @@ private function create_author_mapping_file( $file, $author_data ) {
);
}
$file_resource = fopen( $file, 'w' );
\WP_CLI\utils\write_csv( $file_resource, $author_mapping, array( 'old_user_login', 'new_user_login' ) );

if ( ! $file_resource ) {
return new WP_Error( 'author-mapping-error', "Couldn't create author mapping file." );
}

// TODO: Fix $rows type upstream in write_csv()
// @phpstan-ignore argument.type
\WP_CLI\Utils\write_csv( $file_resource, $author_mapping, array( 'old_user_login', 'new_user_login' ) );

return new WP_Error( 'author-mapping-error', sprintf( 'Please update author mapping file before continuing: %s', $file ) );
} else {
return new WP_Error( 'author-mapping-error', "Couldn't create author mapping file." );
Expand All @@ -395,6 +410,8 @@ private function create_author_mapping_file( $file, $author_data ) {

/**
* Creates users if they don't exist, and build an author mapping file.
*
* @param array<\WP_User> $author_data
*/
private function create_authors_for_mapping( $author_data ) {

Expand Down Expand Up @@ -433,6 +450,9 @@ private function create_authors_for_mapping( $author_data ) {
return $user_id;
}

/**
* @var \WP_User $user
*/
$user = get_user_by( 'id', $user_id );
$author_mapping[] = array(
'old_user_login' => $author->user_login,
Expand All @@ -444,6 +464,8 @@ private function create_authors_for_mapping( $author_data ) {

/**
* Suggests a blog user based on the levenshtein distance.
*
* @return string|\WP_User
*/
private function suggest_user( $author_user_login, $author_user_email = '' ) {

Expand Down