Make WordPress Core

Opened 11 years ago

Last modified 3 years ago

#23276 new defect (bug)

WordPress Importer: Update existing navs instead of new

Reported by: wraithkenny's profile WraithKenny Owned by:
Milestone: WordPress.org Priority: normal
Severity: normal Version:
Component: Import Keywords: needs-refresh needs-testing
Focuses: Cc:

Description

A possible solution (reuses logic present in the code):

$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 );

Attachments (1)

23276.patch (2.6 KB) - added by WraithKenny 11 years ago.

Download all attachments as: .zip

Change History (8)

#1 @johnbillion
11 years ago

  • Keywords reporter-feedback added

I'll mirror what I just said on #23275, it would be great to get this as a patch or at least get a pointer to the file and line, and some more info about the bug and what your fix achieves. Cheers!

@WraithKenny
11 years ago

#2 @WraithKenny
11 years ago

  • Keywords reporter-feedback removed

The issue:

The current version of the plugin imports nav items using wp_update_nav_menu_item( $menu_id, 0, $args ) which because of the second passed argument always creates a new item, even when the item exists already (actually, even when it doesn't, it creates a new one, but with a new id).

The patch just uses the logic that all other post_types uses when importing to either create the item with the correct id, or update the existing item with the correct id (if the existing item is a nav item).

#3 @SergeyBiryukov
11 years ago

  • Keywords has-patch added
  • Milestone changed from Awaiting Review to WordPress.org
  • Version trunk deleted

#4 @chriscct7
9 years ago

  • Keywords needs-refresh needs-testing added; has-patch removed

#5 @RavanH
7 years ago

So... seems the importer still causes duplicate nav_menu_item creation upon each import. Has it never been patched?

#6 @ocean90
6 years ago

#17191 was marked as a duplicate.

#7 @ocean90
3 years ago

#51433 was marked as a duplicate.

Note: See TracTickets for help on using tickets.