-
Notifications
You must be signed in to change notification settings - Fork 43
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
Merged
Merged
Changes from 30 commits
Commits
Show all changes
41 commits
Select commit
Hold shift + click to select a range
5215215
First attempt at adding command palette support
priethor d6006fe
Command polishing
priethor 4ec2af9
Add commands for registered CPTs
priethor 18e250f
Refactor
priethor 77bdff1
Refactor to use wp.scf
priethor b8894cf
Refactor
priethor 6fce2a9
Refactor to pass the data in acf.data.customPostTypes
priethor f28d69d
Fix redirections
priethor 6e4431c
Check capabilities before registering comands
priethor 65485ac
Filter post types that don't enable the setting "show in UI"
priethor b06b8a7
Redo webpack config linting
priethor f5a6cba
Redo webpack linting
priethor 4c6f5da
Apply linting
priethor c592beb
Polish
priethor 3907d63
Refactor
priethor 60b8184
Fix coding standards in comments
priethor af46e4a
Ensure we only register the commands when the palette is available
priethor b2d95ab
Refactor to remove scf and acf prefixes, organizing files in folders …
priethor 101de27
Optimize return early
priethor 4b6a953
Remove unnecessary script registration
priethor 1a62407
Use the `scf` prefix in admin commands.
priethor 47c480c
Use `scf` prefix for CPT commands
priethor 9bed553
Only pass CPTs to the frontend when the array is not empty
priethor dfa0104
CPT commands: only add label as a keyword if not empty
priethor 54176c5
Polish
priethor a23722c
Refactor the depencies to user import statements
priethor 887f2cf
Optimize scripts loading and execution
priethor 9944ea2
Update package-lock.json
priethor 4def381
Slight refactor
priethor 1df01fe
Assets: remove redundant registrations, prefix registered scritps
priethor ccb0edc
Remove redundant admin check
priethor b5f9199
Remove unnecesary checking of `acf_get_acf_post_types`
priethor 71342ac
Change the priority queue to a simpler `requestIdleCallback`
priethor 57ea58c
Update assets/src/js/commands/custom-post-type-commands.js
priethor 7f57f51
Improved URL building
priethor 3f99a4e
Fix command labels for better i18n
priethor ff6bb99
Simplify label calculation leveraging WP core functions
priethor 3b547c3
Remove redundant client-side label calculation
priethor 2af1415
Merge remote-tracking branch 'origin/trunk' into add/command-palette-…
priethor 857b84f
Fixed `@since` annotations to follow the steps of #129
priethor 85cd929
Switch to using the proper labels for the commands
priethor File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,193 @@ | ||
| /** | ||
| * Admin Commands | ||
| * | ||
| * Core WordPress commands for Secure Custom Fields administration. | ||
| * This file registers navigation commands for all primary SCF admin screens, | ||
| * enabling quick access through the WordPress commands interface (Cmd+K / Ctrl+K). | ||
| * | ||
| * @since 6.5.0 | ||
| */ | ||
|
|
||
| /** | ||
| * WordPress dependencies | ||
| */ | ||
| import { __ } from '@wordpress/i18n'; | ||
| import { createElement } from '@wordpress/element'; | ||
| import { Icon } from '@wordpress/components'; | ||
| import { dispatch } from '@wordpress/data'; | ||
| import { createQueue } from '@wordpress/priority-queue'; | ||
|
|
||
| /** | ||
| * Initialize deferred execution queue and context | ||
| */ | ||
| const queue = createQueue(); | ||
| const context = {}; | ||
|
|
||
| /** | ||
| * Register admin commands for SCF | ||
| */ | ||
| queue.add( context, () => { | ||
| if ( ! dispatch( 'core/commands' ) || ! window.acf?.data ) { | ||
| return; | ||
| } | ||
|
|
||
| const commandStore = dispatch( 'core/commands' ); | ||
| const adminUrl = window.acf?.data?.admin_url || ''; | ||
priethor marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| const commands = [ | ||
| { | ||
| name: 'field-groups', | ||
| label: __( 'Field Groups', 'secure-custom-fields' ), | ||
| url: 'edit.php?post_type=acf-field-group', | ||
| icon: 'layout', | ||
| description: __( | ||
| 'SCF: View and manage custom field groups', | ||
| 'secure-custom-fields' | ||
| ), | ||
| keywords: [ | ||
| 'acf', | ||
| 'custom fields', | ||
| 'field editor', | ||
| 'manage fields', | ||
| ], | ||
| }, | ||
| { | ||
| name: 'new-field-group', | ||
| label: __( 'Create New Field Group', 'secure-custom-fields' ), | ||
| url: 'post-new.php?post_type=acf-field-group', | ||
| icon: 'plus', | ||
| description: __( | ||
| 'SCF: Create a new field group to organize custom fields', | ||
| 'secure-custom-fields' | ||
| ), | ||
| keywords: [ | ||
| 'add', | ||
| 'new', | ||
| 'create', | ||
| 'field group', | ||
| 'custom fields', | ||
| ], | ||
| }, | ||
| { | ||
| name: 'post-types', | ||
| label: __( 'Post Types', 'secure-custom-fields' ), | ||
| url: 'edit.php?post_type=acf-post-type', | ||
| icon: 'admin-post', | ||
| description: __( | ||
| 'SCF: Manage custom post types', | ||
| 'secure-custom-fields' | ||
| ), | ||
| keywords: [ 'cpt', 'content types', 'manage post types' ], | ||
| }, | ||
| { | ||
| name: 'new-post-type', | ||
| label: __( 'Create New Post Type', 'secure-custom-fields' ), | ||
| url: 'post-new.php?post_type=acf-post-type', | ||
| icon: 'plus', | ||
| description: __( | ||
| 'SCF: Create a new custom post type', | ||
| 'secure-custom-fields' | ||
| ), | ||
| keywords: [ 'add', 'new', 'create', 'cpt', 'content type' ], | ||
| }, | ||
| { | ||
| name: 'taxonomies', | ||
| label: __( 'Taxonomies', 'secure-custom-fields' ), | ||
| url: 'edit.php?post_type=acf-taxonomy', | ||
| icon: 'category', | ||
| description: __( | ||
| 'SCF: Manage custom taxonomies for organizing content', | ||
| 'secure-custom-fields' | ||
| ), | ||
| keywords: [ 'categories', 'tags', 'terms', 'custom taxonomies' ], | ||
| }, | ||
| { | ||
| name: 'new-taxonomy', | ||
| label: __( 'Create New Taxonomy', 'secure-custom-fields' ), | ||
| url: 'post-new.php?post_type=acf-taxonomy', | ||
| icon: 'plus', | ||
| description: __( | ||
| 'SCF: Create a new custom taxonomy', | ||
| 'secure-custom-fields' | ||
| ), | ||
| keywords: [ | ||
| 'add', | ||
| 'new', | ||
| 'create', | ||
| 'taxonomy', | ||
| 'categories', | ||
| 'tags', | ||
| ], | ||
| }, | ||
| { | ||
| name: 'options-pages', | ||
| label: __( 'Options Pages', 'secure-custom-fields' ), | ||
| url: 'edit.php?post_type=acf-ui-options-page', | ||
| icon: 'admin-settings', | ||
| description: __( | ||
| 'SCF: Manage custom options pages for global settings', | ||
| 'secure-custom-fields' | ||
| ), | ||
| keywords: [ 'settings', 'global options', 'site options' ], | ||
| }, | ||
| { | ||
| name: 'new-options-page', | ||
| label: __( 'Create New Options Page', 'secure-custom-fields' ), | ||
| url: 'post-new.php?post_type=acf-ui-options-page', | ||
| icon: 'plus', | ||
| description: __( | ||
| 'SCF: Create a new custom options page', | ||
| 'secure-custom-fields' | ||
| ), | ||
| keywords: [ 'add', 'new', 'create', 'options', 'settings page' ], | ||
| }, | ||
| { | ||
| name: 'tools', | ||
| label: __( 'SCF Tools', 'secure-custom-fields' ), | ||
| url: 'admin.php?page=acf-tools', | ||
| icon: 'admin-tools', | ||
| description: __( | ||
| 'SCF: Access SCF utility tools', | ||
| 'secure-custom-fields' | ||
| ), | ||
| keywords: [ 'utilities', 'import export', 'json' ], | ||
| }, | ||
| { | ||
| name: 'import', | ||
| label: __( 'Import SCF Data', 'secure-custom-fields' ), | ||
| url: 'admin.php?page=acf-tools&tool=import', | ||
| icon: 'upload', | ||
| description: __( | ||
| 'SCF: Import field groups, post types, taxonomies, and options pages', | ||
| 'secure-custom-fields' | ||
| ), | ||
| keywords: [ 'upload', 'json', 'migration', 'transfer' ], | ||
| }, | ||
| { | ||
| name: 'export', | ||
| label: __( 'Export SCF Data', 'secure-custom-fields' ), | ||
| url: 'admin.php?page=acf-tools&tool=export', | ||
| icon: 'download', | ||
| description: __( | ||
| 'SCF: Export field groups, post types, taxonomies, and options pages', | ||
| 'secure-custom-fields' | ||
| ), | ||
| keywords: [ 'download', 'json', 'backup', 'migration' ], | ||
| }, | ||
| ]; | ||
|
|
||
| commands.forEach( ( command ) => { | ||
| commandStore.registerCommand( { | ||
| name: 'scf/' + command.name, | ||
| label: command.label, | ||
| icon: createElement( Icon, { icon: command.icon } ), | ||
| context: 'admin', | ||
| description: command.description, | ||
| keywords: command.keywords, | ||
| callback: ( { close } ) => { | ||
| document.location = adminUrl + command.url; | ||
| close(); | ||
| }, | ||
| } ); | ||
| } ); | ||
| } ); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| /** | ||
| * Custom Post Type Commands | ||
| * | ||
| * Dynamic commands for user-created custom post types in Secure Custom Fields. | ||
| * This file generates navigation commands for each registered post type that | ||
| * the current user has access to, creating both "View All" and "Add New" commands. | ||
| * | ||
| * Post type data is provided via acf.data.customPostTypes, which is populated | ||
| * by the PHP side after capability checks ensure the user has appropriate access. | ||
| * | ||
| * @since 6.5.0 | ||
| */ | ||
|
|
||
| /** | ||
| * WordPress dependencies | ||
| */ | ||
| import { __, sprintf } from '@wordpress/i18n'; | ||
| import { createElement } from '@wordpress/element'; | ||
| import { Icon } from '@wordpress/components'; | ||
| import { dispatch } from '@wordpress/data'; | ||
| import { createQueue } from '@wordpress/priority-queue'; | ||
|
|
||
| /** | ||
| * Initialize deferred execution queue and context | ||
| */ | ||
| const queue = createQueue(); | ||
| const context = {}; | ||
|
|
||
| /** | ||
| * Register custom post type commands | ||
| */ | ||
| queue.add( context, () => { | ||
| // Only proceed when WordPress commands API and there are custom post types accessible | ||
| if ( | ||
| ! dispatch( 'core/commands' ) || | ||
| ! window.acf?.data?.customPostTypes?.length | ||
| ) { | ||
| return; | ||
| } | ||
|
|
||
| const commandStore = dispatch( 'core/commands' ); | ||
| const adminUrl = window.acf.data.admin_url || ''; | ||
| const postTypes = window.acf.data.customPostTypes; | ||
|
|
||
| postTypes.forEach( ( postType ) => { | ||
| // Skip invalid post types | ||
| if ( ! postType?.name ) { | ||
| return; | ||
| } | ||
|
|
||
| const pluralLabel = postType.label || postType.name; | ||
| const singularLabel = postType.singular_label || pluralLabel; | ||
priethor marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| // Register "View All" command for this post type | ||
| commandStore.registerCommand( { | ||
| name: `scf/cpt-${ postType.name }`, | ||
| label: pluralLabel, | ||
| icon: createElement( Icon, { icon: 'admin-page' } ), | ||
| context: 'admin', | ||
| description: | ||
| /* translators: %s: Post type plural label */ | ||
| sprintf( | ||
| __( 'SCF: View all %s', 'secure-custom-fields' ), | ||
priethor marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| pluralLabel | ||
| ), | ||
| keywords: [ | ||
| 'post type', | ||
| 'content', | ||
| 'cpt', | ||
| postType.name, | ||
| ...( postType.label ? [ postType.label ] : [] ), | ||
| ], | ||
priethor marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| callback: ( { close } ) => { | ||
| document.location = | ||
| adminUrl + | ||
| `edit.php?post_type=${ encodeURIComponent( | ||
| postType.name | ||
| ) }`; | ||
| close(); | ||
| }, | ||
| } ); | ||
|
|
||
| // Register "Add New" command for this post type | ||
| commandStore.registerCommand( { | ||
| name: `scf/new-${ postType.name }`, | ||
| label: | ||
| /* translators: %s: Post type singular label */ | ||
| sprintf( | ||
| __( 'Add New %s', 'secure-custom-fields' ), | ||
priethor marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| singularLabel | ||
| ), | ||
| icon: createElement( Icon, { icon: 'plus' } ), | ||
| context: 'admin', | ||
| description: | ||
| /* translators: %s: Post type singular label */ | ||
| sprintf( | ||
| __( 'SCF: Create a new %s', 'secure-custom-fields' ), | ||
priethor marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| singularLabel | ||
| ), | ||
| keywords: [ | ||
| 'add', | ||
| 'new', | ||
| 'create', | ||
| 'content', | ||
| postType.name, | ||
| ...( postType.label ? [ postType.label ] : [] ), | ||
priethor marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ], | ||
| callback: ( { close } ) => { | ||
| document.location = | ||
| adminUrl + | ||
| `post-new.php?post_type=${ encodeURIComponent( | ||
| postType.name | ||
| ) }`; | ||
| close(); | ||
| }, | ||
| } ); | ||
| } ); | ||
| } ); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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
requestIdleCallbackdirectly instead of using a queue with a single callback.Uh oh!
There was an error while loading. Please reload this page.
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
deferis 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
requestIdleCallbackhere before reading your answer. Should I get rid of that, too? It still seems overkill.