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
14 changes: 14 additions & 0 deletions admin-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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 );
Expand Down Expand Up @@ -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.
*
Expand Down
92 changes: 92 additions & 0 deletions src/notes-cleaner.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php

namespace Yoast\WP\Duplicate_Post;

/**
* Removes invalid block-level Note references from a duplicated post.
*
* @since 4.7
*/
class Notes_Cleaner {

/**
* Removes Note references that do not belong to the duplicated post.
*
* @param int $post_id The duplicated post ID.
*
* @return void
*/
public function clean( $post_id ) {
$valid_note_ids = \array_map(
'intval',
(array) \get_comments(
[
'post_id' => $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<int, array{}> $blocks Parsed blocks.
* @param array<int, int> $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;
}
}
72 changes: 72 additions & 0 deletions tests/WP/Legacy_Duplication_Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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' => '<!-- wp:group {"metadata":{"noteId":[' . $note_id . ',999999]}} --><div class="wp-block-group"><!-- wp:paragraph {"metadata":{"noteId":' . $note_id . '}} --><p>Original content.</p><!-- /wp:paragraph --></div><!-- /wp:group -->' ],
[ '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' => '<!-- wp:paragraph {"metadata":{"noteId":[' . $note_id . ',999999]}} --><p>Content</p><!-- /wp:paragraph -->' ],
[ '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.
*
Expand Down