Skip to content

Commit 35037e6

Browse files
committed
Command Palette: Enqueue command-related assets for all admin pages.
Removes the core command initialization and registration code from the edit-post and edit-site packages and enqueues command-related assets globally. Props wildworks, rcorrales, shailu25, youknowriad. Fixes #63845. git-svn-id: https://develop.svn.wordpress.org/trunk@61022 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 891796d commit 35037e6

File tree

2 files changed

+97
-1
lines changed

2 files changed

+97
-1
lines changed

src/wp-includes/default-filters.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -592,6 +592,7 @@
592592
add_action( 'wp_enqueue_scripts', 'wp_enqueue_classic_theme_styles' );
593593
add_action( 'admin_enqueue_scripts', 'wp_localize_jquery_ui_datepicker', 1000 );
594594
add_action( 'admin_enqueue_scripts', 'wp_common_block_scripts_and_styles' );
595+
add_action( 'admin_enqueue_scripts', 'wp_enqueue_command_palette_assets' );
595596
add_action( 'enqueue_block_assets', 'wp_enqueue_classic_theme_styles' );
596597
add_action( 'enqueue_block_assets', 'wp_enqueue_registered_block_scripts_and_styles' );
597598
add_action( 'enqueue_block_assets', 'enqueue_block_styles_assets', 30 );

src/wp-includes/script-loader.php

Lines changed: 96 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1744,7 +1744,7 @@ function wp_default_styles( $styles ) {
17441744
'block-library' => array(),
17451745
'block-directory' => array(),
17461746
'components' => array(),
1747-
'commands' => array(),
1747+
'commands' => array( 'wp-components' ),
17481748
'edit-post' => array(
17491749
'wp-components',
17501750
'wp-block-editor',
@@ -3419,6 +3419,101 @@ function wp_enqueue_classic_theme_styles() {
34193419
}
34203420
}
34213421

3422+
/**
3423+
* Enqueues the assets required for the Command Palette.
3424+
*
3425+
* @since 6.9.0
3426+
*
3427+
* @global array $menu
3428+
* @global array $submenu
3429+
*/
3430+
function wp_enqueue_command_palette_assets() {
3431+
global $menu, $submenu;
3432+
3433+
$command_palette_settings = array();
3434+
3435+
if ( $menu ) {
3436+
$menu_commands = array();
3437+
foreach ( $menu as $menu_item ) {
3438+
if ( empty( $menu_item[0] ) || ! empty( $menu_item[1] ) && ! current_user_can( $menu_item[1] ) ) {
3439+
continue;
3440+
}
3441+
3442+
// Remove all HTML tags and their contents.
3443+
$menu_label = $menu_item[0];
3444+
while ( preg_match( '/<[^>]*>/', $menu_label ) ) {
3445+
$menu_label = preg_replace( '/<[^>]*>.*?<\/[^>]*>|<[^>]*\/>|<[^>]*>/s', '', $menu_label );
3446+
}
3447+
$menu_label = trim( $menu_label );
3448+
$menu_url = '';
3449+
$menu_slug = $menu_item[2];
3450+
3451+
if ( preg_match( '/\.php($|\?)/', $menu_slug ) || wp_http_validate_url( $menu_slug ) ) {
3452+
$menu_url = $menu_slug;
3453+
} elseif ( ! empty( menu_page_url( $menu_slug, false ) ) ) {
3454+
$menu_url = menu_page_url( $menu_slug, false );
3455+
}
3456+
3457+
if ( $menu_url ) {
3458+
$menu_commands[] = array(
3459+
'label' => $menu_label,
3460+
'url' => $menu_url,
3461+
'name' => $menu_slug,
3462+
);
3463+
}
3464+
3465+
if ( array_key_exists( $menu_slug, $submenu ) ) {
3466+
foreach ( $submenu[ $menu_slug ] as $submenu_item ) {
3467+
if ( empty( $submenu_item[0] ) || ! empty( $submenu_item[1] ) && ! current_user_can( $submenu_item[1] ) ) {
3468+
continue;
3469+
}
3470+
3471+
// Remove all HTML tags and their contents.
3472+
$submenu_label = $submenu_item[0];
3473+
while ( preg_match( '/<[^>]*>/', $submenu_label ) ) {
3474+
$submenu_label = preg_replace( '/<[^>]*>.*?<\/[^>]*>|<[^>]*\/>|<[^>]*>/s', '', $submenu_label );
3475+
}
3476+
$submenu_label = trim( $submenu_label );
3477+
$submenu_url = '';
3478+
$submenu_slug = $submenu_item[2];
3479+
3480+
if ( preg_match( '/\.php($|\?)/', $submenu_slug ) || wp_http_validate_url( $submenu_slug ) ) {
3481+
$submenu_url = $submenu_slug;
3482+
} elseif ( ! empty( menu_page_url( $submenu_slug, false ) ) ) {
3483+
$submenu_url = menu_page_url( $submenu_slug, false );
3484+
}
3485+
3486+
if ( $submenu_url ) {
3487+
$menu_commands[] = array(
3488+
'label' => sprintf(
3489+
/* translators: 1: Menu label, 2: Submenu label. */
3490+
__( '%1$s > %2$s' ),
3491+
$menu_label,
3492+
$submenu_label
3493+
),
3494+
'url' => $submenu_url,
3495+
'name' => $menu_slug . '-' . $submenu_item[2],
3496+
);
3497+
}
3498+
}
3499+
}
3500+
}
3501+
$command_palette_settings['menu_commands'] = $menu_commands;
3502+
}
3503+
3504+
wp_enqueue_script( 'wp-commands' );
3505+
wp_enqueue_style( 'wp-commands' );
3506+
wp_enqueue_script( 'wp-core-commands' );
3507+
3508+
wp_add_inline_script(
3509+
'wp-core-commands',
3510+
sprintf(
3511+
'wp.coreCommands.initializeCommandPalette( %s );',
3512+
wp_json_encode( $command_palette_settings, JSON_HEX_TAG | JSON_UNESCAPED_SLASHES )
3513+
)
3514+
);
3515+
}
3516+
34223517
/**
34233518
* Removes leading and trailing _empty_ script tags.
34243519
*

0 commit comments

Comments
 (0)