CustomPostTypeStorage: Fix incorrect slashes in some json_encoded values#54
Open
nicolas-jaussaud wants to merge 1 commit into
Open
CustomPostTypeStorage: Fix incorrect slashes in some json_encoded values#54nicolas-jaussaud wants to merge 1 commit into
nicolas-jaussaud wants to merge 1 commit into
Conversation
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.
Hi @zinigor!
It looks like we have an issue with the
CustomPostTypeStorage, when saving a string that contains'Screencast.From.2026-07-09.00-38-10.mp4
The cause seems to be that the saved JSON is not valid (it contains
\'), so the value returned byjson_decode()in find() and all() will benullThe value get corrupted during save because:
add_magic_quotesby default on$_POSTfor every requests, which transfrom'to\'(here)\'is not valid in JSON, and is escaped to\\'when we callwp_json_encodeduring save (here)update_post_metaapplywp_unslashon the meta value (here), which revert the escaped\\'to\'(so the final value will be invalid JSON)The recommended approach seems to be to:
add_magic_quotesby applyingwp_unslashon$_POSTwp_slashonly when needed, for core function that expect it (wp_insert_post(),update_post_meta()... etc)We already apply
wp_unslashin ourRequestclass (here), as we copied the core implementation fromWP_REST_Server::serve_request()However, we were still reading from the raw
$_POSTfor saving, we should now use$request->get_body_params()insteadThis PR also update the
CustomPostTypeStorageclass, and make sure to applywp_slashwhere it's now needed (wp_insert_post,wp_update_post,update_post_meta)So before this PR, the data was going through:
And now it should be:
This PR also remove the strip slashes for the repeater value, as it shouldn't be necessary anymore (we will now get an unslashed data from
$request->get_body_params()there as well)I also took the opportunity to generate some tests that simulate a POST request which saves an entity, to be sure we get the same value in each storage