Advertisements

wp_restore_image

Definition:
function wp_restore_image($post_id) {}

Parameters

  • $post_id

Defined filters

  • intermediate_image_sizes
    apply_filters( 'intermediate_image_sizes', array('large', 'medium', 'thumbnail')
  • wp_delete_file
    apply_filters('wp_delete_file', $file)
  • wp_delete_file
    apply_filters( 'wp_delete_file', path_join($parts['dirname'], $meta['sizes'][$default_size]['file'])

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 );

	$restored = false;

	$msg = new stdClass;



	if ( !is_array($backup_sizes) ) {

		$msg->error = __('Cannot load image metadata.');

		return $msg;

	}



	$parts = pathinfo($file);

	$suffix = time() . rand(100, 999);

	$default_sizes = apply_filters( 'intermediate_image_sizes', array('large', 'medium', 'thumbnail') );



	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 edited image

				if ( preg_match('/-e[0-9]{13}\./', $parts['basename']) ) {

					$delpath = apply_filters('wp_delete_file', $file);

					@unlink($delpath);

				}

			} else {

				$backup_sizes["full-$suffix"] = array('width' => $meta['width'], 'height' => $meta['height'], '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'];

		list ( $uwidth, $uheight ) = wp_constrain_dimensions($meta['width'], $meta['height'], 128, 96);

		$meta['hwstring_small'] = "height='$uheight' width='$uwidth'";

	}



	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 edited image

					if ( preg_match('/-e[0-9]{13}-/', $meta['sizes'][$default_size]['file']) ) {

						$delpath = apply_filters( 'wp_delete_file', path_join($parts['dirname'], $meta['sizes'][$default_size]['file']) );

						@unlink($delpath);

					}

				} else {

					$backup_sizes["$default_size-{$suffix}"] = $meta['sizes'][$default_size];

				}

			}



			$meta['sizes'][$default_size] = $data;

		} else {

			unset($meta['sizes'][$default_size]);

		}

	}

4061

Advertisements

No comments yet... Be the first to leave a reply!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: