WordPress.org

Make WordPress Core

Opened 9 months ago

Closed 9 months ago

Last modified 8 months ago

#40626 closed defect (bug) (fixed)

Revisions API do not initialize GLOBALS['post']

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

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 months ago.
40626-add-global.2.patch (1.6 KB) - added by pdufour 9 months ago.

Download all attachments as: .zip

Change History (13)

#1 @ocean90
9 months ago

  • Description modified (diff)

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


9 months ago

#3 @jnylen0
9 months ago

  • Description modified (diff)
  • Keywords has-patch has-unit-tests needs-refresh added
  • Milestone changed from Awaiting Review to 4.7.5
  • Owner set to jnylen0
  • Status changed from new to accepted
  • Version set to 4.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 months ago by jnylen0 (previous) (diff)

#4 @pdufour
9 months ago

Done! Thanks for the quick review @jnylen0.

#5 @joehoyle
9 months 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 months ago

Shortcodes @joehoyle

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


9 months ago

#8 @desrosj
9 months ago

  • Milestone changed from 4.7.5 to 4.8

#9 @rmccue
9 months ago

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

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 months ago

  • Resolution set to fixed
  • Status changed from assigned to closed

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
8 months ago

#40887 was marked as a duplicate.

Note: See TracTickets for help on using tickets.