-
Notifications
You must be signed in to change notification settings - Fork 4.5k
useSaveImage: handle missing editMediaEntity gracefully #70967
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
useSaveImage: handle missing editMediaEntity gracefully #70967
Conversation
- Updated the retrieval of editMediaEntity to use optional chaining. - Added error handling to notify users when no edit media entity action is found during image editing.
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.
To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
Size Change: +57 B (0%) Total Size: 1.89 MB
ℹ️ View Unchanged
|
__( | ||
'Could not edit image. No edit media entity action found.' | ||
), |
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.
In which cases will editMediaEntity
not be available? Is there a more user-friendly error message we can provide? I'm mostly thinking that "edit media entity action" makes sense if you understand the internals of Gutenberg, but probably won't to an end user.
Otherwise, I like the idea of catching early and displaying an error notice!
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.
In which cases will editMediaEntity not be available?
When upload permissions aren't available on the user capabilities.
Maybe we could be upfront and describe the permissions fail like the backend does?
"Could not edit image. Sorry, you are not allowed to upload media on this site."
Or
"Could not edit image. Sorry, you are not allowed to edit media on this site."
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, I like that idea, any of those options sound fine to me. If we want to re-use a string that exists on the backend, there's also Sorry, you are not allowed to edit this item.
which is slightly shorter. But I have no real preference other than using simple(ish) language 🙂
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.
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.
Prettier! My arch enemy! Fixed
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.
Thanks, @ramonjd!
P.S. You can also use WP CLI to remove caps and then reset.
wp cap remove editor upload_files
wp role reset editor
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.
LGTM too!
Also, thanks @Mamaduka — I just noticed that remove_cap
persists in the database and was scratching my head for a second how to reset quickly 👍
Thanks for the helpful reviews @andrewserong and @Mamaduka 🙇🏻 |
) Fix useSaveImage to handle missing editMediaEntity gracefully - Updated the retrieval of editMediaEntity to use optional chaining. - Added error handling to notify users when no edit media entity action is found during image editing. Co-authored-by: ramonjd <ramonopoly@git.wordpress.org> Co-authored-by: andrewserong <andrewserong@git.wordpress.org> Co-authored-by: Mamaduka <mamaduka@git.wordpress.org>
What and how and why?
In #70814 a new private action
editMediaEntity
was added to Core Data. The action acts as an interface toWP_REST_Attachments_Controller REST
API'swp/v2/media/{ id }/edit route
.editMediaEntity
is only accessible in block editor settings if the user has upload permissions. This matches the backend's permission settings: https://github.com/WordPress/wordpress-develop/blob/trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php#L531Because some users might not have upload permissions, this PR adds a check to
editMediaEntity
and optional chaining in the selector.If
editMediaEntity
is not available, an error notification is shown.Shout out to @Mamaduka for the nudge
Testing Instructions
First, test that you can successfully crop and image as admin or editor.
Then remove your upload files permissions, e.g.,
Check that the error shows and nothing else borks.
Screenshots or screencast