Skip to content
Open
Show file tree
Hide file tree
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
114 changes: 114 additions & 0 deletions phpunit/data/wxr-with-note-comments.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!-- This is a WordPress eXtended RSS file generated by WordPress as an export of your site. -->
<!-- It contains information about your site's posts, pages, comments, categories, and other content. -->
<!-- You may use this file to transfer that content from one site to another. -->
<!-- This file is not intended to serve as a complete backup of your site. -->

<!-- To import this information into a WordPress site follow these steps: -->
<!-- 1. Log in to that site as an administrator. -->
<!-- 2. Go to Tools: Import in the WordPress admin panel. -->
<!-- 3. Install the "WordPress" importer from the list. -->
<!-- 4. Activate & Run Importer. -->
<!-- 5. Upload this file using the form provided on that page. -->
<!-- 6. You will first be asked to map the authors in this export file to users -->
<!-- on the site. For each author, you may choose to map to an -->
<!-- existing user on the site or to create a new user. -->
<!-- 7. WordPress will then import each of the posts, pages, comments, categories, etc. -->
<!-- contained in this file into your site. -->

<!-- generator="WordPress/6.9.4" created="2026-03-24 10:39" -->
<rss version="2.0"
xmlns:excerpt="http://wordpress.org/export/1.2/excerpt/"
xmlns:content="http://purl.org/rss/1.0/modules/content/"
xmlns:wfw="http://wellformedweb.org/CommentAPI/"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:wp="http://wordpress.org/export/1.2/"
>

<channel>
<title>My WordPress Website</title>
<link>https://playground.wordpress.net/scope:creative-busy-garden</link>
<description></description>
<pubDate>Tue, 24 Mar 2026 10:39:41 +0000</pubDate>
<language>en-US</language>
<wp:wxr_version>1.2</wp:wxr_version>
<wp:base_site_url>https://playground.wordpress.net/scope:creative-busy-garden</wp:base_site_url>
<wp:base_blog_url>https://playground.wordpress.net/scope:creative-busy-garden</wp:base_blog_url>

<wp:author><wp:author_id>1</wp:author_id><wp:author_login><![CDATA[admin]]></wp:author_login><wp:author_email><![CDATA[admin@localhost.com]]></wp:author_email><wp:author_display_name><![CDATA[admin]]></wp:author_display_name><wp:author_first_name><![CDATA[]]></wp:author_first_name><wp:author_last_name><![CDATA[]]></wp:author_last_name></wp:author>
<generator>https://wordpress.org/?v=6.9.4</generator>

