|
| 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 { createElement } from '@wordpress/element'; |
| 16 | +import { Icon } from '@wordpress/components'; |
| 17 | +import { dispatch } from '@wordpress/data'; |
| 18 | +import { addQueryArgs } from '@wordpress/url'; |
| 19 | + |
| 20 | +/** |
| 21 | + * Register admin commands for SCF |
| 22 | + */ |
| 23 | +const registerAdminCommands = () => { |
| 24 | + if ( ! dispatch( 'core/commands' ) || ! window.acf?.data ) { |
| 25 | + return; |
| 26 | + } |
| 27 | + |
| 28 | + const commandStore = dispatch( 'core/commands' ); |
| 29 | + const adminUrl = window.acf?.data?.admin_url || ''; |
| 30 | + |
| 31 | + const commands = [ |
| 32 | + { |
| 33 | + name: 'field-groups', |
| 34 | + label: __( 'Field Groups', 'secure-custom-fields' ), |
| 35 | + url: 'edit.php', |
| 36 | + urlArgs: { post_type: 'acf-field-group' }, |
| 37 | + icon: 'layout', |
| 38 | + description: __( |
| 39 | + 'SCF: View and manage custom field groups', |
| 40 | + 'secure-custom-fields' |
| 41 | + ), |
| 42 | + keywords: [ |
| 43 | + 'acf', |
| 44 | + 'custom fields', |
| 45 | + 'field editor', |
| 46 | + 'manage fields', |
| 47 | + ], |
| 48 | + }, |
| 49 | + { |
| 50 | + name: 'new-field-group', |
| 51 | + label: __( 'Create New Field Group', 'secure-custom-fields' ), |
| 52 | + url: 'post-new.php', |
| 53 | + urlArgs: { post_type: 'acf-field-group' }, |
| 54 | + icon: 'plus', |
| 55 | + description: __( |
| 56 | + 'SCF: Create a new field group to organize custom fields', |
| 57 | + 'secure-custom-fields' |
| 58 | + ), |
| 59 | + keywords: [ |
| 60 | + 'add', |
| 61 | + 'new', |
| 62 | + 'create', |
| 63 | + 'field group', |
| 64 | + 'custom fields', |
| 65 | + ], |
| 66 | + }, |
| 67 | + { |
| 68 | + name: 'post-types', |
| 69 | + label: __( 'Post Types', 'secure-custom-fields' ), |
| 70 | + url: 'edit.php', |
| 71 | + urlArgs: { post_type: 'acf-post-type' }, |
| 72 | + icon: 'admin-post', |
| 73 | + description: __( |
| 74 | + 'SCF: Manage custom post types', |
| 75 | + 'secure-custom-fields' |
| 76 | + ), |
| 77 | + keywords: [ 'cpt', 'content types', 'manage post types' ], |
| 78 | + }, |
| 79 | + { |
| 80 | + name: 'new-post-type', |
| 81 | + label: __( 'Create New Post Type', 'secure-custom-fields' ), |
| 82 | + url: 'post-new.php', |
| 83 | + urlArgs: { post_type: 'acf-post-type' }, |
| 84 | + icon: 'plus', |
| 85 | + description: __( |
| 86 | + 'SCF: Create a new custom post type', |
| 87 | + 'secure-custom-fields' |
| 88 | + ), |
| 89 | + keywords: [ 'add', 'new', 'create', 'cpt', 'content type' ], |
| 90 | + }, |
| 91 | + { |
| 92 | + name: 'taxonomies', |
| 93 | + label: __( 'Taxonomies', 'secure-custom-fields' ), |
| 94 | + url: 'edit.php', |
| 95 | + urlArgs: { post_type: 'acf-taxonomy' }, |
| 96 | + icon: 'category', |
| 97 | + description: __( |
| 98 | + 'SCF: Manage custom taxonomies for organizing content', |
| 99 | + 'secure-custom-fields' |
| 100 | + ), |
| 101 | + keywords: [ 'categories', 'tags', 'terms', 'custom taxonomies' ], |
| 102 | + }, |
| 103 | + { |
| 104 | + name: 'new-taxonomy', |
| 105 | + label: __( 'Create New Taxonomy', 'secure-custom-fields' ), |
| 106 | + url: 'post-new.php', |
| 107 | + urlArgs: { post_type: 'acf-taxonomy' }, |
| 108 | + icon: 'plus', |
| 109 | + description: __( |
| 110 | + 'SCF: Create a new custom taxonomy', |
| 111 | + 'secure-custom-fields' |
| 112 | + ), |
| 113 | + keywords: [ |
| 114 | + 'add', |
| 115 | + 'new', |
| 116 | + 'create', |
| 117 | + 'taxonomy', |
| 118 | + 'categories', |
| 119 | + 'tags', |
| 120 | + ], |
| 121 | + }, |
| 122 | + { |
| 123 | + name: 'options-pages', |
| 124 | + label: __( 'Options Pages', 'secure-custom-fields' ), |
| 125 | + url: 'edit.php', |
| 126 | + urlArgs: { post_type: 'acf-ui-options-page' }, |
| 127 | + icon: 'admin-settings', |
| 128 | + description: __( |
| 129 | + 'SCF: Manage custom options pages for global settings', |
| 130 | + 'secure-custom-fields' |
| 131 | + ), |
| 132 | + keywords: [ 'settings', 'global options', 'site options' ], |
| 133 | + }, |
| 134 | + { |
| 135 | + name: 'new-options-page', |
| 136 | + label: __( 'Create New Options Page', 'secure-custom-fields' ), |
| 137 | + url: 'post-new.php', |
| 138 | + urlArgs: { post_type: 'acf-ui-options-page' }, |
| 139 | + icon: 'plus', |
| 140 | + description: __( |
| 141 | + 'SCF: Create a new custom options page', |
| 142 | + 'secure-custom-fields' |
| 143 | + ), |
| 144 | + keywords: [ 'add', 'new', 'create', 'options', 'settings page' ], |
| 145 | + }, |
| 146 | + { |
| 147 | + name: 'tools', |
| 148 | + label: __( 'SCF Tools', 'secure-custom-fields' ), |
| 149 | + url: 'admin.php', |
| 150 | + urlArgs: { page: 'acf-tools' }, |
| 151 | + icon: 'admin-tools', |
| 152 | + description: __( |
| 153 | + 'SCF: Access SCF utility tools', |
| 154 | + 'secure-custom-fields' |
| 155 | + ), |
| 156 | + keywords: [ 'utilities', 'import export', 'json' ], |
| 157 | + }, |
| 158 | + { |
| 159 | + name: 'import', |
| 160 | + label: __( 'Import SCF Data', 'secure-custom-fields' ), |
| 161 | + url: 'admin.php', |
| 162 | + urlArgs: { page: 'acf-tools', tool: 'import' }, |
| 163 | + icon: 'upload', |
| 164 | + description: __( |
| 165 | + 'SCF: Import field groups, post types, taxonomies, and options pages', |
| 166 | + 'secure-custom-fields' |
| 167 | + ), |
| 168 | + keywords: [ 'upload', 'json', 'migration', 'transfer' ], |
| 169 | + }, |
| 170 | + { |
| 171 | + name: 'export', |
| 172 | + label: __( 'Export SCF Data', 'secure-custom-fields' ), |
| 173 | + url: 'admin.php', |
| 174 | + urlArgs: { page: 'acf-tools', tool: 'export' }, |
| 175 | + icon: 'download', |
| 176 | + description: __( |
| 177 | + 'SCF: Export field groups, post types, taxonomies, and options pages', |
| 178 | + 'secure-custom-fields' |
| 179 | + ), |
| 180 | + keywords: [ 'download', 'json', 'backup', 'migration' ], |
| 181 | + }, |
| 182 | + ]; |
| 183 | + |
| 184 | + commands.forEach( ( command ) => { |
| 185 | + commandStore.registerCommand( { |
| 186 | + name: 'scf/' + command.name, |
| 187 | + label: command.label, |
| 188 | + icon: createElement( Icon, { icon: command.icon } ), |
| 189 | + context: 'admin', |
| 190 | + description: command.description, |
| 191 | + keywords: command.keywords, |
| 192 | + callback: ( { close } ) => { |
| 193 | + document.location = command.urlArgs |
| 194 | + ? addQueryArgs( adminUrl + command.url, command.urlArgs ) |
| 195 | + : adminUrl + command.url; |
| 196 | + close(); |
| 197 | + }, |
| 198 | + } ); |
| 199 | + } ); |
| 200 | +}; |
| 201 | + |
| 202 | +if ( 'requestIdleCallback' in window ) { |
| 203 | + window.requestIdleCallback( registerAdminCommands, { timeout: 500 } ); |
| 204 | +} else { |
| 205 | + setTimeout( registerAdminCommands, 500 ); |
| 206 | +} |
0 commit comments