Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Map original ID based on username (optional, default to true) #155

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
38 changes: 25 additions & 13 deletions class-wxr-importer.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ public function __construct( $options = array() ) {
'fetch_attachments' => false,
'aggressive_url_search' => false,
'default_author' => null,
'map_usernames' => true,
) );
}

Expand Down Expand Up @@ -1537,20 +1538,31 @@ protected function process_author( $data, $meta ) {

$user_id = wp_insert_user( wp_slash( $userdata ) );
if ( is_wp_error( $user_id ) ) {
$this->logger->error( sprintf(
__( 'Failed to import user "%s"', 'wordpress-importer' ),
$userdata['user_login']
) );
$this->logger->debug( $user_id->get_error_message() );
if ( $this->options['map_usernames']
&& $user_id->get_error_code() === 'existing_user_login'
&& ( $match_username = get_user_by( 'login', $userdata['user_login'] ) ) ) {
$user_id = $match_username->ID;
$this->logger->info( sprintf(
__( 'Mapping original uid %d to user id %d since they belong to the same login name "%s"', 'wordpress-importer' ),
$original_id, $user_id, $userdata['user_login']
) );
}
else {
$this->logger->error( sprintf(
__( 'Failed to import user "%s"', 'wordpress-importer' ),
$userdata['user_login']
) );
$this->logger->debug( $user_id->get_error_message() );

/**
* User processing failed.
*
* @param WP_Error $user_id Error object.
* @param array $userdata Raw data imported for the user.
*/
do_action( 'wxr_importer.process_failed.user', $user_id, $userdata );
return false;
/**
* User processing failed.
*
* @param WP_Error $user_id Error object.
* @param array $userdata Raw data imported for the user.
*/
do_action( 'wxr_importer.process_failed.user', $user_id, $userdata );
return false;
}
}

if ( $original_id ) {
Expand Down