- Timestamp:
- 07/31/2026 06:08:13 AM (3 months ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-revisions-controller.php
r62928 r62952 596 596 * @since 4.7.0 597 597 * @since 5.9.0 Renamed `$post` to `$item` to match parent class for PHP 8 named parameter support. 598 * 599 * @global WP_Post $post Global post object. 598 * @since 7.1.0 The global post is now restored to its previous value before returning. 599 * 600 * @global WP_Post|null $post Global post object. 600 601 * 601 602 * @param WP_Post $item Post revision object. … … 604 605 */ 605 606 public function prepare_item_for_response( $item, $request ) { 607 /* 608 * Save the previous global post so it can be restored before returning. 609 * Preparing the revision sets up the global post and post data, which 610 * must not leak into the rest of the request (e.g. the autosaves endpoint 611 * is preloaded in the block editor, where a leaked global post can cause 612 * the editor to be initialized with the wrong post). 613 * 614 * Note that $post is intentionally not declared as a global here. It must 615 * remain local to this method so that a filter which reassigns the global 616 * post while the response is being prepared (for example on 'the_content') 617 * cannot change which post the remaining fields are read from. 618 */ 619 $previous_post = isset( $GLOBALS['post'] ) && $GLOBALS['post'] instanceof WP_Post ? $GLOBALS['post'] : null; 620 606 621 // Restores the more descriptive, specific name for use within this method. 607 622 $post = $item; … … 614 629 if ( $request->is_method( 'HEAD' ) ) { 615 630 /** This filter is documented in wp-includes/rest-api/endpoints/class-wp-rest-revisions-controller.php */ 616 return apply_filters( 'rest_prepare_revision', new WP_REST_Response( array() ), $post, $request ); 631 $response = apply_filters( 'rest_prepare_revision', new WP_REST_Response( array() ), $post, $request ); 632 633 $this->restore_post_data( $previous_post ); 634 635 return $response; 617 636 } 618 637 … … 718 737 * @param WP_REST_Request $request Request used to generate the response. 719 738 */ 720 return apply_filters( 'rest_prepare_revision', $response, $post, $request ); 739 $response = apply_filters( 'rest_prepare_revision', $response, $post, $request ); 740 741 $this->restore_post_data( $previous_post ); 742 743 return $response; 744 } 745 746 /** 747 * Restores the global post to its previous value after preparing a revision. 748 * 749 * Preparing a revision overwrites the global post and post data via 750 * setup_postdata(). This restores the global post that was in place 751 * beforehand so the change does not leak into the rest of the request. 752 * 753 * Only the global post is guaranteed to be restored. When there was no 754 * previous global post and the main query has no post either, which is the 755 * usual state during a REST request, wp_reset_postdata() has nothing to 756 * restore from, so the remaining globals set by setup_postdata() (such as 757 * $id, $authordata and $pages) are left describing the revision. Clearing 758 * those would mean unsetting each one by hand, which is beyond what is 759 * needed to keep the global post from leaking. 760 * 761 * @since 7.1.0 762 * 763 * @param WP_Post|null $previous_post The global post to restore, or null if there was none. 764 */ 765 private function restore_post_data( ?WP_Post $previous_post ): void { 766 if ( $previous_post ) { 767 $GLOBALS['post'] = $previous_post; 768 setup_postdata( $previous_post ); 769 return; 770 } 771 772 /* 773 * There was no global post to restore, so clear the revision's post data. 774 * This runs before clearing the global post because wp_reset_postdata() 775 * repopulates it from the main query whenever that query has a post. Note 776 * that it is a no-op when the main query has no post, in which case only 777 * the global post below is cleared. 778 */ 779 wp_reset_postdata(); 780 781 /* 782 * Assigned rather than unset so that any `global $post` binding made before 783 * this request keeps pointing at the global. Unsetting removes the entry from 784 * the symbol table, which detaches those bindings, and a later write through 785 * one of them would no longer be visible to get_post(). 786 */ 787 $GLOBALS['post'] = null; 721 788 } 722 789
Note:
See TracChangeset
for help on using the changeset viewer.
![(please configure the [header_logo] section in trac.ini)](/chrome/site/your_project_logo.png)