Fix image meta after editing image, disable the Save button when no changes, fixes #11115

git-svn-id: http://svn.automattic.com/wordpress/trunk@12163 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
azaozz 2009-11-11 11:24:01 +00:00
parent fb1bb44d60
commit fb0d8b44ee
6 changed files with 82 additions and 59 deletions

View File

@ -68,7 +68,7 @@ function wp_image_editor($post_id, $msg = false) {
<div class="imgedit-submit">
<input type="button" onclick="imageEdit.close(<?php echo $post_id; ?>, 1)" class="button" value="<?php echo esc_attr__( 'Cancel' ); ?>" />
<input type="button" onclick="imageEdit.save(<?php echo "$post_id, '$nonce'"; ?>)" class="button-primary imgedit-submit-btn" value="<?php echo esc_attr__( 'Save' ); ?>" />
<input type="button" onclick="imageEdit.save(<?php echo "$post_id, '$nonce'"; ?>)" disabled="disabled" class="button-primary imgedit-submit-btn" value="<?php echo esc_attr__( 'Save' ); ?>" />
</div>
</td>
@ -92,7 +92,12 @@ function wp_image_editor($post_id, $msg = false) {
<div class="imgedit-group-top">
<a class="imgedit-help-toggle" onclick="imageEdit.toggleHelp(this);return false;" href="#"><strong><?php _e('Restore Original Image'); ?></strong></a>
<div class="imgedit-help">
<p><?php _e('Discard any changes and restore the original image. Previously edited copies of the image will not be deleted.'); ?></p>
<p><?php _e('Discard any changes and restore the original image.');
if ( !defined('IMAGE_EDIT_OVERWRITE') || !IMAGE_EDIT_OVERWRITE )
_e(' Previously edited copies of the image will not be deleted.');
?></p>
<div class="imgedit-submit">
<input type="button" onclick="imageEdit.action(<?php echo "$post_id, '$nonce'"; ?>, 'restore')" class="button-primary" value="<?php echo esc_attr__( 'Restore image' ); ?>" <?php echo $can_restore; ?> />
</div>
@ -409,6 +414,7 @@ function stream_preview_image($post_id) {
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 = '';
@ -418,49 +424,51 @@ function wp_restore_image($post_id) {
return $msg;
}
$parts = pathinfo($meta['file']);
$parts = pathinfo($file);
$suffix = time() . rand(100, 999);
$default_sizes = apply_filters( 'intermediate_image_sizes', array('large', 'medium', 'thumbnail') );
$default_sizes[] = 'full';
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_shrink_dimensions($meta['width'], $meta['height']);
$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 ( 'full' == $default_size ) {
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', $meta['file']);
@unlink($delpath);
}
} else {
$backup_sizes["full-$suffix"] = array('width' => $meta['width'], 'height' => $meta['height'], 'file' => $parts['basename']);
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['file'] = path_join($parts['dirname'], $data['file']);
$meta['width'] = $data['width'];
$meta['height'] = $data['height'];
list ( $uwidth, $uheight ) = wp_shrink_dimensions($meta['width'], $meta['height']);
$meta['hwstring_small'] = "height='$uheight' width='$uwidth'";
$restored = update_attached_file($post_id, $meta['file']);
} else {
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;
}
$meta['sizes'][$default_size] = $data;
} else {
unset($meta['sizes'][$default_size]);
}
@ -580,7 +588,8 @@ function wp_save_image($post_id) {
$backup_sizes[$tag] = array('width' => $meta['width'], 'height' => $meta['height'], 'file' => $path_parts['basename']);
$success = update_attached_file($post_id, $new_path);
$meta['file'] = get_attached_file($post_id, true); // get the path unfiltered
$meta['file'] = _wp_relative_upload_path($new_path);
$meta['width'] = imagesx($img);
$meta['height'] = imagesy($img);

View File

@ -88,35 +88,27 @@ function wp_generate_attachment_metadata( $attachment_id, $file ) {
$metadata = array();
if ( preg_match('!^image/!', get_post_mime_type( $attachment )) && file_is_displayable_image($file) ) {
$full_path_file = $file;
$imagesize = getimagesize( $full_path_file );
$imagesize = getimagesize( $file );
$metadata['width'] = $imagesize[0];
$metadata['height'] = $imagesize[1];
list($uwidth, $uheight) = wp_shrink_dimensions($metadata['width'], $metadata['height']);
$metadata['hwstring_small'] = "height='$uheight' width='$uwidth'";
// Make the file path relative to the upload dir
if ( ($uploads = wp_upload_dir()) && false === $uploads['error'] ) { // Get upload directory
if ( 0 === strpos($file, $uploads['basedir']) ) {// Check that the upload base exists in the file path
$file = str_replace($uploads['basedir'], '', $file); // Remove upload dir from the file path
$file = ltrim($file, '/');
}
}
$metadata['file'] = $file;
$metadata['file'] = _wp_relative_upload_path($file);
// make thumbnails and other intermediate sizes
$sizes = array('thumbnail', 'medium', 'large');
$sizes = apply_filters('intermediate_image_sizes', $sizes);
$sizes = apply_filters( 'intermediate_image_sizes', array('thumbnail', 'medium', 'large') );
foreach ($sizes as $size) {
$resized = image_make_intermediate_size( $full_path_file, get_option("{$size}_size_w"), get_option("{$size}_size_h"), get_option("{$size}_crop") );
$resized = image_make_intermediate_size( $file, get_option("{$size}_size_w"), get_option("{$size}_size_h"), get_option("{$size}_crop") );
if ( $resized )
$metadata['sizes'][$size] = $resized;
}
// fetch additional metadata from exif/iptc
$image_meta = wp_read_image_metadata( $full_path_file );
if ($image_meta)
$image_meta = wp_read_image_metadata( $file );
if ( $image_meta )
$metadata['image_meta'] = $image_meta;
}

View File

@ -175,6 +175,11 @@ imageEdit = {
if ( (typeof callback != "unknown") && callback != null )
callback();
if ( $('#imgedit-history-' + postid).val() && $('#imgedit-undone-' + postid).val() == 0 )
$('input.imgedit-submit-btn', '#imgedit-panel-' + postid).removeAttr('disabled');
else
$('input.imgedit-submit-btn', '#imgedit-panel-' + postid).attr('disabled', 'disabled');
t.toggleEditor(postid, 0);
}).attr('src', ajaxurl + '?' + $.param(data));
},

File diff suppressed because one or more lines are too long

View File

@ -68,16 +68,33 @@ function update_attached_file( $attachment_id, $file ) {
return false;
$file = apply_filters( 'update_attached_file', $file, $attachment_id );
$file = _wp_relative_upload_path($file);
// Make the file path relative to the upload dir
if ( ($uploads = wp_upload_dir()) && false === $uploads['error'] ) { // Get upload directory
if ( 0 === strpos($file, $uploads['basedir']) ) {// Check that the upload base exists in the file path
$file = str_replace($uploads['basedir'], '', $file); // Remove upload dir from the file path
$file = ltrim($file, '/');
return update_post_meta( $attachment_id, '_wp_attached_file', $file );
}
/**
* Return relative path to an uploaded file.
*
* The path is relative to the current upload dir.
*
* @since 2.9
* @uses apply_filters() Calls '_wp_relative_upload_path' on file path.
*
* @param string $path Full path to the file
* @return string relative path on success, unchanged path on failure.
*/
function _wp_relative_upload_path( $path ) {
$new_path = $path;
if ( ($uploads = wp_upload_dir()) && false === $uploads['error'] ) {
if ( 0 === strpos($new_path, $uploads['basedir']) ) {
$new_path = str_replace($uploads['basedir'], '', $new_path);
$new_path = ltrim($new_path, '/');
}
}
return update_post_meta( $attachment_id, '_wp_attached_file', $file );
return apply_filters( '_wp_relative_upload_path', $new_path, $path );
}
/**

View File

@ -369,7 +369,7 @@ function wp_default_scripts( &$scripts ) {
$scripts->add( 'codepress', '/wp-includes/js/codepress/codepress.js', false, '0.9.6' );
$scripts->add_data( 'codepress', 'group', 1 );
$scripts->add( 'image-edit', "/wp-admin/js/image-edit$suffix.js", array('jquery', 'json2', 'imgareaselect'), '20090929' );
$scripts->add( 'image-edit', "/wp-admin/js/image-edit$suffix.js", array('jquery', 'json2', 'imgareaselect'), '20091111' );
$scripts->add_data( 'image-edit', 'group', 1 );
$scripts->add( 'set-post-thumbnail', "/wp-admin/js/set-post-thumbnail$suffix.js", array( 'jquery' ), '20091021' );