admin-commands.js
193 lines
| 1 | /** |
| 2 | * Admin Commands |
| 3 | * |
| 4 | * Core WordPress commands for Secure Custom Fields administration. |
| 5 | * This file registers navigation commands for all primary SCF admin screens, |
| 6 | * enabling quick access through the WordPress commands interface (Cmd+K / Ctrl+K). |
| 7 | * |
| 8 | * @since SCF 6.5.0 |
| 9 | */ |
| 10 | |
| 11 | /** |
| 12 | * WordPress dependencies |
| 13 | */ |
| 14 | import { __ } from '@wordpress/i18n'; |
| 15 | import { dispatch, select } from '@wordpress/data'; |
| 16 | import { addQueryArgs } from '@wordpress/url'; |
| 17 | import { |
| 18 | layout, |
| 19 | plus, |
| 20 | postList, |
| 21 | category, |
| 22 | settings, |
| 23 | tool, |
| 24 | upload, |
| 25 | download, |
| 26 | } from '@wordpress/icons'; |
| 27 | |
| 28 | /** |
| 29 | * Register admin commands for SCF |
| 30 | */ |
| 31 | const registerAdminCommands = () => { |
| 32 | if ( ! select( 'core/commands') || ! dispatch( 'core/commands' ) ) { |
| 33 | return; |
| 34 | } |
| 35 | |
| 36 | const registeredCommands = select( 'core/commands' ).getCommands(); |
| 37 | const commandStore = dispatch( 'core/commands' ); |
| 38 | |
| 39 | const viewCommands = [ |
| 40 | { |
| 41 | name: 'field-groups', |
| 42 | label: __( 'Field Groups', 'secure-custom-fields' ), |
| 43 | url: 'edit.php', |
| 44 | urlArgs: { post_type: 'acf-field-group' }, |
| 45 | icon: layout, |
| 46 | keywords: [ |
| 47 | 'acf', |
| 48 | 'custom fields', |
| 49 | 'field editor', |
| 50 | 'manage fields', |
| 51 | ], |
| 52 | }, |
| 53 | { |
| 54 | name: 'post-types', |
| 55 | label: __( 'Post Types', 'secure-custom-fields' ), |
| 56 | url: 'edit.php', |
| 57 | urlArgs: { post_type: 'acf-post-type' }, |
| 58 | icon: postList, |
| 59 | keywords: [ 'cpt', 'content types', 'manage post types' ], |
| 60 | }, |
| 61 | { |
| 62 | name: 'taxonomies', |
| 63 | label: __( 'Taxonomies', 'secure-custom-fields' ), |
| 64 | url: 'edit.php', |
| 65 | urlArgs: { post_type: 'acf-taxonomy' }, |
| 66 | icon: category, |
| 67 | keywords: [ 'categories', 'tags', 'terms', 'custom taxonomies' ], |
| 68 | }, |
| 69 | { |
| 70 | name: 'options-pages', |
| 71 | label: __( 'Options Pages', 'secure-custom-fields' ), |
| 72 | url: 'edit.php', |
| 73 | urlArgs: { post_type: 'acf-ui-options-page' }, |
| 74 | icon: settings, |
| 75 | keywords: [ 'settings', 'global options', 'site options' ], |
| 76 | }, |
| 77 | { |
| 78 | name: 'tools', |
| 79 | label: __( 'SCF Tools', 'secure-custom-fields' ), |
| 80 | url: 'admin.php', |
| 81 | urlArgs: { page: 'acf-tools' }, |
| 82 | icon: tool, |
| 83 | keywords: [ 'utilities', 'import export', 'json' ], |
| 84 | }, |
| 85 | { |
| 86 | name: 'import', |
| 87 | label: __( 'Import SCF Data', 'secure-custom-fields' ), |
| 88 | url: 'admin.php', |
| 89 | urlArgs: { page: 'acf-tools', tool: 'import' }, |
| 90 | icon: upload, |
| 91 | keywords: [ 'upload', 'json', 'migration', 'transfer' ], |
| 92 | }, |
| 93 | { |
| 94 | name: 'export', |
| 95 | label: __( 'Export SCF Data', 'secure-custom-fields' ), |
| 96 | url: 'admin.php', |
| 97 | urlArgs: { page: 'acf-tools', tool: 'export' }, |
| 98 | icon: download, |
| 99 | keywords: [ 'download', 'json', 'backup', 'migration' ], |
| 100 | }, |
| 101 | ]; |
| 102 | |
| 103 | // Create commands - not in SCF's admin menu, so not duplicated. |
| 104 | const createCommands = [ |
| 105 | { |
| 106 | name: 'new-field-group', |
| 107 | label: __( 'Create New Field Group', 'secure-custom-fields' ), |
| 108 | url: 'post-new.php', |
| 109 | urlArgs: { post_type: 'acf-field-group' }, |
| 110 | icon: plus, |
| 111 | keywords: [ |
| 112 | 'add', |
| 113 | 'new', |
| 114 | 'create', |
| 115 | 'field group', |
| 116 | 'custom fields', |
| 117 | ], |
| 118 | }, |
| 119 | { |
| 120 | name: 'new-post-type', |
| 121 | label: __( 'Create New Post Type', 'secure-custom-fields' ), |
| 122 | url: 'post-new.php', |
| 123 | urlArgs: { post_type: 'acf-post-type' }, |
| 124 | icon: plus, |
| 125 | keywords: [ 'add', 'new', 'create', 'cpt', 'content type' ], |
| 126 | }, |
| 127 | { |
| 128 | name: 'new-taxonomy', |
| 129 | label: __( 'Create New Taxonomy', 'secure-custom-fields' ), |
| 130 | url: 'post-new.php', |
| 131 | urlArgs: { post_type: 'acf-taxonomy' }, |
| 132 | icon: plus, |
| 133 | keywords: [ |
| 134 | 'add', |
| 135 | 'new', |
| 136 | 'create', |
| 137 | 'taxonomy', |
| 138 | 'categories', |
| 139 | 'tags', |
| 140 | ], |
| 141 | }, |
| 142 | { |
| 143 | name: 'new-options-page', |
| 144 | label: __( 'Create New Options Page', 'secure-custom-fields' ), |
| 145 | url: 'post-new.php', |
| 146 | urlArgs: { post_type: 'acf-ui-options-page' }, |
| 147 | icon: plus, |
| 148 | keywords: [ 'add', 'new', 'create', 'options', 'settings page' ], |
| 149 | }, |
| 150 | ]; |
| 151 | |
| 152 | const registerCommand = ( command ) => { |
| 153 | commandStore.registerCommand( { |
| 154 | name: 'scf/' + command.name, |
| 155 | label: command.label, |
| 156 | icon: command.icon, |
| 157 | keywords: command.keywords, |
| 158 | callback: ( { close } ) => { |
| 159 | document.location = addQueryArgs( |
| 160 | command.url, |
| 161 | command.urlArgs |
| 162 | ); |
| 163 | close(); |
| 164 | }, |
| 165 | } ); |
| 166 | }; |
| 167 | |
| 168 | // WordPress 6.9+ adds Command Palette commands for all admin menu items. |
| 169 | // For older versions, we need to register them manually. The most reliable way to |
| 170 | // detect this is to check if the commands are already registered. |
| 171 | viewCommands.forEach( ( command ) => { |
| 172 | const commandUrl = addQueryArgs( command.url, command.urlArgs ); |
| 173 | // WordPress stores destination URLs in the command *name*, appended to |
| 174 | // the menu slug (which is also a relative URL), resulting in somewhat |
| 175 | // peculiar naming, e.g. |
| 176 | // edit.php?post_type=acf-field-group-edit.php?post_type=acf-ui-options-page |
| 177 | if ( registeredCommands.some( ( cmd ) => cmd.name.endsWith( commandUrl ) ) ) { |
| 178 | return; |
| 179 | } |
| 180 | registerCommand( command ); |
| 181 | } ); |
| 182 | |
| 183 | // "Create New" commands are not automatically registered by WordPress, |
| 184 | // so we always register them. |
| 185 | createCommands.forEach( registerCommand ); |
| 186 | }; |
| 187 | |
| 188 | if ( 'requestIdleCallback' in window ) { |
| 189 | window.requestIdleCallback( registerAdminCommands, { timeout: 500 } ); |
| 190 | } else { |
| 191 | setTimeout( registerAdminCommands, 500 ); |
| 192 | } |
| 193 |