Skip to content

feat: add Notes as an element to copy when cloning a post - #548

Open
faisalahammad wants to merge 1 commit into
Yoast:trunkfrom
faisalahammad:fix/421-copy-notes
Open

faisalahammad wants to merge 1 commit into
Yoast:trunkfrom
faisalahammad:fix/421-copy-notes

Conversation

@faisalahammad

Copy link
Copy Markdown

Context

WordPress 6.9 stores Notes as comments with comment_type note, and the block markup keeps a reference to the note in the block metadata.noteId attribute. Until now the plugin had no way to copy Notes, so a cloned post lost all its notes and the block references were left pointing at the notes of the original post.

Fixes #421

Summary

This PR can be summarized in the following changelog entry:

  • Adds a Notes option to also copy block-level notes and their replies when cloning a post.

Relevant technical choices:

  • New Notes_Copier class in src/notes-copier.php queries the notes of the original post with get_comments( [ 'type' => 'note' ] ), which only returns open and resolved notes, and copies them with their comment fields and comment meta.
  • Replies are copied level by level, remapping comment_parent to the copied parent note, so the copied thread keeps the same structure even when the database order is not chronological.
  • The note IDs in the block markup are remapped by parsing the copied post content with parse_blocks() and updating the metadata.noteId attribute of each block, then saving it back with serialize_blocks(). Parsing the blocks instead of a text search keeps the remap limited to block metadata, including nested blocks, and leaves the rest of the content untouched.
  • The Notes option is registered in the legacy options list next to Comments, wired into the Clone and New Draft flows through the existing duplicate_post_after_duplicated hook (priority 45), and off by default.
  • Rewrite & Republish is not covered. Its copy is permanently deleted after republishing, so notes copied onto it would be deleted as well, leaving the republished post with block references to notes that no longer exist. Supporting it needs the notes to be transferred from the copy to the original post on republish, which can be handled in a follow-up.

Test instructions

Test instructions for the acceptance test before the PR gets merged

This PR can be acceptance tested by following these steps:

  1. Use WordPress 6.9 or newer and open a post in the block editor.
  2. Select a block and add a Note from the block toolbar, then add a reply to it.
  3. Go to Settings > Duplicate Post, open the "What to copy" tab, and enable the new Notes checkbox. Save.
  4. Clone the post (or use New Draft).
  5. Open the copy in the block editor and check that the note and its reply are there, attached to the right block, and that editing them does not change the notes of the original post.
  6. Disable the Notes checkbox, clone again, and check that this time the copy has no notes.
  7. Repeat steps 4 to 6 with the Bulk duplicate action on the posts list, and with the Copy date option on and off.

Relevant test scenarios

  • Changes should be tested with the browser console open
  • Changes should be tested on different posts/pages/taxonomies/custom post types/custom taxonomies
  • Changes should be tested on different editors (Default Block/Gutenberg/Classic/Elementor/other)
  • Changes should be tested on different browsers
  • Changes should be tested on multisite

Test instructions for QA when the code is in the RC

  • QA should use the same steps as above.

QA can test this PR by following these steps:

Use the same steps as above.

Impact check

This PR affects the following parts of the plugin, which may require extra testing:

  • The "What to copy" tab of the settings page, which gets one new checkbox.
  • The legacy duplication routine, which now runs one extra hook handler when the Notes option is on. With the option off (the default) nothing changes for existing users.

UI changes

  • This PR changes the UI in the plugin. I have added the 'UI change' label to this PR.

Documentation

  • I have written documentation for this change. For example, comments in the Relevant technical choices, comments in the code, documentation on Confluence / shared Google Drive / Yoast developer portal, or other.

Quality assurance

  • I have tested this code to the best of my abilities
  • I have added unittests to verify the code works as intended

Innovation

  • No innovation project is applicable for this PR.
  • This PR falls under an innovation project. I have attached the innovation label and noted the work hours.

Fixes #421

Adds a Notes checkbox to the settings page and a Notes_Copier class
that copies block-level notes and their replies when cloning a post.
The note IDs stored in the block markup are remapped to the copied
notes, so the copy references its own notes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add Notes as an "element to copy" option

1 participant