wppaste
WordPress

WP_REST_Revisions_Controller::restore_post_data( WP_Post|null $previous_post )

Since
7.1.0
Source
wp-includes/rest-api/endpoints/class-wp-rest-revisions-controller.php:765
Restores the global post to its previous value after preparing a revision.

Description

Preparing a revision overwrites the global post and post data via setup_postdata(). This restores the global post that was in place beforehand so the change does not leak into the rest of the request.

Only the global post is guaranteed to be restored. When there was no previous global post and the main query has no post either, which is the usual state during a REST request, wp_reset_postdata() has nothing to restore from, so the remaining globals set by setup_postdata() (such as $id, $authordata and $pages) are left describing the revision. Clearing those would mean unsetting each one by hand, which is beyond what is needed to keep the global post from leaking.

Compatibility

WordPress
since 7.1.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in 1 of the 5 tracked releases, added in 7.1.0, and compiles on PHP 7.4 through 8.6-dev.

Parameters

$previous_postWP_Post|null
The global post to restore, or null if there was none.

Performance profile

How much work a call to WP_REST_Revisions_Controller::restore_post_data() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Trivial

Touches nothing outside its own arguments.

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
7–8

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 13.

Plugin surface
None

Nothing here hands control to plugin code.

Called by
1

1 place in core call this, so the cost is paid more often than your own code shows.

What one call costs · 2 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost WP_REST_Revisions_Controller::restore_post_data() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
always7wp_reset_postdata()
always8setup_postdata()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev137–81
8.5137–81
8.4137–81
8.3137–81
8.2137–81
8.1137–812 fewer instructions than PHP 7.4
7.4158–91

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Uses · 2

  • setup_postdata()Set up global post data.
  • wp_reset_postdata()After looping through a separate query, this function restores the $post global to the current post in the main query.

Used by · 1

Source code

	private function restore_post_data( ?WP_Post $previous_post ): void {		if ( $previous_post ) {			$GLOBALS['post'] = $previous_post;			setup_postdata( $previous_post );			return;		} 		/*		 * There was no global post to restore, so clear the revision's post data.		 * This runs before clearing the global post because wp_reset_postdata()		 * repopulates it from the main query whenever that query has a post. Note		 * that it is a no-op when the main query has no post, in which case only		 * the global post below is cleared.		 */		wp_reset_postdata(); 		/*		 * Assigned rather than unset so that any `global $post` binding made before		 * this request keeps pointing at the global. Unsetting removes the entry from		 * the symbol table, which detaches those bindings, and a later write through		 * one of them would no longer be visible to get_post().		 */		$GLOBALS['post'] = null;	}

Changelog

Introduced in 7.1.0.

Signature, return type and hooks compared across 1 parsed release.

About this page

Parsed data
Generated from the wordpress-develop 7.1.0 tag, from src/wp-includes/rest-api/endpoints/class-wp-rest-revisions-controller.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.