1212 * WordPress dependencies
1313 */
1414import { __ } from '@wordpress/i18n' ;
15- import { dispatch } from '@wordpress/data' ;
15+ import { dispatch , select } from '@wordpress/data' ;
1616import { addQueryArgs } from '@wordpress/url' ;
1717import {
1818 layout ,
@@ -29,10 +29,11 @@ import {
2929 * Register admin commands for SCF
3030 */
3131const registerAdminCommands = ( ) => {
32- if ( ! dispatch ( 'core/commands' ) || ! window . acf ?. data ) {
32+ if ( ! select ( 'core/commands' ) || ! dispatch ( 'core/commands' ) ) {
3333 return ;
3434 }
3535
36+ const registeredCommands = select ( 'core/commands' ) . getCommands ( ) ;
3637 const commandStore = dispatch ( 'core/commands' ) ;
3738
3839 const viewCommands = [
@@ -166,13 +167,22 @@ const registerAdminCommands = () => {
166167 } ;
167168
168169 // WordPress 6.9+ adds Command Palette commands for all admin menu items.
169- const wpVersion = window . acf . data . wp_version ;
170- const isWp69Plus =
171- wpVersion . localeCompare ( '6.9' , undefined , { numeric : true } ) >= 0 ;
170+ // For older versions, we need to register them manually. The most reliable way to
171+ // detect this is to check if the commands are already registered.
172+ viewCommands . forEach ( ( command ) => {
173+ const commandUrl = addQueryArgs ( command . url , command . urlArgs ) ;
174+ // WordPress stores destination URLs in the command *name*, appended to
175+ // the menu slug (which is also a relative URL), resulting in somewhat
176+ // peculiar naming, e.g.
177+ // edit.php?post_type=acf-field-group-edit.php?post_type=acf-ui-options-page
178+ if ( registeredCommands . some ( ( cmd ) => cmd . name . endsWith ( commandUrl ) ) ) {
179+ return ;
180+ }
181+ registerCommand ( command ) ;
182+ } ) ;
172183
173- if ( ! isWp69Plus ) {
174- viewCommands . forEach ( registerCommand ) ;
175- }
184+ // "Create New" commands are not automatically registered by WordPress,
185+ // so we always register them.
176186 createCommands . forEach ( registerCommand ) ;
177187} ;
178188
0 commit comments