Skip to content
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

Preserve image attributes when cropping a custom logo image #2441

Conversation

anton-vlasenko
Copy link

@anton-vlasenko anton-vlasenko commented Mar 21, 2022

This PR aims to implement preserving attributes when cropping a custom logo image.

Steps to test this PR: https://core.trac.wordpress.org/ticket/37750#comment:26

Trac ticket: https://core.trac.wordpress.org/ticket/37750


This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.

Copy link
Contributor

@dream-encode dream-encode left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

More to address here in the actual logic, but these are the things that jump out at me straight away.

src/wp-admin/includes/ajax-actions.php Outdated Show resolved Hide resolved
src/wp-admin/includes/ajax-actions.php Outdated Show resolved Hide resolved
tests/phpunit/tests/ajax/CropImage.php Outdated Show resolved Hide resolved
tests/phpunit/tests/ajax/CropImage.php Outdated Show resolved Hide resolved
tests/phpunit/tests/ajax/CropImage.php Outdated Show resolved Hide resolved
tests/phpunit/tests/ajax/CropImage.php Outdated Show resolved Hide resolved
Copy link
Contributor

@hellofromtonya hellofromtonya left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall looks good. Some changes for consistency.

src/wp-admin/includes/ajax-actions.php Outdated Show resolved Hide resolved
src/wp-admin/includes/ajax-actions.php Outdated Show resolved Hide resolved
src/wp-admin/includes/ajax-actions.php Outdated Show resolved Hide resolved
tests/phpunit/tests/ajax/CropImage.php Outdated Show resolved Hide resolved
tests/phpunit/tests/ajax/CropImage.php Outdated Show resolved Hide resolved
tests/phpunit/tests/ajax/CropImage.php Outdated Show resolved Hide resolved
tests/phpunit/tests/ajax/CropImage.php Outdated Show resolved Hide resolved
tests/phpunit/tests/ajax/CropImage.php Outdated Show resolved Hide resolved
tests/phpunit/tests/ajax/CropImage.php Outdated Show resolved Hide resolved
tests/phpunit/tests/ajax/CropImage.php Outdated Show resolved Hide resolved
$use_original_title = (
'' !== trim( $original_attachment->post_title ) &&
// Check if the original image's title was edited.
pathinfo( $parent_basename, PATHINFO_FILENAME ) !== $original_attachment->post_title
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@anton-vlasenko I added this check to only use the original image's title if it was edited. When there's no title in the image's metadata, by default the image's filename is set as the post_title. This check is making sure the post_title is not the same as that default behavior. If no, then it will use the original image's title.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The tests will need updating though.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!
I'm working on updating the tests.

We have to call sanitize_file_name to escape $original_attachment->post_title, otherwise this comparison will not work as intended.
E.g. post_title can have spaces, but they get replaced with dashes in the filename.
Also, sometimes post_title contains file extension, sometimes it doesn't.
We have to call sanitize_file_name to escape $original_attachment->post_title, otherwise this comparison will not work as intended.
E.g. post_title can have spaces, but they get replaced with dashes in the filename.
@anton-vlasenko anton-vlasenko changed the title Fix/attachment properties are not copied over to the cropped image Preserve image attributes when cropping a custom logo image Mar 24, 2022
Copy link
Contributor

@hellofromtonya hellofromtonya left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The implementation and tests LGTM 👍
Approving - pending input from @felixarntz (see context in the Trac ticket comment).

Copy link
Member

@felixarntz felixarntz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks great @anton-vlasenko! I left two very minor comments for your consideration, although they certainly don't need to block this from getting merged. Please take a look.

@hellofromtonya I've left a comment in https://core.trac.wordpress.org/ticket/37750#comment:33 as well.

Comment on lines +4002 to +4005
// Copy the image caption attribute (post_excerpt field) from the original image.
if ( '' !== trim( $original_attachment->post_excerpt ) ) {
$object['post_excerpt'] = $original_attachment->post_excerpt;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bit of a nit-pick, but I think it would make sense to include this immediately in the associative array above, right after post_title and post_content, which would clarify the relationship of the three when reading the code. The default for this would need to be the empty string (''), which is the default for wp_insert_post (and thus wp_insert_attachment).

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the feedback.
I intentionally didn't include it in the associative array.
Why? I prefer not to specify a default value if the task does not require that.
I prefer to let wp_insert_attachment decide what value to use as default.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a fair point. Works for me 👍

Comment on lines 4010 to 4013
// Copy the image alt text attribute from the original image.
if ( '' !== trim( $original_attachment->_wp_attachment_image_alt ) ) {
update_post_meta( $attachment_id, '_wp_attachment_image_alt', wp_slash( $original_attachment->_wp_attachment_image_alt ) );
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similarly to my above comment, we could integrate this into the associative array passed to wp_insert_attachment above, which would colocate it with the other other related data. Other than in my previous comment though, here it would be more about avoiding an extra call to update_post_meta.

We can use the meta_input key (supported by wp_insert_post and thus also by wp_insert_attachment) to pass this information, basically like $object['meta_input'] = array( '_wp_attachment_image_alt' => wp_slash( $original_attachment->_wp_attachment_image_alt ) ).

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I absolutely agree. I didn't know wp_insert_post supports the meta_input key.
Fixed in 9e2a262
cc @hellofromtonya
I've implemented the change suggested in @felixarntz`s comment above. I tested it manually and it worked as intended. Unit tests also passed.
But if that change will require retesting all use cases manually, please let me know, and I will revert that change.
I also don't want to block this PR from getting merged.

@anton-vlasenko anton-vlasenko force-pushed the fix/attachment-properties-are-not-copied-over-to-the-cropped-image branch from d8f5c63 to 9e2a262 Compare March 25, 2022 14:53
@hellofromtonya
Copy link
Contributor

Committed via changeset https://core.trac.wordpress.org/changeset/53027.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
4 participants