Skip to content
Merged
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: bugfix

Copy Post: fix backslash characters being stripped from post content, title, and excerpt when duplicating a post.
2 changes: 1 addition & 1 deletion projects/plugins/jetpack/modules/copy-post.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ protected function update_content( $source_post, $target_post_id ) {
* @param int $target_post_id Target post ID.
*/
$data = apply_filters( 'jetpack_copy_post_data', $data, $source_post, $target_post_id );
return wp_update_post( $data );
return wp_update_post( wp_slash( $data ) );
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -257,4 +257,38 @@ public function test_copy_footnotes_preserves_content() {
$this->assertIsArray( $target_footnotes );
$this->assertEquals( $footnote_content, $target_footnotes[0]['content'] );
}

/**
* Test that update_content preserves backslashes in post fields.
*/
public function test_update_content_preserves_backslashes() {
$copy_post = new Jetpack_Copy_Post();

$source_content = "Code: \\t is a tab, \\n is a newline, \\\\ is a backslash";
$source_excerpt = "Excerpt with \\t tab";
$source_title = "Title with \\t tab";

$source_post_id = self::factory()->post->create(
wp_slash(
array(
'post_content' => $source_content,
'post_excerpt' => $source_excerpt,
'post_title' => $source_title,
)
)
);
$source_post = get_post( $source_post_id );
$target_post_id = self::factory()->post->create();

$method = new ReflectionMethod( Jetpack_Copy_Post::class, 'update_content' );
if ( PHP_VERSION_ID < 80100 ) {
$method->setAccessible( true );
}
$method->invoke( $copy_post, $source_post, $target_post_id );

$target_post = get_post( $target_post_id );
$this->assertSame( $source_content, $target_post->post_content, 'Backslashes in post_content should be preserved' );
$this->assertSame( $source_excerpt, $target_post->post_excerpt, 'Backslashes in post_excerpt should be preserved' );
$this->assertSame( $source_title, $target_post->post_title, 'Backslashes in post_title should be preserved' );
}
}