-
Notifications
You must be signed in to change notification settings - Fork 24
Add Command Palette support #121
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
base: trunk
Are you sure you want to change the base?
Conversation
Includes a refactor to divide commands between core and post-type based: core commands require admin permissions, whereas post-types are dynamic and depend on their own capabilities.
…similar to modules
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 left some initial general feedback before trying to better understand how it exactly works. I will try to test it next.
const { __ } = wp.i18n; | ||
const { createElement } = wp.element; | ||
const { Icon } = wp.components; | ||
const commandStore = wp.data.dispatch( 'core/commands' ); |
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.
It is possible to use import statements to let the build tackle detecting all dependencies. @cbravobernal added all necessary code to automate it. See:
description: command.description, | ||
keywords: command.keywords, | ||
callback: ( { close } ) => { | ||
document.location = command.url; |
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.
Nit: the following could help remove some duplication from the command config a bit:
document.location = command.url; | |
document.location = adminUrl + command.url; |
You can also divide the work into two PRs since it’s fully functional and tests well. Command Palette support doesn’t have to be a Beta Feature because it’s a straightforward enhancement that anyone familiar with the concept should appreciate. Follow-up work could include:
However, we need to take a closer look at the functionality, particularly the strategy for when these commands become available, depending on the WP version, the presence of custom post types, and user permissions. |
Co-authored-by: Greg Ziółkowski <grzegorz@gziolo.pl>
Co-authored-by: Greg Ziółkowski <grzegorz@gziolo.pl>
- Defer the script loading (defer was introduced in WP 6.3) - Defer script execution by using the priority queue (introduced in WP 5.0) Since the scripts will only be registered in versions with Command API support, which is 6.3+, we can use both
764103d
to
1df01fe
Compare
/** | ||
* Register admin commands for SCF | ||
*/ | ||
queue.add( context, () => { |
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.
It's not really clear to me why you're using "priority-queue" here? Did you actually notice performance issues or something. Also potentially you can just use requestIdleCallback
directly instead of using a queue with a single callback.
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 did not notice any performance difference; I implemented the priority queue in parallel to defer the loading here.
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.
Let's just get rid of it. I think defer
is enough.
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 simplified to a requestIdleCallback
here before reading your answer. Should I get rid of that, too? It still seems overkill.
} | ||
|
||
if ( ! empty( $custom_post_types ) ) { | ||
acf_localize_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.
Personally, I'd argue that we should move away from localized variables like that as much as possible and instead use REST API endpoints. Do SCF has endpoints to retrieve these things, can't we just rely on Core endpoints for post types...?
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 the only endpoints registered are the ones that fetch custom post types and post fields, in every field you can chose to include it or not in the WP default API endpoints for posts fetching, but there are no internal endpoints to retrieve all post types or all post fields created with the plugin.
They are instead localizing the data:
secure-custom-fields/includes/assets.php
Line 603 in 8b71adb
acf_localize_data( $data_to_localize ); |
acf_localize_data( |
Moving everything to a REST API could be done, but this needs to define a new API structure, with its own documentation and decide the approach for the store handling ( using Redux based like wp-data, or move to React Query or something similar )
What's your opinion on that @youknowriad ?
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 are two issues why using the Rest API would require some extra work and I didn't consider for this:
- While it does a PHP function to retrieve SCF Post types (
acf_get_acf_post_types
), it doesn't have a custom enpoint to do so, just WordPress` built in endpoints. - More importantly, in order to show CPTs in the REST API, each CPT needs to enable the
Show in REST API
setting.
Therefore, my guess is we will need custom SCF endpoints for the admin that show all, as we will encounter a similar issue when implementing DataViews. What do you think about this approach, Riad?
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.
We're on WordPress, so for me, we should be use the entities and core-data here. no need to duplicate things that exist.
More importantly, in order to show CPTs in the REST API, each CPT needs to enable the Show in REST API setting.
For me this is something that we should try to get rid of. Every CPT should be visible in the endpoints, I think this is a setting of another age.
That said, for now, a custom endpoint/entity could work if we're not ready to make all CPTs created by SCF show_in_rest true. Are there any valid reasons for not doing it?
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.
Are there any valid reasons for not doing it?
If by this you mean a custom endpoint, the only reason not to do it right now is the extra complexity it adds to this PR, we should probably tackle that separately.
As per the existing setting, I wouldn't change an existing setting as we don't know all the cases or real users (e.g., having CPTs only for the admin to manage private data), though.
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.
As per the existing setting, I wouldn't change an existing setting as we don't know all the cases or real users (e.g., having CPTs only for the admin to manage private data), though.
This is different than show_in_rest
, this should be mostly handled by the public
boolean.
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.
Thinking a bit more about this:
- Right now, we would need to use the
types
endpoint. show_in_rest
determines whether a custom post type gets its own endpoint, but also hides the CPT definition from thetypes
endpoint.- In any case, the
types
endpoint doesn't return any information to know whether a type is a CPT and/or created by SCF (e.g., nopost-type
field)
Therefore, my inclination is to create a new endpoint for custom post types that ignores the show_in_rest
. This will be needed, too, when migrating the Post Types screen to DataViews.
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 ideal scenario is to actually use the types
endpoint and add "metadata" to the post type. That said, I think it's a good interim solution to have a custom endpoint.
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 parallel to this PR, I wonder if we should implement this PR within SCF (before it gets into Core) to enable the command palette everywhere.
So for me, the PR is fine. But I think we should ask ourselves what we want to do with these post types and any SCF UI going forward. Do we want to decouple the server and client and have a consistent data layer to communicate between both (ideally on top of core-data) or do we want to continue with these localized scripts which for me feels like hacks. An investment in the core-data layer will help this PR but also any future PR that would like to rely on SCF entities in its client UI. |
I don't know if a hack, but it did feel like a dated approach that doesn't align with the current direction of core and modern architecture. I do agree with investing in decoupling server and client; the amount of data required in this PR is minimal, but as we move forward with new functionalities, it will pay off or even become necessary. How do you all feel about this PR with the current approach, and then follow up with the new endpoint implementation? |
I'm ok with this personally, but I would try to avoid the |
What
This PR adds support for WordPress Command Palette/Core Commands (Cmd+K / Ctrl+K interface) by registering:
Implementation details
The commands are divided into admin and CPT command files.
show_ui
settingTo prevent errors and optimize performance, JavaScript files for core and/or CPT commands will only be enqueued when needed.
Testing Instructions
editor
should only see CPT commandsRecording
Grabacion.de.pantalla.2025-04-25.a.las.13.55.21.mov
Followups
useCommandLoader
instead, which was introduced in WP 6.4.acf_localize_data