Skip to content

Commit

Permalink
Add term post processing routine.
Browse files Browse the repository at this point in the history
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 .
  • Loading branch information
dcavins committed Jun 22, 2017
1 parent 21cd427 commit c2e2913
Showing 1 changed file with 61 additions and 2 deletions.
63 changes: 61 additions & 2 deletions class-wxr-importer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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'],
Expand Down Expand Up @@ -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 ) {
Expand Down Expand Up @@ -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
*/
Expand Down

0 comments on commit c2e2913

Please sign in to comment.