-
Notifications
You must be signed in to change notification settings - Fork 39
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 19 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,179 @@ | ||
| /** | ||
| * 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 | ||
| */ | ||
|
|
||
| wp.domReady( () => { | ||
| if ( ! wp.data?.dispatch?.( 'core/commands' ) || ! acf?.data ) { | ||
priethor marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return; | ||
| } | ||
|
|
||
| const { __ } = wp.i18n; | ||
| const { createElement } = wp.element; | ||
| const { Icon } = wp.components; | ||
| const commandStore = wp.data.dispatch( 'core/commands' ); | ||
priethor marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| const adminUrl = acf?.data?.admin_url || ''; | ||
|
|
||
| const commands = [ | ||
| { | ||
| name: 'field-groups', | ||
| label: __( 'Field Groups', 'secure-custom-fields' ), | ||
| url: `${ adminUrl }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: `${ adminUrl }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: `${ adminUrl }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: `${ adminUrl }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: `${ adminUrl }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: `${ adminUrl }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: `${ adminUrl }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: `${ adminUrl }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: `${ adminUrl }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: `${ adminUrl }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: `${ adminUrl }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: 'acf/' + command.name, | ||
priethor marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| label: command.label, | ||
| icon: createElement( Icon, { icon: command.icon } ), | ||
| context: 'admin', | ||
| description: command.description, | ||
| keywords: command.keywords, | ||
| callback: ( { close } ) => { | ||
| document.location = command.url; | ||
priethor marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| 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,99 @@ | ||
| /** | ||
| * 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 | ||
| */ | ||
|
|
||
| wp.domReady( () => { | ||
| // Only proceed when WordPress commands API and there are custom post types accessible | ||
| if ( | ||
| ! wp.data?.dispatch?.( 'core/commands' ) || | ||
| ! acf?.data?.customPostTypes?.length | ||
| ) { | ||
| return; | ||
| } | ||
|
|
||
| const { __, sprintf } = wp.i18n; | ||
| const { createElement } = wp.element; | ||
| const { Icon } = wp.components; | ||
| const commandStore = wp.data.dispatch( 'core/commands' ); | ||
| const adminUrl = acf.data.admin_url || ''; | ||
| const postTypes = 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: `acf/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 || '', | ||
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: `acf/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 || '', | ||
| ], | ||
| callback: ( { close } ) => { | ||
| document.location = `${ adminUrl }post-new.php?post_type=${ encodeURIComponent( | ||
| postType.name | ||
| ) }`; | ||
| 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,88 @@ | ||
| <?php | ||
| /** | ||
| * SCF Commands Integration | ||
| * | ||
| * @package Secure Custom Fields | ||
| */ | ||
|
|
||
| if ( ! defined( 'ABSPATH' ) ) { | ||
| exit; // Exit if accessed directly. | ||
| } | ||
|
|
||
| /** | ||
| * Initializes SCF commands integration | ||
| * | ||
| * This function handles the integration with WordPress Commands (Cmd+K / Ctrl+K), | ||
| * providing navigation commands for SCF admin pages and custom post types. | ||
| * | ||
| * The implementation follows these principles: | ||
| * 1. Only loads in screens where WordPress commands are available. | ||
| * 2. Performs capability checks to ensure users only see commands they can access. | ||
| * 3. Core administrative commands are only shown to users with SCF admin capabilities. | ||
| * 4. Custom post type commands are conditionally shown based on edit_posts capability | ||
| * for each specific post type. | ||
| * 5. Post types must have UI enabled (show_ui setting) to appear in commands. | ||
| * | ||
| * @since 6.5.0 | ||
| */ | ||
| function acf_commands_init() { | ||
| // Only load on admin screens. | ||
| if ( ! is_admin() ) { | ||
youknowriad marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return; | ||
| } | ||
|
|
||
| // Ensure we only load our commands where the WordPress commands API is available. | ||
| if ( ! wp_script_is( 'wp-commands', 'registered' ) ) { | ||
priethor marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return; | ||
| } | ||
|
|
||
| $custom_post_types = array(); | ||
|
|
||
| if ( function_exists( 'acf_get_acf_post_types' ) ) { | ||
youknowriad marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| $scf_post_types = acf_get_acf_post_types(); | ||
|
|
||
| foreach ( $scf_post_types as $post_type ) { | ||
| // Skip if post type name is not set (defensive) or post type is inactive. | ||
| if ( empty( $post_type['post_type'] ) || ( isset( $post_type['active'] ) && ! $post_type['active'] ) ) { | ||
| continue; | ||
| } | ||
|
|
||
| $plural_label = $post_type['labels']['name'] ?? $post_type['label'] ?? $post_type['post_type']; | ||
| $singular_label = $post_type['labels']['singular_name'] ?? $post_type['singular_label'] ?? $plural_label; | ||
|
|
||
| $post_type_obj = get_post_type_object( $post_type['post_type'] ); | ||
|
|
||
| // Three conditions must be met to include this post type in the commands: | ||
| // 1. Post type object must exist | ||
| // 2. Current user must have permission to edit posts of this type. | ||
| // 3. Post type must have admin UI enabled (show_ui setting). | ||
| if ( $post_type_obj && | ||
| current_user_can( $post_type_obj->cap->edit_posts ) && | ||
| $post_type_obj->show_ui ) { | ||
| $custom_post_types[] = array( | ||
| 'name' => $post_type['post_type'], | ||
| 'label' => $plural_label, | ||
| 'singular_label' => $singular_label, | ||
| 'icon' => $post_type['menu_icon'] ?? '', | ||
| ); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| acf_localize_data( | ||
| array( | ||
| 'customPostTypes' => $custom_post_types, | ||
| ) | ||
| ); | ||
|
|
||
| if ( ! empty( $custom_post_types ) ) { | ||
| wp_enqueue_script( 'commands-custom-post-types' ); | ||
| } | ||
priethor marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| // Only load admin commands if user has SCF admin capabilities. | ||
| if ( current_user_can( acf_get_setting( 'capability' ) ) ) { | ||
| wp_enqueue_script( 'commands-admin' ); | ||
| } | ||
| } | ||
|
|
||
| add_action( 'admin_enqueue_scripts', 'acf_commands_init' ); | ||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.