From 6d56a2c092695e4e1cc07b6ba02f8cd182cee275 Mon Sep 17 00:00:00 2001 From: helgatheviking <507025+helgatheviking@users.noreply.github.com> Date: Sat, 29 Jan 2022 14:10:02 -0700 Subject: [PATCH 1/5] Import custom meta for nav menu items. Closes #77. --- src/class-wp-import.php | 45 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) diff --git a/src/class-wp-import.php b/src/class-wp-import.php index 6d994aa..326f7e3 100644 --- a/src/class-wp-import.php +++ b/src/class-wp-import.php @@ -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 ) ] ) ) { @@ -996,6 +1018,27 @@ 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 ); + + } + } } } From c0b3724e9263ce5620148de1ad983589188db262 Mon Sep 17 00:00:00 2001 From: Dion Hulse Date: Tue, 19 Jul 2022 16:19:30 +1000 Subject: [PATCH 2/5] Resolve PHPCS linting issues --- src/class-wp-import.php | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/src/class-wp-import.php b/src/class-wp-import.php index 326f7e3..3d4db86 100644 --- a/src/class-wp-import.php +++ b/src/class-wp-import.php @@ -950,16 +950,16 @@ 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' => '' - ); + $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(); @@ -1022,7 +1022,7 @@ function process_menu_item( $item ) { $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 ); + $key = apply_filters( 'import_nav_menu_meta_key', $key, $id, $args ); // Skip old date meta. if ( '_wp_old_date' === $key ) { From a226ab43233eef3ce857e8b6be753c6a22cf40a5 Mon Sep 17 00:00:00 2001 From: Dion Hulse Date: Tue, 19 Jul 2022 16:20:43 +1000 Subject: [PATCH 3/5] Resolve indentation issues --- src/class-wp-import.php | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/src/class-wp-import.php b/src/class-wp-import.php index 3d4db86..adfd5b6 100644 --- a/src/class-wp-import.php +++ b/src/class-wp-import.php @@ -1021,24 +1021,23 @@ function process_menu_item( $item ) { $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 ); + 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; - } + // 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 ) ); + if ( $key ) { + // Export gets meta straight from the DB so could have a serialized string. + $value = maybe_unserialize( $value ); - do_action( 'import_nav_menu_meta', $id, $key, $value ); + add_post_meta( $id, wp_slash( $key ), wp_slash( $value ) ); - } - } + do_action( 'import_nav_menu_meta', $id, $key, $value ); + } + } } } From 6a2eb30b88f2e26b2546409c6ce7ae80cad9c285 Mon Sep 17 00:00:00 2001 From: Dion Hulse Date: Tue, 19 Jul 2022 16:21:39 +1000 Subject: [PATCH 4/5] Resolve indentation issues. --- src/class-wp-import.php | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/class-wp-import.php b/src/class-wp-import.php index adfd5b6..43fb78f 100644 --- a/src/class-wp-import.php +++ b/src/class-wp-import.php @@ -961,18 +961,18 @@ function process_menu_item( $item ) { '_menu_item_xfn' => '', ); - $custom_meta = array(); + $custom_meta = array(); - // Parse meta items into core fields and any custom fields. + // Parse meta items into core fields and any custom fields. foreach ( $item['postmeta'] as $meta ) { - 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 ) { + 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; } From b35eb7959f15efb888b6d01d14fb698ca1489a1f Mon Sep 17 00:00:00 2001 From: Dion Hulse Date: Tue, 19 Jul 2022 16:24:22 +1000 Subject: [PATCH 5/5] Resolve PHPCS issues. --- src/class-wp-import.php | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/class-wp-import.php b/src/class-wp-import.php index 43fb78f..28a8554 100644 --- a/src/class-wp-import.php +++ b/src/class-wp-import.php @@ -951,14 +951,14 @@ function process_menu_item( $item ) { } $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' => '', + '_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(); @@ -966,9 +966,9 @@ function process_menu_item( $item ) { // Parse meta items into core fields and any custom fields. foreach ( $item['postmeta'] as $meta ) { if ( array_key_exists( $meta['key'], $core_meta ) ) { - $core_meta[$meta['key']] = $meta['value']; + $core_meta[ $meta['key'] ] = $meta['value']; } else { - $custom_meta[$meta['key']] = $meta['value']; + $custom_meta[ $meta['key'] ] = $meta['value']; } }