diff --git a/projects/plugins/jetpack/changelog/fix-copy-post-backslash-stripping b/projects/plugins/jetpack/changelog/fix-copy-post-backslash-stripping new file mode 100644 index 000000000000..a16e6fc7f009 --- /dev/null +++ b/projects/plugins/jetpack/changelog/fix-copy-post-backslash-stripping @@ -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. diff --git a/projects/plugins/jetpack/modules/copy-post.php b/projects/plugins/jetpack/modules/copy-post.php index 7b36e130b74d..70a932b4c853 100644 --- a/projects/plugins/jetpack/modules/copy-post.php +++ b/projects/plugins/jetpack/modules/copy-post.php @@ -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 ) ); } /** diff --git a/projects/plugins/jetpack/tests/php/modules/copy-post/Copy_Post_Test.php b/projects/plugins/jetpack/tests/php/modules/copy-post/Copy_Post_Test.php index 7eca0443070a..bb3331197fb7 100644 --- a/projects/plugins/jetpack/tests/php/modules/copy-post/Copy_Post_Test.php +++ b/projects/plugins/jetpack/tests/php/modules/copy-post/Copy_Post_Test.php @@ -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' ); + } }