: str_replace(): Passing null to parameter #2 ($replace) of type array|string is deprecated in
* WordPress Importer class for managing the import process of a WXR file
* WordPress importer class.
class WP_Import extends WP_Importer {
var $max_wxr_version = 1.2; // max. supported WXR version
var $id; // WXR attachment ID
// information to import from WXR file
var $categories = array();
// mappings from old information to new
var $processed_authors = array();
var $author_mapping = array();
var $processed_terms = array();
var $processed_posts = array();
var $post_orphans = array();
var $processed_menu_items = array();
var $menu_item_orphans = array();
var $missing_menu_items = array();
var $fetch_attachments = false;
var $url_remap = array();
var $featured_images = array();
* Registered callback function for the WordPress Importer
* Manages the three separate stages of the WXR import process
$step = empty( $_GET['step'] ) ? 0 : (int) $_GET['step'];
check_admin_referer( 'import-upload' );
if ( $this->handle_upload() ) {
check_admin_referer( 'import-wordpress' );
$this->fetch_attachments = ( ! empty( $_POST['fetch_attachments'] ) && $this->allow_fetch_attachments() );
$this->id = (int) $_POST['import_id'];
$file = get_attached_file( $this->id );
* The main controller for the actual import stage.
* @param string $file Path to the WXR file for importing
function import( $file ) {
add_filter( 'import_post_meta_key', array( $this, 'is_valid_meta_key' ) );
add_filter( 'http_request_timeout', array( &$this, 'bump_request_timeout' ) );
$this->import_start( $file );
$this->get_author_mapping();
wp_suspend_cache_invalidation( true );
$this->process_categories();
wp_suspend_cache_invalidation( false );
// update incorrect/missing information in the DB
$this->backfill_parents();
$this->backfill_attachment_urls();
$this->remap_featured_images();
* Parses the WXR file and prepares us for the task of processing parsed data
* @param string $file Path to the WXR file for importing
function import_start( $file ) {
if ( ! is_file( $file ) ) {
echo '<p><strong>' . __( 'Sorry, there has been an error.', 'wordpress-importer' ) . '</strong><br />';
echo __( 'The file does not exist, please try again.', 'wordpress-importer' ) . '</p>';
$import_data = $this->parse( $file );
if ( is_wp_error( $import_data ) ) {
echo '<p><strong>' . __( 'Sorry, there has been an error.', 'wordpress-importer' ) . '</strong><br />';
echo esc_html( $import_data->get_error_message() ) . '</p>';
$this->version = $import_data['version'];
$this->get_authors_from_import( $import_data );
$this->posts = $import_data['posts'];
$this->terms = $import_data['terms'];
$this->categories = $import_data['categories'];
$this->tags = $import_data['tags'];
$this->base_url = esc_url( $import_data['base_url'] );
wp_defer_term_counting( true );
wp_defer_comment_counting( true );
do_action( 'import_start' );
* Performs post-import cleanup of files and the cache
wp_import_cleanup( $this->id );
foreach ( get_taxonomies() as $tax ) {
delete_option( "{$tax}_children" );
_get_term_hierarchy( $tax );
wp_defer_term_counting( false );
wp_defer_comment_counting( false );
echo '<p>' . __( 'All done.', 'wordpress-importer' ) . ' <a href="' . admin_url() . '">' . __( 'Have fun!', 'wordpress-importer' ) . '</a>' . '</p>';
echo '<p>' . __( 'Remember to update the passwords and roles of imported users.', 'wordpress-importer' ) . '</p>';
do_action( 'import_end' );
* Handles the WXR upload and initial parsing of the file to prepare for
* displaying author import options
* @return bool False if error uploading or invalid file, true otherwise
function handle_upload() {
$file = wp_import_handle_upload();
if ( isset( $file['error'] ) ) {
echo '<p><strong>' . __( 'Sorry, there has been an error.', 'wordpress-importer' ) . '</strong><br />';
echo esc_html( $file['error'] ) . '</p>';
} elseif ( ! file_exists( $file['file'] ) ) {
echo '<p><strong>' . __( 'Sorry, there has been an error.', 'wordpress-importer' ) . '</strong><br />';
printf( __( 'The export file could not be found at <code>%s</code>. It is likely that this was caused by a permissions problem.', 'wordpress-importer' ), esc_html( $file['file'] ) );
$this->id = (int) $file['id'];
$import_data = $this->parse( $file['file'] );
if ( is_wp_error( $import_data ) ) {
echo '<p><strong>' . __( 'Sorry, there has been an error.', 'wordpress-importer' ) . '</strong><br />';
echo esc_html( $import_data->get_error_message() ) . '</p>';
$this->version = $import_data['version'];
if ( $this->version > $this->max_wxr_version ) {
echo '<div class="error"><p><strong>';
printf( __( 'This WXR file (version %s) may not be supported by this version of the importer. Please consider updating.', 'wordpress-importer' ), esc_html( $import_data['version'] ) );
echo '</strong></p></div>';
$this->get_authors_from_import( $import_data );
* Retrieve authors from parsed WXR data
* Uses the provided author information from WXR 1.1 files
* or extracts info from each post for WXR 1.0 files
* @param array $import_data Data returned by a WXR parser
function get_authors_from_import( $import_data ) {
if ( ! empty( $import_data['authors'] ) ) {
$this->authors = $import_data['authors'];
// no author information, grab it from the posts
foreach ( $import_data['posts'] as $post ) {
$login = sanitize_user( $post['post_author'], true );
printf( __( 'Failed to import author %s. Their posts will be attributed to the current user.', 'wordpress-importer' ), esc_html( $post['post_author'] ) );
if ( ! isset( $this->authors[ $login ] ) ) {
$this->authors[ $login ] = array(
'author_login' => $login,
'author_display_name' => $post['post_author'],
* Display pre-import options, author importing/mapping and option to
function import_options() {
// phpcs:disable Generic.WhiteSpace.ScopeIndent.Incorrect
<form action="<?php echo admin_url( 'admin.php?import=wordpress&step=2' ); ?>" method="post">
<?php wp_nonce_field( 'import-wordpress' ); ?>
<input type="hidden" name="import_id" value="<?php echo $this->id; ?>" />
<?php if ( ! empty( $this->authors ) ) : ?>
<h3><?php _e( 'Assign Authors', 'wordpress-importer' ); ?></h3>
<p><?php _e( 'To make it simpler for you to edit and save the imported content, you may want to reassign the author of the imported item to an existing user of this site, such as your primary administrator account.', 'wordpress-importer' ); ?></p>
<?php if ( $this->allow_create_users() ) : ?>
<p><?php printf( __( 'If a new user is created by WordPress, a new password will be randomly generated and the new user’s role will be set as %s. Manually changing the new user’s details will be necessary.', 'wordpress-importer' ), esc_html( get_option( 'default_role' ) ) ); ?></p>
<?php foreach ( $this->authors as $author ) : ?>
<li><?php $this->author_select( $j++, $author ); ?></li>
<?php if ( $this->allow_fetch_attachments() ) : ?>
<h3><?php _e( 'Import Attachments', 'wordpress-importer' ); ?></h3>
<input type="checkbox" value="1" name="fetch_attachments" id="import-attachments" />
<label for="import-attachments"><?php _e( 'Download and import file attachments', 'wordpress-importer' ); ?></label>
<p class="submit"><input type="submit" class="button" value="<?php esc_attr_e( 'Submit', 'wordpress-importer' ); ?>" /></p>
// phpcs:enable Generic.WhiteSpace.ScopeIndent.Incorrect
* Display import options for an individual author. That is, either create
* a new user based on import info or map to an existing user
* @param int $n Index for each author in the form
* @param array $author Author information, e.g. login, display name, email
function author_select( $n, $author ) {
_e( 'Import author:', 'wordpress-importer' );
echo ' <strong>' . esc_html( $author['author_display_name'] );
if ( '1.0' != $this->version ) {
echo ' (' . esc_html( $author['author_login'] ) . ')';
if ( '1.0' != $this->version ) {
echo '<div style="margin-left:18px">';
$create_users = $this->allow_create_users();
echo '<label for="user_new_' . $n . '">';
if ( '1.0' != $this->version ) {
_e( 'or create new user with login name:', 'wordpress-importer' );
_e( 'as a new user:', 'wordpress-importer' );
$value = esc_attr( sanitize_user( $author['author_login'], true ) );
echo ' <input type="text" id="user_new_' . $n . '" name="user_new[' . $n . ']" value="' . $value . '" /><br />';
echo '<label for="imported_authors_' . $n . '">';
if ( ! $create_users && '1.0' == $this->version ) {
_e( 'assign posts to an existing user:', 'wordpress-importer' );
_e( 'or assign posts to an existing user:', 'wordpress-importer' );
echo ' ' . wp_dropdown_users(
'name' => "user_map[$n]",
'id' => 'imported_authors_' . $n,
'show_option_all' => __( '- Select -', 'wordpress-importer' ),
'show' => 'display_name_with_login',
echo '<input type="hidden" name="imported_authors[' . $n . ']" value="' . esc_attr( $author['author_login'] ) . '" />';
if ( '1.0' != $this->version ) {
* Map old author logins to local user IDs based on decisions made
* in import options form. Can map to an existing user, create a new user
* or falls back to the current user in case of error with either of the previous
function get_author_mapping() {
if ( ! isset( $_POST['imported_authors'] ) ) {
$create_users = $this->allow_create_users();
foreach ( (array) $_POST['imported_authors'] as $i => $old_login ) {
// Multisite adds strtolower to sanitize_user. Need to sanitize here to stop breakage in process_posts.
$santized_old_login = sanitize_user( $old_login, true );
$old_id = isset( $this->authors[ $old_login ]['author_id'] ) ? intval( $this->authors[ $old_login ]['author_id'] ) : false;
if ( ! empty( $_POST['user_map'][ $i ] ) ) {
$user = get_userdata( intval( $_POST['user_map'][ $i ] ) );
if ( isset( $user->ID ) ) {
$this->processed_authors[ $old_id ] = $user->ID;
$this->author_mapping[ $santized_old_login ] = $user->ID;
} elseif ( $create_users ) {
if ( ! empty( $_POST['user_new'][ $i ] ) ) {
$user_id = wp_create_user( $_POST['user_new'][ $i ], wp_generate_password() );
} elseif ( '1.0' != $this->version ) {
'user_login' => $old_login,
'user_pass' => wp_generate_password(),
'user_email' => isset( $this->authors[ $old_login ]['author_email'] ) ? $this->authors[ $old_login ]['author_email'] : '',
'display_name' => $this->authors[ $old_login ]['author_display_name'],
'first_name' => isset( $this->authors[ $old_login ]['author_first_name'] ) ? $this->authors[ $old_login ]['author_first_name'] : '',
'last_name' => isset( $this->authors[ $old_login ]['author_last_name'] ) ? $this->authors[ $old_login ]['author_last_name'] : '',
$user_id = wp_insert_user( $user_data );
if ( ! is_wp_error( $user_id ) ) {
$this->processed_authors[ $old_id ] = $user_id;
$this->author_mapping[ $santized_old_login ] = $user_id;
printf( __( 'Failed to create new user for %s. Their posts will be attributed to the current user.', 'wordpress-importer' ), esc_html( $this->authors[ $old_login ]['author_display_name'] ) );
if ( defined( 'IMPORT_DEBUG' ) && IMPORT_DEBUG ) {
echo ' ' . $user_id->get_error_message();
// failsafe: if the user_id was invalid, default to the current user
if ( ! isset( $this->author_mapping[ $santized_old_login ] ) ) {
$this->processed_authors[ $old_id ] = (int) get_current_user_id();
$this->author_mapping[ $santized_old_login ] = (int) get_current_user_id();
* Create new categories based on import information
* Doesn't create a new category if its slug already exists
function process_categories() {
$this->categories = apply_filters( 'wp_import_categories', $this->categories );
if ( empty( $this->categories ) ) {
foreach ( $this->categories as $cat ) {
// if the category already exists leave it alone
$term_id = term_exists( $cat['category_nicename'], 'category' );
if ( is_array( $term_id ) ) {
$term_id = $term_id['term_id'];
if ( isset( $cat['term_id'] ) ) {
$this->processed_terms[ intval( $cat['term_id'] ) ] = (int) $term_id;
$parent = empty( $cat['category_parent'] ) ? 0 : category_exists( $cat['category_parent'] );
$description = isset( $cat['category_description'] ) ? $cat['category_description'] : '';
'category_nicename' => $cat['category_nicename'],
'category_parent' => $parent,
'cat_name' => wp_slash( $cat['cat_name'] ),
'category_description' => wp_slash( $description ),
$id = wp_insert_category( $data, true );
if ( ! is_wp_error( $id ) && $id > 0 ) {
if ( isset( $cat['term_id'] ) ) {
$this->processed_terms[ intval( $cat['term_id'] ) ] = $id;
printf( __( 'Failed to import category %s', 'wordpress-importer' ), esc_html( $cat['category_nicename'] ) );
if ( defined( 'IMPORT_DEBUG' ) && IMPORT_DEBUG ) {
echo ': ' . $id->get_error_message();
$this->process_termmeta( $cat, $id );
unset( $this->categories );
* Create new post tags based on import information
* Doesn't create a tag if its slug already exists
function process_tags() {
$this->tags = apply_filters( 'wp_import_tags', $this->tags );
if ( empty( $this->tags ) ) {
foreach ( $this->tags as $tag ) {
// if the tag already exists leave it alone
$term_id = term_exists( $tag['tag_slug'], 'post_tag' );
if ( is_array( $term_id ) ) {
$term_id = $term_id['term_id'];
if ( isset( $tag['term_id'] ) ) {
$this->processed_terms[ intval( $tag['term_id'] ) ] = (int) $term_id;
$description = isset( $tag['tag_description'] ) ? $tag['tag_description'] : '';
'slug' => $tag['tag_slug'],
'description' => wp_slash( $description ),
$id = wp_insert_term( wp_slash( $tag['tag_name'] ), 'post_tag', $args );
if ( ! is_wp_error( $id ) ) {
if ( isset( $tag['term_id'] ) ) {
$this->processed_terms[ intval( $tag['term_id'] ) ] = $id['term_id'];
printf( __( 'Failed to import post tag %s', 'wordpress-importer' ), esc_html( $tag['tag_name'] ) );
if ( defined( 'IMPORT_DEBUG' ) && IMPORT_DEBUG ) {
echo ': ' . $id->get_error_message();
$this->process_termmeta( $tag, $id['term_id'] );
* Create new terms based on import information
* Doesn't create a term its slug already exists