Make WordPress Core

Opened 9 years ago

Closed 9 years ago

Last modified 6 weeks ago

#40626 closed defect (bug) (fixed)

Revisions API do not initialize GLOBALS['post']

Reported by: pdufour Owned by: rmccue
Priority: normal Milestone: 4.8
Component: REST API Version: 4.7
Severity: normal Keywords: has-unit-tests has-patch commit
Cc: Focuses:

Description (last modified by jnylen0)

Calling functions like get_post() do not work within the Post Revisions API endpoint since they don't have a line like $GLOBALS['post'] = $post; which the posts controller does have https://github.com/WordPress/WordPress/blob/4.7.4/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php#L1381-L1383.

Fix if I understand the issue would be to add those two lines to prepare_item_for_response in the revisions controller.

Attachments (2)

40626-add-global.patch (1.7 KB ) - added by pdufour 9 years ago.
40626-add-global.2.patch (1.6 KB ) - added by pdufour 9 years ago.

Download all attachments as: .zip

Change History (14)

#1 @ocean90
9 years ago

  • Description modified (diff)

This ticket was mentioned in Slack in #core-restapi by pdufour. View the logs.


9 years ago

#3 @jnylen0
9 years ago

  • Description modified (diff)
  • Keywords has-patch has-unit-tests needs-refresh added
  • Milestone Awaiting Review4.7.5
  • Owner set to jnylen0
  • Status newaccepted
  • Version4.7

Thanks for the report and the patch. There are two super-minor whitespace issues in 40626-add-global.patch:

  • Blank line added to the beginning of the test file
  • Extra space in $parent_post_id = wp_is_post_revision ( $post->ID );

As far as the change itself, I can't think of any unintended effects this would have. Anyone else? Otherwise let's get this committed soon.

Last edited 9 years ago by jnylen0 (previous) (diff)

#4 @pdufour
9 years ago

Done! Thanks for the quick review @jnylen0.

#5 @joehoyle
9 years ago

Unless it's needed, I'm not sure _why_ we would want to add this. I believe they are there in the Posts Controller because there's some internal WP functions that require these globals to be set, so we had to make $post global. If that's not explicitly needed in the revisions endpoint, I don't think we should add it. All filters in the Revisions controller should get passed the Request or WP_Post object, so I'm not sure why you'd want to do get_post() with not ID param.

#6 @pdufour
9 years ago

Shortcodes @joehoyle

This ticket was mentioned in Slack in #core by desrosj. View the logs.


9 years ago

#8 @desrosj
9 years ago

  • Milestone 4.7.54.8

#9 @rmccue
9 years ago

  • Keywords commit added; needs-refresh removed
  • Owner changed from jnylen0 to rmccue
  • Status acceptedassigned

This is primarily for compatibility with existing deep filters inside WP core, so +1 on this. Patch looks good to me as well.

#10 @rmccue
9 years ago

  • Resolutionfixed
  • Status assignedclosed

In 40601:

REST API: Set global $post when preparing revisions.

For compatibility with filters on hooks on content filters (such as shortcodes), we need to set the global. This mirrors the Posts controller.

Props pdufour, jnylen0.
Fixes #40626.

#11 @ocean90
9 years ago

#40887 was marked as a duplicate.

#12 @westonruter
6 weeks ago

In 62952:

REST API: Restore the global post after preparing a revision.

WP_REST_Revisions_Controller::prepare_item_for_response() set the global $post to the revision being prepared and called setup_postdata(), but never restored the previous value, so the change persisted for the remainder of the request. WP_REST_Autosaves_Controller delegates to this method and the block editor preloads the autosaves endpoint on every load, so a post with a pending autosave could leave the global $post pointing at the autosave revision while edit-form-blocks.php built the editor bootstrap, initializing the editor with the wrong post and rewriting the URL to it.

The previous global $post is now captured before setup_postdata() and restored on every return path, including the early return for HEAD requests.

Developed in https://github.com/WordPress/wordpress-develop/pull/12248.
Follow-up to r40601, r59899.

Props micahele, gusgomezpg, westonruter, wildworks, dhrupo.
See #40626, #43502.
Fixes #65495.

Note: See TracTickets for help on using tickets.