WordPress.org

Ready to get started?Download WordPress

Forums

WordPress Importer
Line-ending issue breaks metadata import (8 posts)

  1. WraithKenny
    Member
    Posted 1 year ago #

    The fix that worked for me at L703:

    // export gets meta straight from the DB so could have a serialized string
    if ( ! $value )
    	$value = maybe_unserialize( str_replace( array("\r\n", "\r", "\n"), PHP_EOL, $meta['value'] ) );

    http://wordpress.org/extend/plugins/wordpress-importer/

  2. WraithKenny
    Member
    Posted 1 year ago #

    Background: I traced an failed import of metadata to that line, the unserialize function was choking on something. Since I'm a very slow guy, it had me stumped for about a full day. I did lots of testing before I figured out it was choking on the line endings. str_replace is used to normalize the line-endings, which fixed the issue for me. My testing was done in xampp (apache/php on win) and the import was from a unix server.

  3. WraithKenny
    Member
    Posted 1 year ago #

    actually, it needs $value = maybe_unserialize( str_replace( array("\r\n", "\r", "\n"), "\r\n", $meta['value'] ) );

  4. WraithKenny
    Member
    Posted 1 year ago #

    There's an additional problem with meta importing. Some plugins require their meta to be unique, and use get_post_meta as unique. The importer just vanilla imports the meta using add_post_meta without the unique flag...which breaks these plugins. It's difficult to solve since some meta isn't unique, but there's no way to tell the difference. The entire algorithm will have to be reconsidered to fix that bug.

  5. WraithKenny
    Member
    Posted 1 year ago #

    Actually, my line-ending fix doesn't work for cases where one string in the serialized array has a line-ending type different from another.

  6. WraithKenny
    Member
    Posted 1 year ago #

    For fixing the duplicate menu items:

    $menu_item_db_id = (int) $item['post_id'];
    $original_object = get_post( $menu_item_db_id );
    if ( is_null( $original_object ) )  {
    
    	$post_parent = (int) $item['post_parent'];
    	if ( $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
    		} else {
    			$this->post_orphans[intval($post['post_id'])] = $post_parent;
    			$post_parent = 0;
    		}
    	}
    
    	// map the post author
    	$author = sanitize_user( $item['post_author'], true );
    	if ( isset( $this->author_mapping[$author] ) )
    		$author = $this->author_mapping[$author];
    	else
    		$author = (int) get_current_user_id();
    
    	$postdata = array(
    		'import_id' => $item['post_id'],
    		'post_author' => $author,
    		'post_date' => $item['post_date'],
    		'post_date_gmt' => $item['post_date_gmt'],
    		'post_content' => $item['post_content'],
    		'post_excerpt' => $item['post_excerpt'],
    		'post_title' => $item['post_title'],
    		'post_status' => $item['status'],
    		'post_name' => $item['post_name'],
    		'comment_status' => $item['comment_status'],
    		'ping_status' => $item['ping_status'],
    		'guid' => $item['guid'],
    		'post_parent' => $post_parent,
    		'menu_order' => $item['menu_order'],
    		'post_type' => $item['post_type'],
    		'post_password' => $item['post_password']
    	);
    	$menu_item_db_id = wp_insert_post( $postdata, true );
    
    	if ( is_wp_error( $menu_item_db_id ) ) {
    		$post_type_object = get_post_type_object( $item['post_type'] );
    		printf( __( 'Failed to import %s ā€œ%sā€', 'wordpress-importer' ),
    			$post_type_object->labels->singular_name, esc_html($item['post_title']) );
    		if ( defined('IMPORT_DEBUG') && IMPORT_DEBUG )
    			echo ': ' . $post_id->get_error_message();
    		echo '<br />';
    	}
    }
    
    $id = wp_update_nav_menu_item( $menu_id, $menu_item_db_id, $args );
  7. WraithKenny
    Member
    Posted 1 year ago #

    for fixing the metadata

    if ( $key ) {
    	// export gets meta straight from the DB so could have a serialized string
    	if ( ! $value )
    		$value = maybe_unserialize( $meta['value'] );
    	if ( empty( $value ) )
    		$value = maybe_unserialize( str_replace( array("\r\n", "\r", "\n"), "\r\n", $meta['value'] ) );
    	if ( empty( $value ) )
    		$value = maybe_unserialize(
    			preg_replace( // deprecated in 5.5.0
    				'!s:(\d+):"(.*?)";!se',
    				"'s:'.strlen('$2').':\"$2\";'",
    				$meta['value']
    			)
    		);
    	/* The following edit is not recommended for general use */
    	//delete_post_meta( $post_id, $key );
    	//if ( ! empty( $value ) )
    	//	update_post_meta( $post_id, $key, $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;
    }
  8. WraithKenny
    Member
    Posted 1 year ago #

    The problem described by the "post_meta" issue should probably be addressed by each plugin/theme via the 'import_post_meta' hook.

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic

Tags