wp_restore_post_revision( int|WP_Post $revision, array $fields = null ): int|false|null
- Since
- 2.6.0
- Source
wp-includes/revision.php:462
Description
Can restore a past revision using all fields of the post revision, or only selected fields.
Compatibility
- WordPress
- since 2.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
$revisionint|WP_Post- Revision ID or revision object.
$fieldsarrayoptional- What fields to restore from. Defaults to all.Default:
null
Return value
int|false|null- Null if error, false if no fields to restore, (int) post ID if success.
Performance profile
How much work a call to wp_restore_post_revision() 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
- Scaling
- Scales with input
- Instructions
- 10–61
- Plugin surface
- 1 hook
- Called by
- 1
Reaches the database via get_post().
The body loops, so the work grows with what you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 68.
Third-party callbacks on 'wp_restore_post_revision' run inside this call, and their cost is not bounded by anything here.
1 place in core call this, so the cost is paid more often than your own code shows.
What it touches
- hookthird-party callbacks
do_action()called directly - querycontent query
get_post()one call below wp_restore_post_revision()
Further down the call graph this can also reach option, cache, serialize and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.
What one call costs · 9 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost wp_restore_post_revision() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 10 | wp_get_post_revision() |
is_array($fields) | 23–24 | wp_get_post_revision(), array_keys(), array_intersect() |
!is_array($fields) | 30–31 | wp_get_post_revision(), _wp_post_revision_fields(), array_keys(), array_keys(), array_intersect() |
is_array($fields) | 35–36 | wp_get_post_revision(), array_keys(), array_intersect(), wp_slash(), wp_update_post() |
is_array($fields) && is_wp_error() | 39–40 | wp_get_post_revision(), array_keys(), array_intersect(), wp_slash(), wp_update_post(), is_wp_error() |
!is_array($fields) | 42–43 | wp_get_post_revision(), _wp_post_revision_fields(), array_keys(), array_keys(), array_intersect(), wp_slash(), wp_update_post() |
!is_array($fields) && is_wp_error() | 46–47 | wp_get_post_revision(), _wp_post_revision_fields(), array_keys(), array_keys(), array_intersect(), wp_slash(), wp_update_post(), is_wp_error() |
is_array($fields) && !is_wp_error() | 53–54 | wp_get_post_revision(), array_keys(), array_intersect(), wp_slash(), wp_update_post(), is_wp_error(), get_current_user_id(), update_post_meta(), do_action() |
!is_array($fields) && !is_wp_error() | 60–61 | wp_get_post_revision(), _wp_post_revision_fields(), array_keys(), array_keys(), array_intersect(), wp_slash(), wp_update_post(), is_wp_error(), get_current_user_id(), update_post_meta(), do_action() |
Across PHP versions
Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 68 instructions, 10–61 executed per call, 7 branches. The work does not change between versions.
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.
Hooks and filters fired · 1
One hook fires while wp_restore_post_revision() runs, in this order:
- do_action( wp_restore_post_revision )actionline 503 (+41 into the body)
Fires after a post revision has been restored.
Uses · 8
- wp_get_post_revision()Gets a post revision.
- _wp_post_revision_fields()Determines which fields of posts are to be saved in revisions.
- wp_slash()Adds slashes to a string or recursively adds slashes to strings within an array.
- wp_update_post()Updates a post with new post data.
- is_wp_error()Checks whether the given variable is a WordPress Error.
- update_post_meta()Updates a post meta field based on the given post ID.
- get_current_user_id()Gets the current user's ID.
- do_action()Calls the callback functions that have been added to an action hook.
Used by · 1
- wp_xmlrpc_server::wp_restoreRevision()Restores a post revision.
Source code
function wp_restore_post_revision( $revision, $fields = null ) { $revision = wp_get_post_revision( $revision, ARRAY_A ); if ( ! $revision ) { return $revision; } if ( ! is_array( $fields ) ) { $fields = array_keys( _wp_post_revision_fields( $revision ) ); } $update = array(); foreach ( array_intersect( array_keys( $revision ), $fields ) as $field ) { $update[ $field ] = $revision[ $field ]; } if ( ! $update ) { return false; } $update['ID'] = $revision['post_parent']; $update = wp_slash( $update ); // Since data is from DB. $post_id = wp_update_post( $update ); if ( ! $post_id || is_wp_error( $post_id ) ) { return $post_id; } // Update last edit user. update_post_meta( $post_id, '_edit_last', get_current_user_id() ); /** * Fires after a post revision has been restored. * * @since 2.6.0 * * @param int $post_id Post ID. * @param int $revision_id Post revision ID. */ do_action( 'wp_restore_post_revision', $post_id, $revision['ID'] ); return $post_id;}Changelog
Introduced in 2.6.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 6.9.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.