Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unslash post content before parsing during save #3455

Merged
merged 1 commit into from
Nov 13, 2017

Conversation

mcsf
Copy link
Contributor

@mcsf mcsf commented Nov 13, 2017

Fixes #3426

See #2806 for context on wpautop filter.

Description

Per the comment in the fix, WP_REST_Posts_Controller slashes post data before inserting/updating a post. This data gets unslashed by wp_insert_post right before saving to the DB. The PEG parser expects unslashed input in order to properly parse JSON attributes.

How Has This Been Tested?

Try reproducing the parent issue.

Types of changes

Bug fix.

Checklist:

  • My code is tested.
  • My code follows the WordPress code style.
  • My code follows has proper inline documentation.

lib/blocks.php Outdated
// WP_REST_Posts_Controller slashes post data before inserting/updating
// a post. This data gets unslashed by `wp_insert_post` right before
// saving to the DB. The PEG parser needs unslashed input in order to
// properly parse JSON attributes.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this mean we need to "slash" data back after our custom behavior?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, yes, I left that line out. Amended. (Initially, not slashing back didn't break anything for me, but obviously any other piece consuming the hook should expect slashed data, as the signature for the hook states.)

@codecov
Copy link

codecov bot commented Nov 13, 2017

Codecov Report

Merging #3455 into master will increase coverage by 0.56%.
The diff coverage is n/a.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #3455      +/-   ##
==========================================
+ Coverage   33.85%   34.42%   +0.56%     
==========================================
  Files         252      252              
  Lines        6731     6922     +191     
  Branches     1221     1284      +63     
==========================================
+ Hits         2279     2383     +104     
- Misses       3755     3824      +69     
- Partials      697      715      +18
Impacted Files Coverage Δ
editor/block-mover/index.js 4.76% <0%> (-5.24%) ⬇️
blocks/library/freeform/old-editor.js 1.01% <0%> (-0.36%) ⬇️
editor/store-defaults.js 100% <0%> (ø) ⬆️
editor/modes/text-editor/index.js 0% <0%> (ø) ⬆️
blocks/library/button/index.js 23.07% <0%> (+3.07%) ⬆️
blocks/library/gallery/index.js 29.78% <0%> (+3.86%) ⬆️
components/autocomplete/index.js 78.82% <0%> (+5.56%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 81566fd...adf94b4. Read the comment docs.

@mcsf mcsf force-pushed the fix/unslash-wp-insert-post-data branch from 8ac56b6 to b013025 Compare November 13, 2017 11:48
lib/blocks.php Outdated
$data['post_content'] = gutenberg_wpautop_block_content( $data['post_content'] );
$data['post_content'] = wp_slash( $data['post_content'] );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor but It might be better to avoid storing any unslashed data in $data[ 'post_content' ] and use a temporary variable.

Copy link
Contributor

@youknowriad youknowriad left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Confirmed this fixes the issue, Thanks 👍

@mcsf mcsf force-pushed the fix/unslash-wp-insert-post-data branch from b013025 to adf94b4 Compare November 13, 2017 12:35
@mcsf mcsf merged commit ef44776 into master Nov 13, 2017
@gziolo gziolo deleted the fix/unslash-wp-insert-post-data branch November 13, 2017 12:51
@@ -220,7 +220,15 @@ function gutenberg_wpautop_block_content( $content ) {
*/
function gutenberg_wpautop_insert_post_data( $data ) {
if ( ! empty( $data['post_content'] ) && gutenberg_content_has_blocks( $data['post_content'] ) ) {
$data['post_content'] = gutenberg_wpautop_block_content( $data['post_content'] );
// WP_REST_Posts_Controller slashes post data before inserting/updating
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can expect this filter (wp_insert_post_data ) will be called not in the context of a REST API save, but generally any save for post content. Do we need to account for this? Is it wasteful (or worse, breaking) for us to be unslashing in these other contexts?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good questions.

Is it breaking? My reasoning:

  • A comment in wp_insert_post(), upon creating $data from a bunch of fields, reads Expected slashed, referring to said fields.
  • At the end, wp_insert_post() blindly unslashes all of $data, with no prior check for certain kinds of formatting in $data, which I took as another sign that it is expected that wp_insert_post be passed slashed data.
  • It would be odd if saving via the REST API would silently differ in such a crucial aspect.
  • Assuming that all other actors (incl. third parties) play well and provide slashed data, there shouldn't be anything breaking about this, as that would imply that $x !== wp_slash( wp_unslash( $x ) ).

Is it wasteful? Plausibly; we'd have to measure this. We could bypass this for non-REST-API contexts, but from my first paragraph it seems like the better default to leave it on.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Centered button identified as "modified externally"
3 participants