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.


git-svn-id: https://develop.svn.wordpress.org/trunk@40601 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Ryan McCue 2017-05-10 04:31:28 +00:00
parent 3f66cf5a2d
commit 292a871e44
2 changed files with 15 additions and 0 deletions

View File

@ -338,6 +338,9 @@ class WP_REST_Revisions_Controller extends WP_REST_Controller {
* @return WP_REST_Response Response object.
*/
public function prepare_item_for_response( $post, $request ) {
$GLOBALS['post'] = $post;
setup_postdata( $post );
$schema = $this->get_item_schema();

View File

@ -335,4 +335,16 @@ class WP_Test_REST_Revisions_Controller extends WP_Test_REST_Controller_Testcase
$this->assertEquals( rest_url( '/wp/v2/' . $parent_base . '/' . $revision->post_parent ), $links['parent'][0]['href'] );
}
public function test_get_item_sets_up_postdata() {
wp_set_current_user( self::$editor_id );
$request = new WP_REST_Request( 'GET', '/wp/v2/posts/' . self::$post_id . '/revisions/' . $this->revision_id1 );
$this->server->dispatch( $request );
$post = get_post();
$parent_post_id = wp_is_post_revision( $post->ID );
$this->assertEquals( $post->ID, $this->revision_id1 );
$this->assertEquals( $parent_post_id, self::$post_id );
}
}