<item>
<title><![CDATA[Page with notes and comments]]></title>
<link>https://playground.wordpress.net/scope:creative-busy-garden/page-with-notes-and-comments/</link>
<pubDate>Tue, 24 Mar 2026 10:38:09 +0000</pubDate>
<dc:creator><![CDATA[admin]]></dc:creator>
<guid isPermaLink="false">https://playground.wordpress.net/scope:creative-busy-garden/?page_id=10</guid>
<description></description>
<content:encoded><![CDATA[<!-- wp:heading {"metadata":{"noteId":3}} -->
<h2 class="wp-block-heading">Note on heading 1</h2>
<!-- /wp:heading -->

<!-- wp:paragraph {"metadata":{"noteId":2}} -->
<p>Paragraph with note 1</p>
<!-- /wp:paragraph -->]]></content:encoded>
<excerpt:encoded><![CDATA[]]></excerpt:encoded>
<wp:post_id>10</wp:post_id>
<wp:post_date><![CDATA[2026-03-24 10:38:09]]></wp:post_date>
<wp:post_date_gmt><![CDATA[2026-03-24 10:38:09]]></wp:post_date_gmt>
<wp:post_modified><![CDATA[2026-03-24 10:38:14]]></wp:post_modified>
<wp:post_modified_gmt><![CDATA[2026-03-24 10:38:14]]></wp:post_modified_gmt>
<wp:comment_status><![CDATA[open]]></wp:comment_status>
<wp:ping_status><![CDATA[closed]]></wp:ping_status>
<wp:post_name><![CDATA[page-with-notes-and-comments]]></wp:post_name>
<wp:status><![CDATA[publish]]></wp:status>
<wp:post_parent>0</wp:post_parent>
<wp:menu_order>0</wp:menu_order>
<wp:post_type><![CDATA[page]]></wp:post_type>
<wp:post_password><![CDATA[]]></wp:post_password>
<wp:is_sticky>0</wp:is_sticky>
<wp:comment>
<wp:comment_id>2</wp:comment_id>
<wp:comment_author><![CDATA[admin]]></wp:comment_author>
<wp:comment_author_email><![CDATA[admin@localhost.com]]></wp:comment_author_email>
<wp:comment_author_url>http://127.0.0.1:34717</wp:comment_author_url>
<wp:comment_author_IP><![CDATA[127.0.0.1]]></wp:comment_author_IP>
<wp:comment_date><![CDATA[2026-03-24 10:37:45]]></wp:comment_date>
<wp:comment_date_gmt><![CDATA[2026-03-24 10:37:45]]></wp:comment_date_gmt>
<wp:comment_content><![CDATA[Note on para 1]]></wp:comment_content>
<wp:comment_approved><![CDATA[0]]></wp:comment_approved>
<wp:comment_type><![CDATA[note]]></wp:comment_type>
<wp:comment_parent>0</wp:comment_parent>
<wp:comment_user_id>1</wp:comment_user_id>
</wp:comment>
<wp:comment>
<wp:comment_id>3</wp:comment_id>
<wp:comment_author><![CDATA[admin]]></wp:comment_author>
<wp:comment_author_email><![CDATA[admin@localhost.com]]></wp:comment_author_email>
<wp:comment_author_url>http://127.0.0.1:34717</wp:comment_author_url>
<wp:comment_author_IP><![CDATA[127.0.0.1]]></wp:comment_author_IP>
<wp:comment_date><![CDATA[2026-03-24 10:38:07]]></wp:comment_date>
<wp:comment_date_gmt><![CDATA[2026-03-24 10:38:07]]></wp:comment_date_gmt>
<wp:comment_content><![CDATA[Note on heading 1]]></wp:comment_content>
<wp:comment_approved><![CDATA[0]]></wp:comment_approved>
<wp:comment_type><![CDATA[note]]></wp:comment_type>
<wp:comment_parent>0</wp:comment_parent>
<wp:comment_user_id>1</wp:comment_user_id>
</wp:comment>
<wp:comment>
<wp:comment_id>4</wp:comment_id>
<wp:comment_author><![CDATA[admin]]></wp:comment_author>
<wp:comment_author_email><![CDATA[admin@localhost.com]]></wp:comment_author_email>
<wp:comment_author_url>http://127.0.0.1:34717</wp:comment_author_url>
<wp:comment_author_IP><![CDATA[127.0.0.1]]></wp:comment_author_IP>
<wp:comment_date><![CDATA[2026-03-24 10:38:26]]></wp:comment_date>
<wp:comment_date_gmt><![CDATA[2026-03-24 10:38:26]]></wp:comment_date_gmt>
<wp:comment_content><![CDATA[Comment on post Page with notes and comments]]></wp:comment_content>
<wp:comment_approved><![CDATA[1]]></wp:comment_approved>
<wp:comment_type><![CDATA[comment]]></wp:comment_type>
<wp:comment_parent>0</wp:comment_parent>
<wp:comment_user_id>1</wp:comment_user_id>
</wp:comment>
</item>
</channel>
</rss>
37 changes: 37 additions & 0 deletions phpunit/tests/import.php
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,43 @@ public static function data_flat_attachment_import_rewrites_attachment_url() {
);
}

