-
Notifications
You must be signed in to change notification settings - Fork 4.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix generating file names for edited files, and use wp_unique_filename() #23440
Conversation
Also some better errors and some cleanup of names.
Size Change: +1.62 kB (0%) Total Size: 1.13 MB
ℹ️ View Unchanged
|
|
||
if ( is_wp_error( $saved ) ) { | ||
return $saved; | ||
} | ||
|
||
// Update attachment details. | ||
// Create new attachment post. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we copy the rest of the attachment data?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, may make sense. But then we'll get two (or more) attachments with the same description and caption. In general don't think the image alt text should be carried over as alt has to be contextual. Editing an older, already used image would suggest it is being used in different context. Editing an image just after uploading it won't have alt text.
return new WP_Error( 'rest_cannot_find_attached_file', __( 'Unable to find original media file.', 'gutenberg' ), array( 'status' => 500 ) ); | ||
} | ||
if ( isset( $params['rotation'] ) ) { // TODO: use `rotate` instead? | ||
// CW / CCW |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Might be good to write it in full :)
|
||
$media_id = $params['media_id']; | ||
// This also confirms the attachment is an image. | ||
$image_file = wp_get_original_image_path( $attachment_id ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Might be good to leave a comment saying that this is the original uploaded file.
|
||
$filename = rtrim( dirname( $image_path ), '/' ) . '/' . $target_file; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Previously we were overriding the last edited file, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, kind of. Was pretty messy. Overwriting the last edited file if it is in the same uploads subdirectory, but not all sub-sizes of it. If the edited file was smaller, and not all sub-sizes were created, some of the old sub-sizes were left "orphaned".
This can be fixed but then we will either need to delete the attachment for the previously edited image, or reuse it and replace the image files and meta. Both of these are not good options if the previously edited image was used in a post.
Better workflow and UX would be to keep all edited files, perhaps let the user know they can delete unused images from the media library.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me! Just left some questions.
…at has not been edited.
- Move the check if the crop dimensions are > 0.1% to the js. - Test if the crop dimensions are whitin bounds in PHP. - Make crop values optional.
No need to look at `x` and `y`. If they have changed, the width or/and height have changed too. Also fix white space in (ugly) array alignment...
Co-authored-by: Timothy Jacobs <[email protected]>
Follow-up to #23284.
Description
Fixes few errors and inconsistencies when editing an already-edited image file, and when creating the new attachment post. Also adds some better errors and some cleanup of vars, names, etc. and still has few TBD/TODO.
How has this been tested?
When testing please monitor what files are created in the uploads dir (
wp-content/uploads/year/month/
).Checklist: