Make WordPress Core


Ignore:
Timestamp:
07/31/2026 06:08:13 AM (3 months ago)
Author:
westonruter
Message:

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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-revisions-controller.php

    r62928 r62952  
    596596         * @since 4.7.0
    597597         * @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.
    600601         *
    601602         * @param WP_Post         $item    Post revision object.
    … …  
    604605         */
    605606        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
    606621                // Restores the more descriptive, specific name for use within this method.
    607622                $post = $item;
    … …  
    614629                if ( $request->is_method( 'HEAD' ) ) {
    615630                        /** 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;
    617636                }
    618637
    … …  
    718737                 * @param WP_REST_Request  $request  Request used to generate the response.
    719738                 */
    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;
    721788        }
    722789
Note: See TracChangeset for help on using the changeset viewer.