wp_restore_image

Restores the metadata for a given attachment.

Description

wp_restore_image( (int) $post_id ); 

Parameters (1)

0. $post_id (int)
Attachment post ID.

Usage

  1. if ( !function_exists( 'wp_restore_image' ) ) { 
  2. require_once ABSPATH . '/wp-admin/includes/image-edit.php'; 
  3.  
  4. // Attachment post ID. 
  5. $post_id = -1; 
  6.  
  7. // NOTICE! Understand what this does before running. 
  8. $result = wp_restore_image($post_id); 
  9.  

Defined (1)

The function is defined in the following location(s).

/wp-admin/includes/image-edit.php  
  1. function wp_restore_image($post_id) { 
  2. $meta = wp_get_attachment_metadata($post_id); 
  3. $file = get_attached_file($post_id); 
  4. $backup_sizes = $old_backup_sizes = get_post_meta( $post_id, '_wp_attachment_backup_sizes', true ); 
  5. $restored = false; 
  6. $msg = new stdClass; 
  7.  
  8. if ( !is_array($backup_sizes) ) { 
  9. $msg->error = __('Cannot load image metadata.'); 
  10. return $msg; 
  11.  
  12. $parts = pathinfo($file); 
  13. $suffix = time() . rand(100, 999); 
  14. $default_sizes = get_intermediate_image_sizes(); 
  15.  
  16. if ( isset($backup_sizes['full-orig']) && is_array($backup_sizes['full-orig']) ) { 
  17. $data = $backup_sizes['full-orig']; 
  18.  
  19. if ( $parts['basename'] != $data['file'] ) { 
  20. if ( defined('IMAGE_EDIT_OVERWRITE') && IMAGE_EDIT_OVERWRITE ) { 
  21.  
  22. // Delete only if it's an edited image. 
  23. if ( preg_match('/-e[0-9]{13}\./', $parts['basename']) ) { 
  24. wp_delete_file( $file ); 
  25. } elseif ( isset( $meta['width'], $meta['height'] ) ) { 
  26. $backup_sizes["full-$suffix"] = array('width' => $meta['width'], 'height' => $meta['height'], 'file' => $parts['basename']); 
  27.  
  28. $restored_file = path_join($parts['dirname'], $data['file']); 
  29. $restored = update_attached_file($post_id, $restored_file); 
  30.  
  31. $meta['file'] = _wp_relative_upload_path( $restored_file ); 
  32. $meta['width'] = $data['width']; 
  33. $meta['height'] = $data['height']; 
  34.  
  35. foreach ( $default_sizes as $default_size ) { 
  36. if ( isset($backup_sizes["$default_size-orig"]) ) { 
  37. $data = $backup_sizes["$default_size-orig"]; 
  38. if ( isset($meta['sizes'][$default_size]) && $meta['sizes'][$default_size]['file'] != $data['file'] ) { 
  39. if ( defined('IMAGE_EDIT_OVERWRITE') && IMAGE_EDIT_OVERWRITE ) { 
  40.  
  41. // Delete only if it's an edited image. 
  42. if ( preg_match('/-e[0-9]{13}-/', $meta['sizes'][$default_size]['file']) ) { 
  43. $delete_file = path_join( $parts['dirname'], $meta['sizes'][$default_size]['file'] ); 
  44. wp_delete_file( $delete_file ); 
  45. } else { 
  46. $backup_sizes["$default_size-{$suffix}"] = $meta['sizes'][$default_size]; 
  47.  
  48. $meta['sizes'][$default_size] = $data; 
  49. } else { 
  50. unset($meta['sizes'][$default_size]); 
  51.  
  52. if ( ! wp_update_attachment_metadata( $post_id, $meta ) || 
  53. ( $old_backup_sizes !== $backup_sizes && ! update_post_meta( $post_id, '_wp_attachment_backup_sizes', $backup_sizes ) ) ) { 
  54.  
  55. $msg->error = __('Cannot save image metadata.'); 
  56. return $msg; 
  57.  
  58. if ( !$restored ) 
  59. $msg->error = __('Image metadata is inconsistent.'); 
  60. else 
  61. $msg->msg = __('Image restored successfully.'); 
  62.  
  63. return $msg;