wppaste
WordPress

_wp_upgrade_revisions_of_post( WP_Post $post, array $revisions ): bool

Since
3.6.0
Source
wp-includes/revision.php:1009
Upgrades the revisions author, adds the current post as a revision and sets the revisions version to 1.

Compatibility

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

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Parameters

$postWP_Post
Post object.
$revisionsarray
Current revisions of the post.

Return value

bool
true if the revisions were upgraded, false if problems.

Performance profile

How much work a call to _wp_upgrade_revisions_of_post() 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
Heavy

Reaches the database via ->query().

Scaling
Scales with input

The body loops, so the work grows with what you pass in.

Instructions
27–97

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

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 it touches

  • optionoption read or writeget_option()called directly
  • cacheobject cachewp_cache_delete()called directly
  • sqldatabase query->query()called directly
  • hookthird-party callbacksapply_filters()one call below _wp_upgrade_revisions_of_post()
  • serializeserialisationmaybe_unserialize()one call below _wp_upgrade_revisions_of_post()
  • querycontent queryget_post()one call below _wp_upgrade_revisions_of_post()

Further down the call graph this can also reach transient. That is the worst case, several calls deep and usually down an error path, not what a normal call pays.

What one call costs · 21 distinct outcomes

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

WhenInstructionsCalls it makes
always27–31time(), ->prepare(), ->query(), get_option()
always49–53time(), ->prepare(), ->query(), update_option(), reset(), current(), next(), _wp_get_post_revision_version(), delete_option()
always54–58time(), ->prepare(), ->query(), update_option(), reset(), current(), next(), _wp_get_post_revision_version(), delete_option(), wp_save_post_revision()
!$locked58–62time(), ->prepare(), ->query(), get_option(), update_option(), reset(), current(), next(), _wp_get_post_revision_version(), delete_option()
!$locked63–67time(), ->prepare(), ->query(), get_option(), update_option(), reset(), current(), next(), _wp_get_post_revision_version(), delete_option(), wp_save_post_revision()
$this_revision_version !== false && !$this_revision_version68time(), ->prepare(), ->query(), update_option(), reset(), current(), next(), _wp_get_post_revision_version(), ID(), delete_option()
$this_revision_version !== false && !$this_revision_version73time(), ->prepare(), ->query(), update_option(), reset(), current(), next(), _wp_get_post_revision_version(), ID(), delete_option(), wp_save_post_revision()
$this_revision_version !== false && !$this_revision_version74time(), ->prepare(), ->query(), update_option(), reset(), current(), next(), _wp_get_post_revision_version(), ID(), wp_cache_delete(), delete_option()
$this_revision_version !== false && !$this_revision_version74–77time(), ->prepare(), ->query(), update_option(), reset(), current(), next(), _wp_get_post_revision_version(), _wp_get_post_revision_version(), ID(), delete_option()
!$locked && $this_revision_version !== false && !$this_revision_version77time(), ->prepare(), ->query(), get_option(), update_option(), reset(), current(), next(), _wp_get_post_revision_version(), ID(), delete_option()
$this_revision_version !== false && !$this_revision_version79time(), ->prepare(), ->query(), update_option(), reset(), current(), next(), _wp_get_post_revision_version(), ID(), wp_cache_delete(), delete_option(), wp_save_post_revision()
$this_revision_version !== false && !$this_revision_version79–82time(), ->prepare(), ->query(), update_option(), reset(), current(), next(), _wp_get_post_revision_version(), _wp_get_post_revision_version(), ID(), delete_option(), wp_save_post_revision()
9 further outcomes, up to 97 instructions
$this_revision_version !== false && !$this_revision_version80–83time(), ->prepare(), ->query(), update_option(), reset(), current(), next(), _wp_get_post_revision_version(), _wp_get_post_revision_version(), ID(), wp_cache_delete(), delete_option()
!$locked && $this_revision_version !== false && !$this_revision_version82time(), ->prepare(), ->query(), get_option(), update_option(), reset(), current(), next(), _wp_get_post_revision_version(), ID(), delete_option(), wp_save_post_revision()
!$locked && $this_revision_version !== false && !$this_revision_version83time(), ->prepare(), ->query(), get_option(), update_option(), reset(), current(), next(), _wp_get_post_revision_version(), ID(), wp_cache_delete(), delete_option()
!$locked && $this_revision_version !== false && !$this_revision_version83–86time(), ->prepare(), ->query(), get_option(), update_option(), reset(), current(), next(), _wp_get_post_revision_version(), _wp_get_post_revision_version(), ID(), delete_option()
$this_revision_version !== false && !$this_revision_version85–88time(), ->prepare(), ->query(), update_option(), reset(), current(), next(), _wp_get_post_revision_version(), _wp_get_post_revision_version(), ID(), wp_cache_delete(), delete_option(), wp_save_post_revision()
!$locked && $this_revision_version !== false && !$this_revision_version88time(), ->prepare(), ->query(), get_option(), update_option(), reset(), current(), next(), _wp_get_post_revision_version(), ID(), wp_cache_delete(), delete_option(), wp_save_post_revision()
!$locked && $this_revision_version !== false && !$this_revision_version88–91time(), ->prepare(), ->query(), get_option(), update_option(), reset(), current(), next(), _wp_get_post_revision_version(), _wp_get_post_revision_version(), ID(), delete_option(), wp_save_post_revision()
!$locked && $this_revision_version !== false && !$this_revision_version89–92time(), ->prepare(), ->query(), get_option(), update_option(), reset(), current(), next(), _wp_get_post_revision_version(), _wp_get_post_revision_version(), ID(), wp_cache_delete(), delete_option()
!$locked && $this_revision_version !== false && !$this_revision_version94–97time(), ->prepare(), ->query(), get_option(), update_option(), reset(), current(), next(), _wp_get_post_revision_version(), _wp_get_post_revision_version(), ID(), wp_cache_delete(), delete_option(), wp_save_post_revision()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev10127–9710
8.510127–9710
8.410127–97103 fewer instructions than PHP 8.3
8.310427–10010
8.210427–10010
8.110427–10010
7.410427–10010

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 · 6

