Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Request code change to import custom menu fields added by plugins #84

Open
BackuPs opened this issue Oct 20, 2020 · 4 comments
Open

Request code change to import custom menu fields added by plugins #84

BackuPs opened this issue Oct 20, 2020 · 4 comments

Comments

@BackuPs
Copy link

BackuPs commented Oct 20, 2020

No description provided.

@BackuPs
Copy link
Author

BackuPs commented Oct 20, 2020

Hi

Could you please change the function process_menu_item($item) to this so custom menu fields added by plugins or themes also get imported. It is a easy fix for a missing piece of code that has been asked for several years.
`

function process_menu_item( $item ) {
	// skip draft, orphaned menu items
	if ( 'draft' == $item['status'] )
		return;

	$menu_slug = false;
	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'];
				break;
			}
		}
	}

	// no nav_menu term associated with this menu item
	if ( ! $menu_slug ) {
		_e( 'Menu item skipped due to missing menu slug', 'wordpress-importer' );
		echo '<br />';
		return;
	}

	$menu_id = term_exists( $menu_slug, 'nav_menu' );
	if ( ! $menu_id ) {
		printf( __( 'Menu item skipped due to invalid menu slug: %s', 'wordpress-importer' ), esc_html( $menu_slug ) );
		echo '<br />';
		return;
	} else {
		$menu_id = is_array( $menu_id ) ? $menu_id['term_id'] : $menu_id;
	}

	$backup_menu_item_meta=array();
	$backup_menu_item_meta['postmeta']= $item['postmeta'];

	foreach ( $item['postmeta'] as $meta )
		${$meta['key']} = $meta['value'];

	if ( 'taxonomy' == $_menu_item_type && 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( $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;
		return;
	}

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

	$args = array(
		'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 ) )  {
		$menu_item_db_id=$id;
		$backup_menu_item_meta['postmeta']= apply_filters('wordpress_importer_menu_items_meta_import',$backup_menu_item_meta['postmeta'],$id);
		$skip_meta_items= array('_menu_item_type','_menu_item_menu_item_parent','_menu_item_object_id','_menu_item_object','_menu_item_target','_menu_item_classes','_menu_item_xfn','_menu_item_url');
		if (is_array($backup_menu_item_meta['postmeta'])  && !empty($backup_menu_item_meta['postmeta'])) {
			foreach ( $backup_menu_item_meta['postmeta'] as $meta ) {
				if (!in_array($meta['key'],$skip_meta_items)) {
					update_post_meta( $menu_item_db_id, $meta['key'], $meta['value']);
				}
			}
		}
		$this->processed_menu_items[intval($item['post_id'])] = (int) $id;
	}
}`

afbeelding

https://share.getcloudapp.com/Wnurkb4q?utm_source=show

I do not want to change the plugin every time to get this part to work. The code works just fine and imports the custom fields added by any plugin or theme to a menu item.

The function is found in the file called class-wp-import.php

Please confirm adaption of this code change.

@BackuPs BackuPs changed the title Rqeuest code change to import custom menu fields added by plugins Request code change to import custom menu fields added by plugins Oct 20, 2020
@jrfnl
Copy link
Member

jrfnl commented Oct 20, 2020

And that's why this is open source, you can send in a pull request.

@BackuPs
Copy link
Author

BackuPs commented Oct 20, 2020

@jrfnl Well if i add a pullrequest i get a check failed on php 7.3 which does not make any sense as it is not on the code changes i made.

#85

@dd32
Copy link
Member

dd32 commented Oct 21, 2020

Well if i add a pullrequest i get a check failed on php 7.3 which does not make any sense as it is not on the code changes i made.

It's the WordPress coding standards checks that are failing. You can see the failing rules here: https://travis-ci.com/github/WordPress/wordpress-importer/jobs/402491804 It looks like it probably just needs spaces around everything.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants