: str_replace(): Passing null to parameter #2 ($replace) of type array|string is deprecated in
if (is_array($term_id)) $term_id = $term_id['term_id'];
if (isset($term['term_id']))
$this->processed_terms[intval($term['term_id'])] = (int) $term_id;
if (empty($term['term_parent'])) {
$parent = term_exists($term['term_parent'], $term['term_taxonomy']);
if (is_array($parent)) $parent = $parent['term_id'];
$description = isset($term['term_description']) ? $term['term_description'] : '';
$termarr = array('slug' => $term['slug'], 'description' => $description, 'parent' => intval($parent));
$id = wp_insert_term($term['term_name'], $term['term_taxonomy'], $termarr);
if (isset($term['term_id']))
$this->processed_terms[intval($term['term_id'])] = $id['term_id'];
/* translators: term taxonomy, term name */
printf(wp_kses_post(__('Failed to import %1$s %2$s', 'popup-builder')), esc_html($term['term_taxonomy']), esc_html($term['term_name']));
if (defined('IMPORT_DEBUG') && IMPORT_DEBUG)
echo ': ' . esc_html( $id->get_error_message() );
$this->process_termmeta($term, $id['term_id']);
* Add metadata to imported term.
* @param array $term Term data from WXR import.
* @param int $term_id ID of the newly created term.
protected function process_termmeta($term, $term_id)
if (!isset($term['termmeta'])) {
$term['termmeta'] = array();
* Filters the metadata attached to an imported term.
* @param array $termmeta Array of term meta.
* @param int $term_id ID of the newly created term.
* @param array $term Term data from the WXR import.
$term['termmeta'] = apply_filters('wp_import_term_meta', $term['termmeta'], $term_id, $term);
if (empty($term['termmeta'])) {
foreach ($term['termmeta'] as $meta) {
* Filters the meta key for an imported piece of term meta.
* @param string $meta_key Meta key.
* @param int $term_id ID of the newly created term.
* @param array $term Term data from the WXR import.
$key = apply_filters('import_term_meta_key', $meta['key'], $term_id, $term);
// Export gets meta straight from the DB so could have a serialized string
$value = maybe_unserialize($meta['value']);
add_term_meta($term_id, $key, $value);
* Fires after term meta is imported.
* @param int $term_id ID of the newly created term.
* @param string $key Meta key.
* @param mixed $value Meta value.
do_action('import_term_meta', $term_id, $key, $value);
* Create new posts based on import information
* Posts marked as having a parent which doesn't exist will become top level items.
* Doesn't create a new post if: the post type doesn't exist, the given post ID
* is already noted as imported or a post with the same title and date already exists.
* Note that new/updated terms, comments and meta are imported for the last of the above.
public function process_posts()
$this->posts = apply_filters('wp_import_posts', $this->posts);
foreach ($this->posts as $post) {
$post = apply_filters('wp_import_post_data_raw', $post);
if (!post_type_exists($post['post_type'])) {
/* translators: post title, post type */
printf(wp_kses_post(__('Failed to import “%1$s”: Invalid post type %2$s', 'popup-builder')),
esc_html($post['post_title']), esc_html($post['post_type']));
do_action('wp_import_post_exists', $post);
if (isset($this->processed_posts[$post['post_id']]) && !empty($post['post_id'])) {
if ($post['status'] == 'auto-draft') {
if ('nav_menu_item' == $post['post_type']) {
$this->process_menu_item($post);
$post_type_object = get_post_type_object($post['post_type']);
$post_exists = post_exists($post['post_title'], '', $post['post_date']);
* Filter ID of the existing post corresponding to post currently importing.
* Return 0 to force the post to be imported. Filter the ID to be something else
* to override which existing post is mapped to the imported post.
* @param int $post_exists Post ID, or 0 if post did not exist.
* @param array $post The post array to be inserted.
$post_exists = apply_filters('wp_import_existing_post', $post_exists, $post);
if ($post_exists && get_post_type($post_exists) == $post['post_type']) {
/* translators: singular name ,post title */
printf(wp_kses_post(__('%1$s “%2$s” already exists.', 'popup-builder')), esc_html( $post_type_object->labels->singular_name ), esc_html($post['post_title']));
$comment_post_ID = $post_id = $post_exists;
$this->processed_posts[ intval($post['post_id']) ] = intval($post_exists);
$post_parent = (int) $post['post_parent'];
// if we already know the parent, map it to the new local ID
if (isset($this->processed_posts[$post_parent])) {
$post_parent = $this->processed_posts[$post_parent];
// otherwise record the parent for later
$this->post_orphans[intval($post['post_id'])] = $post_parent;
$author = sanitize_user($post['post_author'], true);
if (isset($this->author_mapping[$author])) {
$author = $this->author_mapping[$author];
$author = (int) get_current_user_id();
'import_id' => $post['post_id'], 'post_author' => $author, 'post_date' => $post['post_date'],
'post_date_gmt' => $post['post_date_gmt'], 'post_content' => $post['post_content'],
'post_excerpt' => $post['post_excerpt'], 'post_title' => $post['post_title'],
'post_status' => $post['status'], 'post_name' => $post['post_name'],
'comment_status' => $post['comment_status'], 'ping_status' => $post['ping_status'],
'guid' => $post['guid'], 'post_parent' => $post_parent, 'menu_order' => $post['menu_order'],
'post_type' => $post['post_type'], 'post_password' => $post['post_password']
$original_post_ID = $post['post_id'];
$postdata = apply_filters('wp_import_post_data_processed', $postdata, $post);
$postdata = wp_slash($postdata);
if ('attachment' == $postdata['post_type']) {
$remote_url = !empty($post['attachment_url']) ? $post['attachment_url'] : $post['guid'];
// try to use _wp_attached file for upload folder placement to ensure the same location as the export site
// e.g. location is 2003/05/image.jpg but the attachment post_date is 2010/09, see media_handle_upload()
$postdata['upload_date'] = $post['post_date'];
if (isset($post['postmeta'])) {
foreach($post['postmeta'] as $meta) {
if ($meta['key'] == '_wp_attached_file') {
if (preg_match('%^[0-9]{4}/[0-9]{2}%', $meta['value'], $matches))
$postdata['upload_date'] = $matches[0];
$comment_post_ID = $post_id = $this->process_attachment($postdata, $remote_url);
$comment_post_ID = $post_id = wp_insert_post($postdata, true);
do_action('wp_import_insert_post', $post_id, $original_post_ID, $postdata, $post);
if (is_wp_error($post_id)) {
/* translators: singular name ,post title */
printf(wp_kses_post(__('Failed to import %1$s “%2$s”', 'popup-builder')),
esc_html($post_type_object->labels->singular_name), esc_html($post['post_title']));
if (defined('IMPORT_DEBUG') && IMPORT_DEBUG)
echo ': ' . esc_html( $post_id->get_error_message() );
if ($post['is_sticky'] == 1) {
// map pre-import ID to local ID
$this->processed_posts[intval($post['post_id'])] = (int) $post_id;
if (!isset($post['terms']))
$post['terms'] = array();
$post['terms'] = apply_filters('wp_import_post_terms', $post['terms'], $post_id, $post);
// add categories, tags and other terms
if (!empty($post['terms'])) {
foreach ($post['terms'] as $term) {
// back compat with WXR 1.0 map 'tag' to 'post_tag'
$taxonomy = ('tag' == $term['domain']) ? 'post_tag' : $term['domain'];
$term_exists = term_exists($term['slug'], $taxonomy);
$term_id = is_array($term_exists) ? $term_exists['term_id'] : $term_exists;
$t = wp_insert_term($term['name'], $taxonomy, array('slug' => $term['slug']));
$term_id = $t['term_id'];
do_action('wp_import_insert_term', $t, $term, $post_id, $post);
/* translators: taxonomy type , taxonomy name */
printf(wp_kses_post(__('Failed to import %1$s %2$s', 'popup-builder')), esc_html($taxonomy), esc_html($term['name']));
if (defined('IMPORT_DEBUG') && IMPORT_DEBUG)
echo ': ' . esc_html( $t->get_error_message() );
do_action('wp_import_insert_term_failed', $t, $term, $post_id, $post);
$terms_to_set[$taxonomy][] = intval($term_id);
foreach ($terms_to_set as $tax => $ids) {
$tt_ids = wp_set_post_terms($post_id, $ids, $tax);
do_action('wp_import_set_post_terms', $tt_ids, $ids, $tax, $post_id, $post);
unset($post['terms'], $terms_to_set);
if (!isset($post['comments']))
$post['comments'] = array();
$post['comments'] = apply_filters('wp_import_post_comments', $post['comments'], $post_id, $post);
if (!empty($post['comments'])) {
$inserted_comments = array();
foreach ($post['comments'] as $comment) {
$comment_id = $comment['comment_id'];
$newcomments[$comment_id]['comment_post_ID'] = $comment_post_ID;
$newcomments[$comment_id]['comment_author'] = $comment['comment_author'];
$newcomments[$comment_id]['comment_author_email'] = $comment['comment_author_email'];
$newcomments[$comment_id]['comment_author_IP'] = $comment['comment_author_IP'];
$newcomments[$comment_id]['comment_author_url'] = $comment['comment_author_url'];
$newcomments[$comment_id]['comment_date'] = $comment['comment_date'];
$newcomments[$comment_id]['comment_date_gmt'] = $comment['comment_date_gmt'];
$newcomments[$comment_id]['comment_content'] = $comment['comment_content'];
$newcomments[$comment_id]['comment_approved'] = $comment['comment_approved'];
$newcomments[$comment_id]['comment_type'] = $comment['comment_type'];
$newcomments[$comment_id]['comment_parent'] = $comment['comment_parent'];
$newcomments[$comment_id]['commentmeta'] = isset($comment['commentmeta']) ? $comment['commentmeta'] : array();
if (isset($this->processed_authors[$comment['comment_user_id']]))
$newcomments[$comment_id]['user_id'] = $this->processed_authors[$comment['comment_user_id']];
foreach ($newcomments as $key => $comment) {
// if this is a new post we can skip the comment_exists() check
if (!$post_exists || !comment_exists($comment['comment_author'], $comment['comment_date'])) {
if (isset($inserted_comments[$comment['comment_parent']]))
$comment['comment_parent'] = $inserted_comments[$comment['comment_parent']];
$comment = wp_slash($comment);
$comment = wp_filter_comment($comment);
$inserted_comments[$key] = wp_insert_comment($comment);
do_action('wp_import_insert_comment', $inserted_comments[$key], $comment, $comment_post_ID, $post);
foreach($comment['commentmeta'] as $meta) {
$value = maybe_unserialize($meta['value']);
add_comment_meta($inserted_comments[$key], $meta['key'], $value);
unset($newcomments, $inserted_comments, $post['comments']);
if (!isset($post['postmeta']))
$post['postmeta'] = array();
$post['postmeta'] = apply_filters('wp_import_post_meta', $post['postmeta'], $post_id, $post);
if (!empty($post['postmeta'])) {
foreach ($post['postmeta'] as $meta) {
$key = apply_filters('import_post_meta_key', $meta['key'], $post_id, $post);
if ('_edit_last' == $key) {
if (isset($this->processed_authors[intval($meta['value'])]))
$value = $this->processed_authors[intval($meta['value'])];
// export gets meta straight from the DB so could have a serialized string
$value = maybe_unserialize($meta['value']);
add_post_meta($post_id, $key, $value);
do_action('import_post_meta', $post_id, $key, $value);
// if the post has a featured image, take note of this in case of remap
if ('_thumbnail_id' == $key)
$this->featured_images[$post_id] = (int) $value;
* Attempt to create a new menu item from import data
* Fails for draft, orphaned menu items and those without an associated nav_menu
* or an invalid nav_menu term. If the post type or term object which the menu item
* represents doesn't exist then the menu item will not be imported (waits until the
* end of the import to retry again before discarding).
* @param array $item Menu item details from WXR file
public function process_menu_item($item)
// skip draft, orphaned menu items
if ('draft' == $item['status'])
if (isset($item['terms'])) {
// loop through terms, assume first nav_menu term is correct menu
foreach ($item['terms'] as $term) {
if ('nav_menu' == $term['domain']) {
$menu_slug = $term['slug'];
// no nav_menu term associated with this menu item
esc_html_e('Menu item skipped due to missing menu slug', 'popup-builder');
$menu_id = term_exists($menu_slug, 'nav_menu');
/* translators: menu slug */
printf(wp_kses_post(__('Menu item skipped due to invalid menu slug: %s', 'popup-builder')), esc_html($menu_slug));
$menu_id = is_array($menu_id) ? $menu_id['term_id'] : $menu_id;
foreach ($item['postmeta'] as $meta) {
${$meta['key']} = $meta['value'];
if ('taxonomy' == $_menu_item_type && isset( $_menu_item_object_id ) && isset($this->processed_terms[intval($_menu_item_object_id)])) {
$_menu_item_object_id = $this->processed_terms[intval($_menu_item_object_id)];
else if ('post_type' == $_menu_item_type && isset( $_menu_item_object_id ) && isset($this->processed_posts[intval($_menu_item_object_id)])) {
$_menu_item_object_id = $this->processed_posts[intval($_menu_item_object_id)];
else if ('custom' != $_menu_item_type) {
// associated object is missing or not imported yet, we'll retry later
$this->missing_menu_items[] = $item;
if (isset($this->processed_menu_items[intval($_menu_item_menu_item_parent)])) {
$_menu_item_menu_item_parent = $this->processed_menu_items[intval($_menu_item_menu_item_parent)];
else if ($_menu_item_menu_item_parent) {
$this->menu_item_orphans[intval($item['post_id'])] = (int) $_menu_item_menu_item_parent;
$_menu_item_menu_item_parent = 0;
// wp_update_nav_menu_item expects CSS classes as a space separated string
$_menu_item_classes = maybe_unserialize($_menu_item_classes);
if (is_array($_menu_item_classes)) {
$_menu_item_classes = implode(' ', $_menu_item_classes);
'menu-item-object-id' => $_menu_item_object_id,
'menu-item-object' => $_menu_item_object,
'menu-item-parent-id' => $_menu_item_menu_item_parent,
'menu-item-position' => intval($item['menu_order']),
'menu-item-type' => $_menu_item_type,
'menu-item-title' => $item['post_title'],
'menu-item-url' => $_menu_item_url,
'menu-item-description' => $item['post_content'],
'menu-item-attr-title' => $item['post_excerpt'],
'menu-item-target' => $_menu_item_target,
'menu-item-classes' => $_menu_item_classes,
'menu-item-xfn' => $_menu_item_xfn,
'menu-item-status' => $item['status']
$id = wp_update_nav_menu_item($menu_id, 0, $args);
if ($id && !is_wp_error($id)) {
$this->processed_menu_items[intval($item['post_id'])] = (int) $id;
* If fetching attachments is enabled then attempt to create a new attachment
* @param array $post Attachment post details from WXR
* @param string $url URL to fetch attachment from
* @return int|WP_Error Post ID on success, WP_Error otherwise
public function process_attachment($post, $url)
if (!$this->fetch_attachments) {
return new \WP_Error('attachment_processing_error',
__('Fetching attachments is not enabled', 'popup-builder'));
// if the URL is absolute, but does not contain address, then upload it assuming base_site_url
if (preg_match('|^/[\w\W]+$|', $url))
$url = rtrim($this->base_url, '/') . $url;
$upload = $this->fetch_remote_file($url, $post);
if (is_wp_error($upload)) {
if ($info = wp_check_filetype($upload['file'])) {
$post['post_mime_type'] = $info['type'];
return new \WP_Error('attachment_processing_error', __('Invalid file type', 'popup-builder'));
$post['guid'] = $upload['url'];
// as per wp-admin/includes/upload.php
$post_id = wp_insert_attachment($post, $upload['file']);
wp_update_attachment_metadata($post_id, wp_generate_attachment_metadata($post_id, $upload['file']));
// remap resized image URLs, works by stripping the extension and remapping the URL stub.
if (preg_match('!^image/!', $info['type'])) {
$name = basename($parts['basename'], ".{$parts['extension']}"); // PATHINFO_FILENAME in PHP 5.2
$parts_new = pathinfo($upload['url']);
$name_new = basename($parts_new['basename'], ".{$parts_new['extension']}");
$this->url_remap[$parts['dirname'] . '/' . $name] = $parts_new['dirname'] . '/' . $name_new;