/**
* Test that note comment IDs are correctly remapped in block content.
*
* @covers WP_Import::update_block_note_ids
* @covers WP_Import::process_post_comments
*/
public function test_note_comment_ids_are_remapped_in_block_content() {
$authors = array(
'admin' => 1,
);
$this->_import_wp( DIR_TESTDATA_WP_IMPORTER . '/wxr-with-note-comments.xml', $authors );

$posts = get_posts(
array(
'post_type' => 'page',
'post_status' => 'publish',
'title' => 'Page with notes and comments',
)
);
$post = $posts[0];
$comments = get_comments(
array(
'post_id' => $post->ID,
'type' => 'note',
)
);

$new_comment_ids = array_map( 'intval', wp_list_pluck( $comments, 'comment_ID' ) );

$this->assertStringNotContainsString( '{"noteId":2}', $post->post_content, 'Old noteId 2 should have been remapped.' );
$this->assertStringNotContainsString( '{"noteId":3}', $post->post_content, 'Old noteId 3 should have been remapped.' );

foreach ( $new_comment_ids as $new_id ) {
$this->assertStringContainsString( '{"noteId":' . $new_id . '}', $post->post_content, "New noteId $new_id should be present in block content." );
}
}

/**
* Provides a mocked HTTP response when the importer downloads attachments.
*
Expand Down
98 changes: 98 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,98 @@ protected function process_post_comment_metas( $comment_id, $commentmeta ) {
}
}

/**
* Remaps noteId references in block metadata after note-type comments are imported.
*
* @param int $post_id ID of the post whose block content should be updated.
* @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;
}

if ( ! str_contains( $post->post_content, '"noteId"' ) ) {
return;
}

// @todo Replace with WP_HTML_Tag_Processor or WP_Block_Processor once minimum version support is 6.2 or 6.9 respectively.
$parser = new WP_Block_Parser();
$parser->document = $post->post_content;
$parser->offset = 0;
$end = strlen( $post->post_content );
$replacements = array();

while ( $parser->offset < $end ) {
$next_token = $parser->next_token();
list( $token_type, $block_name, $attrs, $start_offset, $token_length ) = $next_token;

if ( 'no-more-tokens' === $token_type ) {
break;
}

$parser->offset = $start_offset + $token_length;

if ( 'block-opener' !== $token_type && 'void-block' !== $token_type ) {
continue;
}

$old_note_id = $attrs['metadata']['noteId'] ?? null;

if (
! ( is_string( $old_note_id ) || is_int( $old_note_id ) ) ||
! isset( $this->processed_comments[ $old_note_id ] )
) {
continue;
}

$attribute_string = substr( $post->post_content, $start_offset, $token_length );
$attribute_json_start = strcspn( $attribute_string, '{' );
$attribute_json_end = strrpos( $attribute_string, '}' );

if ( false === $attribute_json_end || $attribute_json_start >= $attribute_json_end ) {
continue;
}

$json_start = $start_offset + $attribute_json_start;
$json_length = $attribute_json_end - $attribute_json_start + 1;

$attrs['metadata']['noteId'] = $this->processed_comments[ $old_note_id ];
$replacements[] = array( $json_start, $json_length, serialize_block_attributes( $attrs ) );
}

if ( empty( $replacements ) ) {
return;
}

$post_content = $post->post_content;
$updated_content = '';
$was_at = 0;

foreach ( $replacements as $replacement ) {
list( $offset, $length, $new_json ) = $replacement;

$pre_length = $offset - $was_at;
$updated_content .= substr( $post_content, $was_at, $pre_length ) . $new_json;
$was_at = $offset + $length;
}
Comment thread
dmsnell marked this conversation as resolved.

$updated_content .= substr( $post_content, $was_at );

wp_update_post(
// Cast to object to ensure wp_update_post() will add the required slashes.
(object) array(
'ID' => $post_id,
'post_content' => $updated_content,
)
);
}

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