wp_restore_image( int $post_id ): stdClass
- Since
- 2.9.0
- Source
wp-admin/includes/image-edit.php:815
Compatibility
- WordPress
- since 2.9.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
$post_idint- Attachment post ID.
Return value
stdClass- Image restoration message object.
Performance profile
How much work a call to wp_restore_image() 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
- 28–133
- Plugin surface
- None
- 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 211.
Nothing here hands control to plugin code.
1 place in core call this, so the cost is paid more often than your own code shows.
What it touches
- querycontent query
get_post()one call below wp_restore_image() - hookthird-party callbacks
apply_filters()one call below wp_restore_image()
Further down the call graph this can also reach serialize, cache, option 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 · 13 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost wp_restore_image() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 28–30 | wp_get_attachment_metadata(), get_attached_file(), get_post_meta(), __() |
is_array($meta) && is_array($backup_sizes) | 56–69 | wp_get_attachment_metadata(), get_attached_file(), get_post_meta(), pathinfo(), time(), rand(), get_intermediate_image_sizes(), wp_update_attachment_metadata(), __() |
is_array($meta) && is_array($backup_sizes) && wp_update_attachment_metadata() && $old_backup_sizes !== false | 64–75 | wp_get_attachment_metadata(), get_attached_file(), get_post_meta(), pathinfo(), time(), rand(), get_intermediate_image_sizes(), wp_update_attachment_metadata(), update_post_meta(), __() |
is_array($meta) && is_array($backup_sizes) && wp_update_attachment_metadata() && $old_backup_sizes === false && defined('IMAGE_EDIT_OVERWRITE') | 67–73 | wp_get_attachment_metadata(), get_attached_file(), get_post_meta(), pathinfo(), time(), rand(), get_intermediate_image_sizes(), wp_update_attachment_metadata(), __(), delete_post_meta() |
is_array($meta) && is_array($backup_sizes) && wp_update_attachment_metadata() && $old_backup_sizes !== false && update_post_meta() && defined('IMAGE_EDIT_OVERWRITE') | 73–79 | wp_get_attachment_metadata(), get_attached_file(), get_post_meta(), pathinfo(), time(), rand(), get_intermediate_image_sizes(), wp_update_attachment_metadata(), update_post_meta(), __(), delete_post_meta() |
is_array($meta) && is_array($backup_sizes) && isset($backup_sizes) | 91–123 | wp_get_attachment_metadata(), get_attached_file(), get_post_meta(), pathinfo(), time(), rand(), get_intermediate_image_sizes(), path_join(), update_attached_file(), _wp_relative_upload_path(), wp_update_attachment_metadata(), __() |
is_array($meta) && is_array($backup_sizes) && isset($backup_sizes) && wp_update_attachment_metadata() && $old_backup_sizes !== false | 99–129 | wp_get_attachment_metadata(), get_attached_file(), get_post_meta(), pathinfo(), time(), rand(), get_intermediate_image_sizes(), path_join(), update_attached_file(), _wp_relative_upload_path(), wp_update_attachment_metadata(), update_post_meta(), __() |
is_array($meta) && is_array($backup_sizes) && isset($backup_sizes) && wp_update_attachment_metadata() && $old_backup_sizes === false && defined('IMAGE_EDIT_OVERWRITE') | 102–127 | wp_get_attachment_metadata(), get_attached_file(), get_post_meta(), pathinfo(), time(), rand(), get_intermediate_image_sizes(), path_join(), update_attached_file(), _wp_relative_upload_path(), wp_update_attachment_metadata(), __(), delete_post_meta() |
is_array($meta) && is_array($backup_sizes) && isset($backup_sizes) && defined('IMAGE_EDIT_OVERWRITE') | 102–115 | wp_get_attachment_metadata(), get_attached_file(), get_post_meta(), pathinfo(), time(), rand(), get_intermediate_image_sizes(), wp_delete_file(), path_join(), update_attached_file(), _wp_relative_upload_path(), wp_update_attachment_metadata(), __() |
is_array($meta) && is_array($backup_sizes) && isset($backup_sizes) && wp_update_attachment_metadata() && $old_backup_sizes !== false && update_post_meta() && defined('IMAGE_EDIT_OVERWRITE') | 108–133 | wp_get_attachment_metadata(), get_attached_file(), get_post_meta(), pathinfo(), time(), rand(), get_intermediate_image_sizes(), path_join(), update_attached_file(), _wp_relative_upload_path(), wp_update_attachment_metadata(), update_post_meta(), __(), delete_post_meta() |
is_array($meta) && is_array($backup_sizes) && isset($backup_sizes) && defined('IMAGE_EDIT_OVERWRITE') && wp_update_attachment_metadata() && $old_backup_sizes !== false | 110–121 | wp_get_attachment_metadata(), get_attached_file(), get_post_meta(), pathinfo(), time(), rand(), get_intermediate_image_sizes(), wp_delete_file(), path_join(), update_attached_file(), _wp_relative_upload_path(), wp_update_attachment_metadata(), update_post_meta(), __() |
is_array($meta) && is_array($backup_sizes) && isset($backup_sizes) && defined('IMAGE_EDIT_OVERWRITE') && wp_update_attachment_metadata() && $old_backup_sizes === false | 113–119 | wp_get_attachment_metadata(), get_attached_file(), get_post_meta(), pathinfo(), time(), rand(), get_intermediate_image_sizes(), wp_delete_file(), path_join(), update_attached_file(), _wp_relative_upload_path(), wp_update_attachment_metadata(), __(), delete_post_meta() |
1 further outcome, up to 125 instructions
is_array($meta) && is_array($backup_sizes) && isset($backup_sizes) && defined('IMAGE_EDIT_OVERWRITE') && wp_update_attachment_metadata() && $old_backup_sizes !== false && update_post_meta() | 119–125 | wp_get_attachment_metadata(), get_attached_file(), get_post_meta(), pathinfo(), time(), rand(), get_intermediate_image_sizes(), wp_delete_file(), path_join(), update_attached_file(), _wp_relative_upload_path(), wp_update_attachment_metadata(), update_post_meta(), __(), delete_post_meta() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 211 | 28–133 | 26 | |
| 8.5 | 211 | 28–133 | 26 | |
| 8.4 | 211 | 28–133 | 26 | 6 fewer instructions than PHP 8.3 |
| 8.3 | 217 | 28–133 | 26 | |
| 8.2 | 217 | 28–133 | 26 | |
| 8.1 | 217 | 28–133 | 26 | 1 fewer instruction than PHP 7.4 |
| 7.4 | 218 | 28–134 | 26 |
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 · 13
- wp_get_attachment_metadata()Retrieves attachment metadata for attachment ID.
- get_attached_file()Retrieves attached file path based on attachment ID.
- get_post_meta()Retrieves a post meta field for the given post ID.
- __()Retrieves the translation of $text.
- get_intermediate_image_sizes()Gets the available intermediate image size names.
- wp_delete_file()Deletes a file.
- path_join()Joins two filesystem paths together.
- update_attached_file()Updates attachment file path based on attachment ID.
- _wp_relative_upload_path()Returns relative path to an uploaded file.
- wp_update_attachment_metadata()Updates metadata for an attachment.
- update_post_meta()Updates a post meta field based on the given post ID.
- delete_post_meta()Deletes a post meta field for the given post ID.
Show all 13
- stdClass::__construct()
Used by · 1
- wp_ajax_image_editor()Handles image editing via AJAX.
Source code
function wp_restore_image( $post_id ) { $meta = wp_get_attachment_metadata( $post_id ); $file = get_attached_file( $post_id ); $backup_sizes = get_post_meta( $post_id, '_wp_attachment_backup_sizes', true ); $old_backup_sizes = $backup_sizes; $restored = false; $msg = new stdClass(); if ( ! is_array( $meta ) || ! is_array( $backup_sizes ) ) { $msg->error = __( 'Cannot load image metadata.' ); return $msg; } $meta['sizes'] ??= array(); $parts = pathinfo( $file ); $suffix = time() . rand( 100, 999 ); $default_sizes = get_intermediate_image_sizes(); if ( isset( $backup_sizes['full-orig'] ) && is_array( $backup_sizes['full-orig'] ) ) { $data = $backup_sizes['full-orig']; if ( $parts['basename'] !== $data['file'] ) { if ( defined( 'IMAGE_EDIT_OVERWRITE' ) && IMAGE_EDIT_OVERWRITE ) { // Delete only if it's an edited image. if ( preg_match( '/-e[0-9]{13}\./', $parts['basename'] ) ) { wp_delete_file( $file ); } } elseif ( isset( $meta['width'], $meta['height'] ) ) { $backup_sizes[ "full-$suffix" ] = array( 'width' => $meta['width'], 'height' => $meta['height'], 'filesize' => $meta['filesize'], 'file' => $parts['basename'], ); } } $restored_file = path_join( $parts['dirname'], $data['file'] ); $restored = update_attached_file( $post_id, $restored_file ); $meta['file'] = _wp_relative_upload_path( $restored_file ); $meta['width'] = $data['width']; $meta['height'] = $data['height']; if ( isset( $data['filesize'] ) ) { /* * Restore the original filesize if it was backed up. * * See https://core.trac.wordpress.org/ticket/59684. */ $meta['filesize'] = $data['filesize']; } } foreach ( $default_sizes as $default_size ) { if ( isset( $backup_sizes[ "$default_size-orig" ] ) ) { $data = $backup_sizes[ "$default_size-orig" ]; if ( isset( $meta['sizes'][ $default_size ] ) && $meta['sizes'][ $default_size ]['file'] !== $data['file'] ) { if ( defined( 'IMAGE_EDIT_OVERWRITE' ) && IMAGE_EDIT_OVERWRITE ) { // Delete only if it's an edited image. if ( preg_match( '/-e[0-9]{13}-/', $meta['sizes'][ $default_size ]['file'] ) ) { $delete_file = path_join( $parts['dirname'], $meta['sizes'][ $default_size ]['file'] ); wp_delete_file( $delete_file ); } } else { $backup_sizes[ "$default_size-{$suffix}" ] = $meta['sizes'][ $default_size ]; } } $meta['sizes'][ $default_size ] = $data; } else { unset( $meta['sizes'][ $default_size ] ); } } if ( ! wp_update_attachment_metadata( $post_id, $meta ) || ( $old_backup_sizes !== $backup_sizes && ! update_post_meta( $post_id, '_wp_attachment_backup_sizes', $backup_sizes ) ) ) { $msg->error = __( 'Cannot save image metadata.' );Changelog
Introduced in 2.9.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 7.1.0 tag, from
src/wp-admin/includes/image-edit.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.