-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathadmin-commands.php
80 lines (69 loc) · 2.61 KB
/
admin-commands.php
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
<?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() {
// Ensure we only load our commands where the WordPress commands API is available.
if ( ! wp_script_is( 'wp-commands', 'registered' ) ) {
return;
}
$custom_post_types = array();
$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'] ?? '',
);
}
}
if ( ! empty( $custom_post_types ) ) {
acf_localize_data(
array(
'customPostTypes' => $custom_post_types,
)
);
wp_enqueue_script( 'scf-commands-custom-post-types' );
}
// Only load admin commands if user has SCF admin capabilities.
if ( current_user_can( acf_get_setting( 'capability' ) ) ) {
wp_enqueue_script( 'scf-commands-admin' );
}
}
add_action( 'admin_enqueue_scripts', 'acf_commands_init' );