From c2e291352e667ce0ce88b4bcdf81c318223feb24 Mon Sep 17 00:00:00 2001 From: David Cavins Date: Thu, 22 Jun 2017 15:04:44 -0500 Subject: [PATCH] Add term post processing routine. If the WXR reader encounters a child term before its parent term, it cannot set the child/parent relationship at the time the child term is created. In this case, we save the parent term slug as meta on the child term. Then, after all terms are inserted, loop through and update the parent/child relationships as needed . --- class-wxr-importer.php | 63 ++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 61 insertions(+), 2 deletions(-) diff --git a/class-wxr-importer.php b/class-wxr-importer.php index 3a4ae27..9a5f637 100644 --- a/class-wxr-importer.php +++ b/class-wxr-importer.php @@ -1696,8 +1696,8 @@ protected function process_term( $data, $meta ) { } else if ( $parent_id = $this->term_exists( array( 'taxonomy' => $data['taxonomy'], 'slug' => $data['parent'] ) ) ) { $data['parent'] = $parent_id; } else { - // Prepare for remapping later - $meta[] = array( 'key' => '_wxr_import_parent', 'value' => $parent_id ); + // Prepare for remapping later, using the slug. + $meta[] = array( 'key' => '_wxr_import_parent', 'value' => $data['parent'] ); $requires_remapping = true; // Wipe the parent for now @@ -1743,6 +1743,14 @@ protected function process_term( $data, $meta ) { // Add the new term to the exists array. $this->exists['term'][ $mapping_key ] = $term_id; + // Add the termmeta if necessary. + if ( $requires_remapping ) { + foreach ( $meta as $insert) { + update_term_meta( $term_id, $insert['key'], $insert['value'] ); + } + $this->requires_remapping['term'][ $term_id ] = true; + } + $this->logger->info( sprintf( __( 'Imported "%s" (%s)', 'wordpress-importer' ), $data['name'], @@ -1841,6 +1849,9 @@ protected function post_process() { if ( ! empty( $this->requires_remapping['comment'] ) ) { $this->post_process_comments( $this->requires_remapping['comment'] ); } + if ( ! empty( $this->requires_remapping['term'] ) ) { + $this->post_process_terms( $this->requires_remapping['term'] ); + } } protected function post_process_posts( $todo ) { @@ -2045,6 +2056,54 @@ protected function post_process_comments( $todo ) { } } + protected function post_process_terms( $todo ) { + foreach ( $todo as $term_id => $_ ) { + $data = array(); + + $parent_term_slug = get_term_meta( $term_id, '_wxr_import_parent', true ); + if ( ! empty( $parent_term_slug ) ) { + $term = get_term( (int) $term_id ); + $parent_mapping_key = sha1( $term->taxonomy . ':' . $parent_term_slug ); + + // Have we imported the parent now? + if ( isset( $this->mapping['term'][ $parent_mapping_key ] ) ) { + $data['parent'] = $this->mapping['term'][ $parent_mapping_key ]; + // Next, see if the term already exists. + } else if ( $parent_id = $this->term_exists( array( 'taxonomy' => $term->taxonomy, 'slug' => $parent_term_slug ) ) ) { + $data['parent'] = $parent_id; + } else { + $this->logger->warning( sprintf( + __( 'Could not find the parent for term #%d', 'wordpress-importer' ), + $term_id + ) ); + $this->logger->debug( sprintf( + __( 'Term %d was imported with parent %S, but the parent term could not be found', 'wordpress-importer' ), + $term_id, + $parent_term_slug + ) ); + } + } + + // Do we have updates to make? + if ( empty( $data ) ) { + continue; + } + + // Run the update + $result = wp_update_term( $term_id, $term->taxonomy, $data ); + if ( empty( $result ) ) { + $this->logger->warning( sprintf( + __( 'Could not update term #%d with mapped data', 'wordpress-importer' ), + $term_id + ) ); + continue; + } + + // Clear out our temporary meta keys + delete_term_meta( $term_id, '_wxr_import_parent' ); + } + } + /** * Use stored mapping information to update old attachment URLs */