: str_replace(): Passing null to parameter #2 ($replace) of type array|string is deprecated in
if (!class_exists('WP_Importer')) {
$class_wp_importer = ABSPATH.'wp-admin/includes/class-wp-importer.php';
if (file_exists($class_wp_importer)) {
require $class_wp_importer;
require_once SG_POPUP_LIBS_PATH.'parsers.php';
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
public function dispatch()
$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) sanitize_text_field($_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
public 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_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
public function import_start($file)
echo '<p><strong>' . esc_html( __('Sorry, there has been an error.', 'popup-builder') ). '</strong><br />';
echo esc_html( __('The file does not exist, please try again.', 'popup-builder') ). '</p>';
$import_data = $this->parse($file);
if (is_wp_error($import_data)) {
echo '<p><strong>' . esc_html( __('Sorry, there has been an error.', 'popup-builder') ). '</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
public function import_end()
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>'.esc_html__('All done.', 'popup-builder').' <a href="'.esc_url( admin_url() ).'edit.php?post_type='.esc_html( SG_POPUP_POST_TYPE ).'">'.esc_html__('Have fun!', 'popup-builder').'</a>'.'</p>';
* 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
public function handle_upload()
$file = wp_import_handle_upload();
if (isset($file['error'])) {
echo '<p><strong>' . esc_html( __('Sorry, there has been an error.', 'popup-builder') ).'</strong><br />';
echo esc_html($file['error']).'</p>';
else if (!file_exists($file['file'])) {
echo '<p><strong>' . esc_html( __('Sorry, there has been an error.', 'popup-builder') ).'</strong><br />';
/* translators: export file path */
printf(wp_kses_post(__('The export file could not be found at <code>%s</code>. It is likely that this was caused by a permissions problem.', 'popup-builder')), esc_html($file['file']));
$this->id = (int)$file['id'];
$import_data = $this->parse($file['file']);
if (is_wp_error($import_data)) {
echo '<p><strong>' . esc_html( __('Sorry, there has been an error.', 'popup-builder') ).'</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>';
/* translators: import data version */
printf(wp_kses_post( __('This WXR file (version %s) may not be supported by this version of the importer. Please consider updating.', 'popup-builder') ), 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
public 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);
/* translators: post author */
printf(wp_kses_post( __('Failed to import author %s. Their posts will be attributed to the current user.', 'popup-builder') ), 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
public function import_options()
<form action="<?php echo esc_url( admin_url('admin.php?import='.SG_POPUP_POST_TYPE.'&step=2') ); ?>" method="post">
<?php wp_nonce_field('import-wordpress'); ?>
<input type="hidden" name="import_id" value="<?php echo esc_html($this->id); ?>" />
<?php if (!empty($this->authors)) : ?>
<h3><?php esc_html_e('Assign Authors', 'popup-builder'); ?></h3>
<p><?php esc_html_e('To make it easier 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. For example, you may want to import all the entries as <code>admin</code>s entries.', 'popup-builder'); ?></p>
<?php if ($this->allow_create_users()) : ?>
/* translators: default role */
printf(wp_kses_post(__('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.', 'popup-builder') ), esc_html(get_option('default_role'))); ?></p>
<?php foreach ($this->authors as $author) : ?>
<li><?php $this->author_select($j++, $author); ?></li>
<p class="submit"><input type="submit" class="button" value="<?php esc_attr_e('Submit', 'popup-builder'); ?>" /></p>
* 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
public function author_select($n, $author)
esc_html_e('Import author:', 'popup-builder');
echo ' <strong>' . esc_html($author['author_display_name']);
if ($this->version != '1.0') echo ' (' . esc_html($author['author_login']) . ')';
if ($this->version != '1.0')
echo '<div style="margin-left:18px">';
$create_users = $this->allow_create_users();
if ($this->version != '1.0') {
esc_html_e('or create new user with login name:', 'popup-builder');
esc_html_e('as a new user:', 'popup-builder');
$value = esc_attr(sanitize_user($author['author_login'], true));
echo ' <input type="text" name="user_new['.esc_attr($n).']" value="'. esc_attr( $value ) .'" /><br />';
if (!$create_users && $this->version == '1.0')
esc_html_e('assign posts to an existing user:', 'popup-builder');
esc_html_e('or assign posts to an existing user:', 'popup-builder');
wp_dropdown_users(array('name' => "user_map[$n]", 'multi' => true, 'show_option_all' => __('- Select -', 'popup-builder')));
echo '<input type="hidden" name="imported_authors['.esc_attr($n).']" value="' . esc_attr($author['author_login']) . '" />';
if ($this->version != '1.0')
* 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
public function get_author_mapping()
* We only allow administrator to do this action
if ( ! current_user_can( 'manage_options' ) ) {
$nonce = isset( $_POST['_wpnonce'] ) ? sanitize_text_field( $_POST['_wpnonce'] ) : '';
if ( empty( $nonce ) || !wp_verify_nonce( $nonce, 'import-wordpress' ) ) {
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]));
$this->processed_authors[$old_id] = $user->ID;
$this->author_mapping[$santized_old_login] = $user->ID;
} else if ($create_users) {
if (!empty($_POST['user_new'][$i])) {
$user_id = wp_create_user( sanitize_user( $_POST['user_new'][$i] ), wp_generate_password());
} else if ($this->version != '1.0') {
'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;
/* translators: author display name */
printf(wp_kses_post(__('Failed to create new user for %s. Their posts will be attributed to the current user.', 'popup-builder')), esc_html($this->authors[$old_login]['author_display_name']));
if (defined('IMPORT_DEBUG') && IMPORT_DEBUG)
echo ' ' . esc_html( $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
public 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;
$category_parent = empty($cat['category_parent']) ? 0 : category_exists($cat['category_parent']);
$category_description = isset($cat['category_description']) ? $cat['category_description'] : '';
'category_nicename' => $cat['category_nicename'],
'category_parent' => $category_parent,
'cat_name' => $cat['cat_name'],
'category_description' => $category_description
$catarr = wp_slash($catarr);
$id = wp_insert_category($catarr);
if (isset($cat['term_id']))
$this->processed_terms[intval($cat['term_id'])] = $id;
/* translators: category nicename */
printf(wp_kses_post(__('Failed to import category %s', 'popup-builder')), esc_html($cat['category_nicename']));
if (defined('IMPORT_DEBUG') && IMPORT_DEBUG)
echo ': ' . esc_html( $id->get_error_message() );
$this->process_termmeta($cat, $id['term_id']);
unset($this->categories);
* Create new post tags based on import information
* Doesn't create a tag if its slug already exists
public function process_tags()
$this->tags = apply_filters('wp_import_tags', $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;
$tag_desc = isset($tag['tag_description']) ? $tag['tag_description'] : '';
$tagarr = array('slug' => $tag['tag_slug'], 'description' => $tag_desc);
$id = wp_insert_term($tag['tag_name'], 'post_tag', $tagarr);
if (isset($tag['term_id']))
$this->processed_terms[intval($tag['term_id'])] = $id['term_id'];
/* translators: tag name */
printf(wp_kses_post(__('Failed to import post tag %s', 'popup-builder')), esc_html($tag['tag_name']));
if (defined('IMPORT_DEBUG') && IMPORT_DEBUG)
echo ': ' . esc_html( $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
public function process_terms()
$this->terms = apply_filters('wp_import_terms', $this->terms);
foreach ($this->terms as $term) {
// if the term already exists in the correct taxonomy leave it alone
$term_id = term_exists($term['slug'], $term['term_taxonomy']);