diff --git a/admin-functions.php b/admin-functions.php index 26acf31ae..f745f16c0 100644 --- a/admin-functions.php +++ b/admin-functions.php @@ -10,6 +10,7 @@ return; } +use Yoast\WP\Duplicate_Post\Notes_Cleaner; use Yoast\WP\Duplicate_Post\UI\Newsletter; use Yoast\WP\Duplicate_Post\Utils; @@ -70,6 +71,7 @@ function duplicate_post_admin_init() { add_action( 'duplicate_post_after_duplicated', 'duplicate_post_copy_comments', 40, 2 ); } + add_action( 'duplicate_post_after_duplicated', 'duplicate_post_clean_note_metadata', 46, 1 ); add_action( 'duplicate_post_after_duplicated', 'duplicate_post_copy_post_taxonomies', 50, 2 ); add_filter( 'plugin_row_meta', 'duplicate_post_add_plugin_links', 10, 2 ); @@ -540,6 +542,18 @@ function duplicate_post_copy_children( $new_id, $post, $status = '' ) { } } +/** + * Removes invalid Note metadata from a duplicated post. + * + * @param int $new_id The new post ID. + * + * @return void + */ +function duplicate_post_clean_note_metadata( $new_id ) { + $notes_cleaner = new Notes_Cleaner(); + $notes_cleaner->clean( $new_id ); +} + /** * Copies comments. * diff --git a/src/notes-cleaner.php b/src/notes-cleaner.php new file mode 100644 index 000000000..11a7d99e5 --- /dev/null +++ b/src/notes-cleaner.php @@ -0,0 +1,92 @@ + $post_id, + 'type' => 'note', + 'fields' => 'ids', + ], + ), + ); + + $content = \get_post_field( 'post_content', $post_id ); + $blocks = \parse_blocks( $content ); + $changed = $this->remove_invalid_note_ids( $blocks, $valid_note_ids ); + + if ( ! $changed ) { + return; + } + + \wp_update_post( + [ + 'ID' => $post_id, + 'post_content' => \serialize_blocks( $blocks ), + ], + ); + } + + /** + * Removes invalid Note references from blocks recursively. + * + * @param array $blocks Parsed blocks. + * @param array $valid_note_ids Note IDs belonging to the post. + * + * @return bool Whether any references were removed. + */ + private function remove_invalid_note_ids( array &$blocks, array $valid_note_ids ) { + $changed = false; + + foreach ( $blocks as &$block ) { + if ( isset( $block['attrs']['metadata']['noteId'] ) ) { + $note_ids = (array) $block['attrs']['metadata']['noteId']; + $filtered = \array_values( + \array_filter( + $note_ids, + static function ( $note_id ) use ( $valid_note_ids ) { + return \in_array( (int) $note_id, $valid_note_ids, true ); + }, + ), + ); + + if ( \count( $filtered ) !== \count( $note_ids ) ) { + $changed = true; + if ( empty( $filtered ) ) { + unset( $block['attrs']['metadata']['noteId'] ); + } + elseif ( \is_array( $block['attrs']['metadata']['noteId'] ) ) { + $block['attrs']['metadata']['noteId'] = $filtered; + } + else { + $block['attrs']['metadata']['noteId'] = $filtered[0]; + } + } + } + + if ( ! empty( $block['innerBlocks'] ) && $this->remove_invalid_note_ids( $block['innerBlocks'], $valid_note_ids ) ) { + $changed = true; + } + } + unset( $block ); + + return $changed; + } +} diff --git a/tests/WP/Legacy_Duplication_Test.php b/tests/WP/Legacy_Duplication_Test.php index e78a0cc4b..fd5f4f6b6 100644 --- a/tests/WP/Legacy_Duplication_Test.php +++ b/tests/WP/Legacy_Duplication_Test.php @@ -3,6 +3,7 @@ namespace Yoast\WP\Duplicate_Post\Tests\WP; use WP_Post; +use Yoast\WP\Duplicate_Post\Notes_Cleaner; use Yoast\WPTestUtils\WPIntegration\TestCase; /** @@ -357,6 +358,77 @@ public function test_copies_comments_with_threading_and_skips_pingbacks() { $this->assertSame( (int) $by_content['Parent comment']->comment_ID, (int) $by_content['Reply comment']->comment_parent ); } + /** + * Tests that stale Note metadata is removed from cloned content. + * + * @covers ::duplicate_post_create_duplicate + * @covers ::duplicate_post_clean_note_metadata + * @covers \Yoast\WP\Duplicate_Post\Notes_Cleaner::clean + * + * @return void + */ + public function test_removes_stale_note_metadata_from_cloned_content() { + $original = $this->create_original_post(); + $note_id = $this->factory->comment->create( + [ + 'comment_post_ID' => $original->ID, + 'comment_type' => 'note', + 'comment_content' => 'Original note', + ], + ); + + global $wpdb; + // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching + $wpdb->update( + $wpdb->posts, + [ 'post_content' => '

Original content.

' ], + [ 'ID' => $original->ID ], + ); + \clean_post_cache( $original->ID ); + $original = \get_post( $original->ID ); + + \add_action( 'duplicate_post_after_duplicated', 'duplicate_post_clean_note_metadata', 46, 1 ); + $new_id = \duplicate_post_create_duplicate( $original ); + \remove_action( 'duplicate_post_after_duplicated', 'duplicate_post_clean_note_metadata', 46 ); + \clean_post_cache( $new_id ); + $blocks = \parse_blocks( \get_post_field( 'post_content', $new_id ) ); + + $this->assertArrayNotHasKey( 'noteId', $blocks[0]['attrs']['metadata'] ); + $this->assertArrayNotHasKey( 'noteId', $blocks[0]['innerBlocks'][0]['attrs']['metadata'] ); + } + + /** + * Tests that valid target Notes are retained while foreign references are removed. + * + * @covers \Yoast\WP\Duplicate_Post\Notes_Cleaner::clean + * + * @return void + */ + public function test_retains_note_metadata_belonging_to_target_post() { + $target_id = $this->factory->post->create(); + $note_id = $this->factory->comment->create( + [ + 'comment_post_ID' => $target_id, + 'comment_type' => 'note', + 'comment_content' => 'Target note', + ], + ); + global $wpdb; + // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching + $wpdb->update( + $wpdb->posts, + [ 'post_content' => '

Content

' ], + [ 'ID' => $target_id ], + ); + \clean_post_cache( $target_id ); + + ( new Notes_Cleaner() )->clean( $target_id ); + + $blocks = \parse_blocks( \get_post_field( 'post_content', $target_id ) ); + + $this->assertSame( [ $note_id ], $blocks[0]['attrs']['metadata']['noteId'] ); + } + /** * Tests that copied children include grandchildren via recursion. *