Used by · 1

  • edit_post()Updates an existing post with values provided in `$_POST`.

Source code

function _wp_upgrade_revisions_of_post( $post, $revisions ) {	global $wpdb; 	// Add post option exclusively.	$lock   = "revision-upgrade-{$post->ID}";	$now    = time();	$result = $wpdb->query( $wpdb->prepare( "INSERT IGNORE INTO `$wpdb->options` (`option_name`, `option_value`, `autoload`) VALUES (%s, %s, 'off') /* LOCK */", $lock, $now ) ); 	if ( ! $result ) {		// If we couldn't get a lock, see how old the previous lock is.		$locked = get_option( $lock ); 		if ( ! $locked ) {			/*			 * Can't write to the lock, and can't read the lock.			 * Something broken has happened.			 */			return false;		} 		if ( $locked > $now - HOUR_IN_SECONDS ) {			// Lock is not too old: some other process may be upgrading this post. Bail.			return false;		} 		// Lock is too old - update it (below) and continue.	} 	// If we could get a lock, re-"add" the option to fire all the correct filters.	update_option( $lock, $now ); 	reset( $revisions );	$add_last = true; 	do {		$this_revision = current( $revisions );		$prev_revision = next( $revisions ); 		$this_revision_version = _wp_get_post_revision_version( $this_revision ); 		// Something terrible happened.		if ( false === $this_revision_version ) {			continue;		} 		/*		 * 1 is the latest revision version, so we're already up to date.		 * No need to add a copy of the post as latest revision.		 */		if ( 0 < $this_revision_version ) {			$add_last = false;			continue;		} 		// Always update the revision version.		$update = array(			'post_name' => preg_replace( '/^(\d+-(?:autosave|revision))[\d-]*$/', '$1-v1', $this_revision->post_name ),		); 		/*		 * If this revision is the oldest revision of the post, i.e. no $prev_revision,		 * the correct post_author is probably $post->post_author, but that's only a good guess.		 * Update the revision version only and Leave the author as-is.		 */		if ( $prev_revision ) {			$prev_revision_version = _wp_get_post_revision_version( $prev_revision ); 			// If the previous revision is already up to date, it no longer has the information we need :(			if ( $prev_revision_version < 1 ) {				$update['post_author'] = $prev_revision->post_author;			}		} 		// Upgrade this revision.		$result = $wpdb->update( $wpdb->posts, $update, array( 'ID' => $this_revision->ID ) ); 		if ( $result ) {			wp_cache_delete( $this_revision->ID, 'posts' );		}	} while ( $prev_revision );

Changelog

Introduced in 3.6.0. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 6.7.7 tag, from src/wp-includes/revision.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.