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

remap post-parent for already existing post (attachments) #167

Open
wants to merge 1 commit 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
28 changes: 27 additions & 1 deletion class-wxr-importer.php
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,7 @@ public function parse_authors( $file ) {
public function import( $file ) {
add_filter( 'import_post_meta_key', array( $this, 'is_valid_meta_key' ) );
add_filter( 'http_request_timeout', array( &$this, 'bump_request_timeout' ) );
add_action( 'wxr_importer.process_already_imported.post', array( &$this, 'parent_already_existing_attachments' ), 10, 2 );

$result = $this->import_start( $file );
if ( is_wp_error( $result ) ) {
Expand Down Expand Up @@ -714,6 +715,30 @@ protected function parse_post_node( $node ) {
return compact( 'data', 'meta', 'comments', 'terms' );
}

/**
* Fired by wxr_importer.process_already_imported.post
* Remap post_parent for attachment that already existing
* Without this, existing attachment (having lost their parent because of deletion/re-import (post-parent set to 0)
* would have their processing skipped during subsequent imports and never get their parent-id correctly updated anymore.
*
* This function expand what process_post() does by testing $parent_id + call to process_attachment() and process_post_meta()
*/
function parent_already_existing_attachments( $data, $post_exists ) {
$post_id = $post_exists;
$parent_id = isset( $data['post_parent'] ) ? (int) $data['post_parent'] : 0;
if ( $parent_id ) {
if ( isset( $this->mapping['post'][ $parent_id ] ) ) {
wp_update_post( ['ID' => $post_id, 'post_parent' => $this->mapping['post'][ $parent_id ] ] );
} else {
$post = get_post($post_id);
$this->process_post_meta( [ ['key' => '_wxr_import_parent', 'value' => $parent_id ] ], $post_id, $post );
wp_update_post( ['ID' => $post_id, 'post_parent' => 0 ] );
$this->requires_remapping['post'][ $post_id ] = true;
}
$this->mark_post_exists( $data, $post_id );
}
}

/**
* Create new posts based on import information
*
Expand Down Expand Up @@ -769,8 +794,9 @@ protected function process_post( $data, $meta, $comments, $terms ) {
* Post processing already imported.
*
* @param array $data Raw data imported for the post.
* @param int $post_exists Already existing post id
*/
do_action( 'wxr_importer.process_already_imported.post', $data );
do_action( 'wxr_importer.process_already_imported.post', $data, $post_exists );

// Even though this post already exists, new comments might need importing
$this->process_comments( $comments, $original_id, $data, $post_exists );
Expand Down