-
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
DataViews Extensibility: Add a hook to allow third-party scripts to register/unregister post type actions #64138
Conversation
…egister/unregister post type actions
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. |
One small (or not) performance optimization in this PR is that the actions are only registered when needed, they are not globally registered anymore. |
Size Change: +4.12 kB (+0.23%) Total Size: 1.77 MB
ℹ️ View Unchanged
|
031cbe2
to
f41e562
Compare
Overall, it’s a common problem that we have with plugins which often run before the editor registers its default handlers, the same applies to block types, block variations. I was thinking again about replicating: add_action( ‘init’, ‘someCallback’ ); Today, we offer in the documentation a workaround with wp.domReady( someCallback ); Example: removing registered block types. However, it’d definitely help to have it more formalized. It’s only the question whether we introduce specific actions like in this PR, mix that with some general purpose: wp.hooks.addAction( ‘editor/init’, ‘someCallback’ ); Or we offer both. Concluding, I’m very much in favor of the proposed approach. I’m also happy to discuss further the overall strategy for other extensibility points that addresses the developer experience hurdles when plugins run code to early by introducing familiar patterns as they have to use in PHP that ensure the code runs at the right time. |
Pros here:
Cons:
I think it's the right approach though, we've been accumulating these global registration functions and the limit is showing. |
Can you share more details regarding that aspect? Do you mean the problem that some plugins can run after the Can we avoid it by implementing a similar solution to One good aspect would be that when |
const { registerPostTypeActions } = unlock( useDispatch( editorStore ) ); | ||
useEffect( () => { | ||
registerPostTypeActions( postType ); | ||
}, [ registerPostTypeActions, postType ] ); |
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 don't love these kinds of effects tbh. Not sure what the alternative is, I guess filtered context wouldn't work.
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.
This is for me a good thing: Only register the actions and run the filter when needed (when using usePostAction hook), that said, I agree that the code is weird, but I don't know of another pattern that provides the same benefits.
} ); | ||
} ); | ||
|
||
doAction( 'core.registerPostTypeActions', Promise.resolve(), postType ); |
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.
Could our action registration use the same hook?
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 can see two sides here:
- Core code is not special and should use the same thing.
- Adds complexity: figuring out when to register the action, which package and also no special treatment for performance...
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.
What are the changes to isReady for?
The |
I drafted PR to keep the discussion going: |
Related #61084
What?
In #62052 an API to register and unregister dataviews actions has been implemented. But in order to allow third-party developers to be able to unregister these actions properly we need to offer a hook that third-party can use to "time" their register/unregister function calls properly. This PR does so by introducing the
core.registerPostTypeActions
hook to do so.So something like:
Another goal of this PR is to allow us (core) to use the "post type" object to register/unregister actions. For instance "viewPost" action need to only be register for post types with is_viewable as true...
Testing Instructions
Check that the post actions dropdown work properly in both the dataviews and the editor.