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