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

Import custom meta for nav menu items. #119

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 43 additions & 1 deletion src/class-wp-import.php
Original file line number Diff line number Diff line change
Expand Up @@ -950,8 +950,30 @@ function process_menu_item( $item ) {
$menu_id = is_array( $menu_id ) ? $menu_id['term_id'] : $menu_id;
}

$core_meta = array(
'_menu_item_object_id' => '',
'_menu_item_object' => '',
'_menu_item_menu_item_parent' => '',
'_menu_item_type' => '',
'_menu_item_url' => '',
'_menu_item_target' => '',
'_menu_item_classes' => '',
'_menu_item_xfn' => '',
);

$custom_meta = array();

// Parse meta items into core fields and any custom fields.
foreach ( $item['postmeta'] as $meta ) {
${$meta['key']} = $meta['value'];
if ( array_key_exists( $meta['key'], $core_meta ) ) {
$core_meta[ $meta['key'] ] = $meta['value'];
} else {
$custom_meta[ $meta['key'] ] = $meta['value'];
}
}

foreach ( $core_meta as $key => $value ) {
${$key} = $value;
}

if ( 'taxonomy' == $_menu_item_type && isset( $this->processed_terms[ intval( $_menu_item_object_id ) ] ) ) {
Expand Down Expand Up @@ -996,6 +1018,26 @@ function process_menu_item( $item ) {
$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;

$custom_meta = apply_filters( 'wp_import_nav_menu_meta', $custom_meta, $id, $args );

foreach ( $custom_meta as $key => $value ) {
$key = apply_filters( 'import_nav_menu_meta_key', $key, $id, $args );

// Skip old date meta.
if ( '_wp_old_date' === $key ) {
$key = false;
}

if ( $key ) {
// Export gets meta straight from the DB so could have a serialized string.
$value = maybe_unserialize( $value );

add_post_meta( $id, wp_slash( $key ), wp_slash( $value ) );

do_action( 'import_nav_menu_meta', $id, $key, $value );
}
}
}
}

Expand Down