Copy Post: preserve backslashes in duplicated post content#48376
Closed
shsajalchowdhury wants to merge 1 commit into
Closed
Copy Post: preserve backslashes in duplicated post content#48376shsajalchowdhury wants to merge 1 commit into
shsajalchowdhury wants to merge 1 commit into
Conversation
When duplicating a post, backslash characters were being stripped from the content. This happened because wp_update_post() calls wp_unslash() on array input (via wp_insert_post -> sanitize_post), but the data from get_post() is already unslashed. The fix adds wp_slash() before passing the data array to wp_update_post(), so wp_unslash() inside restores the original content with backslashes intact. Includes tests verifying backslash sequences like \t, \n, \\ are preserved when copying posts. Fixes Automattic#46020
Contributor
|
Thank you for your PR! When contributing to Jetpack, we have a few suggestions that can help us test and review your patch:
This comment will be updated as you work on your PR and make changes. If you think that some of those checks are not needed for your PR, please explain why you think so. Thanks for cooperation 🤖 |
Contributor
|
Somehow this PR slipped through the cracks and a separate fix was merged (#48870), so I'll close this one, but thanks for your contribution! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What was the problem?
When duplicating a post via the Copy Post feature, backslash characters (
\) were stripped from the post content. For example,\t is a tabin a code block becamet is a tab.This happened because
wp_update_post()callswp_unslash()on array input (viawp_insert_post()→sanitize_post()), but the source data fromget_post()is already unslashed. The double unslashing removes legitimate backslash characters.Closes #46020
How was it fixed?
Added
wp_slash()to the data array before passing it towp_update_post(). This way,wp_unslash()insidewp_update_post()restores the original content with backslashes intact.This matches the documented behavior in WordPress core:
wp_update_post()only auto-slashes when passed an object, not an array. When passing an array, the caller is responsible for slashing the data.Reference: https://developer.wordpress.org/reference/functions/wp_update_post/
Changes
projects/plugins/jetpack/modules/copy-post.php— wrap$datainwp_slash()beforewp_update_post()projects/plugins/jetpack/tests/php/modules/copy-post/Copy_Post_Test.php— added tests for backslash preservationprojects/plugins/jetpack/changelog/fix-copy-post-backslash-stripping— changelog entryTesting instructions
\t is a tab\t is a tab(nott is a tab)\\,\n,\fto confirm all are preservedjetpack test php --filter=copy-postDoes this pull request change what data or activity we track or use?
No. This PR only fixes how post content is passed to a WordPress core function. No data collection, tracking, or privacy-related changes.