-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Core Data: Resolve entity collection user permissions #64504
Conversation
Flaky tests detected in 9fe7bd6. 🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/10398462601
|
Size Change: +1.18 kB (+0.07%) Total Size: 1.77 MB
ℹ️ View Unchanged
|
packages/core-data/src/resolvers.js
Outdated
for ( const targetHint of targetHints ) { | ||
for ( const action of ALLOWED_RESOURCE_ACTIONS ) { | ||
const permissionKey = getUserPermissionCacheKey( | ||
action, | ||
{ kind, name, id: targetHint.id } | ||
); | ||
|
||
dispatch.receiveUserPermission( | ||
permissionKey, | ||
targetHint.permissions[ action ] | ||
); | ||
dispatch.finishResolution( 'canUser', [ | ||
action, | ||
{ kind, name, id: targetHint.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.
A bit crude, but it does its job 😄
@WordPress/gutenberg-core, I think this is ready for early reviews/feedback 🙇 |
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. |
98e9287
to
357dfaf
Compare
* @param WP_REST_Response $response Response to extract links from. | ||
* @return array Map of link relation to list of link hashes. | ||
*/ | ||
public static function get_response_links( $response ) { |
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.
Any tests for this? When this should get into core, there need to be tests.
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.
100%. I would like to do that, though I'm unsure what the best approach is. Maybe @TimothyBJacobs has some suggestions.
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.
I pushed a commit with some tests.
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.
I like this approach a lot. Avoids all the extra requests. Would love some validation of the REST API endpoint changes though. I don't feel like my voice is enough for something that can be adopted for all endpoints.
function gutenberg_override_default_rest_server() { | ||
return 'Gutenberg_REST_Server'; | ||
} | ||
add_filter( 'wp_rest_server_class', 'gutenberg_override_default_rest_server' ); |
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.
Won't this break any pre-existing code that declares their own custom WP_REST_Server
?
Example usage in existing plugin directory: https://wpdirectory.net/search/01J5R1B8MEEW01RJ3WZDC2XPY8
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.
The same is true for every REST API override in the Gutenberg plugin. Come with the project that builds a new WP in a plugin.
We can use a low-priority filter here (0
or 1
) and prioritize other integrations, but this would mean not shipping enhancement for those plugins.
As far as I know, server overrides should be rare in plugins unless they're shipping their custom server.
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.
A low priority sounds like a good compromise. Hopefully, there will be little to no overlap.
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.
I updated the hook priority. I also checked most of the plugins from the directory results, and none of them actually override the server class. But there will be some cases that aren't part of the plugin directory.
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.
Better be safe than sorry. Nice to see that most of these plugin directory instances were actually tests.
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.
Haven't done extensive testing, just yet. Will do, but wanted to discuss something first.
My biggest concern is around overriding the default REST server, which some plugins already do, and by overriding it ourselves, we may potentially break those plugins.
* @return array Map of link relation to list of link hashes. | ||
*/ | ||
// @core-merge: Do not merge. The method is copied here to fix the inheritance issue. | ||
public static function get_compact_response_links( $response ) { |
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.
Is there a good reason to keep these methods static?
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.
I think they've been static since REST API got merged in the core. They need to remain static; otherwise, we'll break backward compatibility.
P.S. I had to copy two extra methods because of how the inheritance of self
vs static
keywords works. See: https://www.php.net/manual/en/language.oop5.late-static-bindings.php.
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.
Yep, exactly that.
return array(); | ||
} | ||
|
||
$server = rest_get_server(); |
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.
If this method is not static
, $server
can literally be $this
, no?
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.
I think we need an instantiated server here (which rest_get_server
provides) and not a reference to the current class.
I copied this from WordPress/wordpress-develop#7139, so I might be bit wrong about reasoning 😅
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.
Right, this is not a new method, but an overriede of an existing Core method which is static
. So this needs to stay static
, hence why we get access to the server via rest_get_server()
.
Thanks for adding the unit test, @TimothyBJacobs 🙇 I think this is ready to land. Can I get final approval? |
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.
REST API changes look good to me.
It is nice to see clear performance improvements on the CodeVitals page. |
Co-authored-by: Mamaduka <mamaduka@git.wordpress.org> Co-authored-by: TimothyBJacobs <timothyblynjacobs@git.wordpress.org> Co-authored-by: swissspidy <swissspidy@git.wordpress.org> Co-authored-by: youknowriad <youknowriad@git.wordpress.org> Co-authored-by: tyxla <tyxla@git.wordpress.org> Co-authored-by: spacedmonkey <spacedmonkey@git.wordpress.org>
What?
Track: https://core.trac.wordpress.org/ticket/61739
Based on: WordPress/wordpress-develop#7139
PR updates
WP_REST_Sever
to add thetargetHints
property to the link description object as part of the self-link. ThetargetHints
provides info about actions the user is able to perform for each item.It also updates the
getEntityRecords
resolver to bulk update user permissions for fetched collection items.Why?
Consumers who render a list of posts and CRUD actions related to each item must make a separate OPTIONS request to check if the user has permission to perform these actions.
The N+1 requests affect the performance on the client and the server.
Testing Instructions
wp-admin/site-editor.php?postType=page&layout=table
.OPTIONS
requests.Testing Instructions for Keyboard
Same.
Screenshots or screencast
Data Views > Pages
Before
After
Data Views > Templates
Before
After