Skip to content
Open
52 changes: 52 additions & 0 deletions src/class-wp-import.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class WP_Import extends WP_Importer {
public $author_mapping = array();
public $processed_terms = array();
public $processed_posts = array();
public $processed_comments = array();
public $post_orphans = array();
public $processed_menu_items = array();
public $menu_item_orphans = array();
Expand Down Expand Up @@ -818,6 +819,7 @@ public function process_posts() {
if ( ! empty( $post['comments'] ) ) {
$this->process_post_comments( $post['comments'], (bool) $post_exists, $comment_post_id, $post );
unset( $post['comments'] );
$this->update_block_note_ids( $post_id );
}

if ( ! isset( $post['postmeta'] ) ) {
Expand Down Expand Up @@ -1095,6 +1097,10 @@ protected function process_post_comments( $comments, $post_exists, $comment_post
do_action( 'wp_import_insert_comment', $inserted_comment_id, $comment, $comment_post_id, $post );
$this->process_post_comment_metas( $inserted_comment_id, $comment['commentmeta'] );
$inserted_comments[ $key ] = $inserted_comment_id;
// Store comment ID mapping for note-type comments to update noteId references in blocks.
if ( isset( $comment['comment_type'] ) && 'note' === $comment['comment_type'] ) {
$this->processed_comments[ $key ] = $inserted_comment_id;
}
++$num_comments;
}
}
Expand Down Expand Up @@ -1139,6 +1145,52 @@ protected function process_post_comment_metas( $comment_id, $commentmeta ) {
}
}

/**
* Update noteId references in block metadata to reflect new note-type comment IDs.
*
* Scans post content for Gutenberg blocks with noteId in metadata and updates
* them to match the new comment IDs assigned during import. Only processes
* comments with type 'note'.
*
* @param int $post_id The ID of the post being processed.
* @return void
*/
protected function update_block_note_ids( int $post_id = 0 ): void {
if ( empty( $this->processed_comments ) ) {
return;
}

$post = get_post( $post_id );

if ( ! $post ) {
return;
}

$content = $post->post_content;
$updated = false;

foreach ( $this->processed_comments as $old_note_id => $new_note_id ) {
$search = '"noteId":' . $old_note_id;
$replace = '"noteId":' . $new_note_id;

$new_content = str_replace( $search, $replace, $content );
Comment thread
hbhalodia marked this conversation as resolved.
Outdated

if ( $new_content !== $content ) {
$content = $new_content;
$updated = true;
}
}

if ( $updated ) {
wp_update_post(
array(
'ID' => $post_id,
'post_content' => $content,
)
);
}
Comment thread
dmsnell marked this conversation as resolved.
}

/**
* Process a single comment meta entry for an imported comment.
*
Expand Down
Loading