Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/wp-includes/default-filters.php
Original file line number Diff line number Diff line change
Expand Up @@ -592,6 +592,7 @@
add_action( 'wp_enqueue_scripts', 'wp_enqueue_classic_theme_styles' );
add_action( 'admin_enqueue_scripts', 'wp_localize_jquery_ui_datepicker', 1000 );
add_action( 'admin_enqueue_scripts', 'wp_common_block_scripts_and_styles' );
add_action( 'admin_enqueue_scripts', 'wp_enqueue_command_palette_assets' );
add_action( 'enqueue_block_assets', 'wp_enqueue_classic_theme_styles' );
add_action( 'enqueue_block_assets', 'wp_enqueue_registered_block_scripts_and_styles' );
add_action( 'enqueue_block_assets', 'enqueue_block_styles_assets', 30 );
Expand Down
97 changes: 96 additions & 1 deletion src/wp-includes/script-loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -1744,7 +1744,7 @@ function wp_default_styles( $styles ) {
'block-library' => array(),
'block-directory' => array(),
'components' => array(),
'commands' => array(),
'commands' => array( 'wp-components' ),
'edit-post' => array(
'wp-components',
'wp-block-editor',
Expand Down Expand Up @@ -3419,6 +3419,101 @@ function wp_enqueue_classic_theme_styles() {
}
}

/**
* Enqueues the assets required for the Command Palette.
*
* @since 6.9.0
*
* @global array $menu
* @global array $submenu
*/
function wp_enqueue_command_palette_assets() {
global $menu, $submenu;

$command_palette_settings = array();

if ( $menu ) {
$menu_commands = array();
foreach ( $menu as $menu_item ) {
if ( empty( $menu_item[0] ) || ! empty( $menu_item[1] ) && ! current_user_can( $menu_item[1] ) ) {
continue;
}

// Remove all HTML tags and their contents.
$menu_label = $menu_item[0];
while ( preg_match( '/<[^>]*>/', $menu_label ) ) {
$menu_label = preg_replace( '/<[^>]*>.*?<\/[^>]*>|<[^>]*\/>|<[^>]*>/s', '', $menu_label );
}
$menu_label = trim( $menu_label );
$menu_url = '';
$menu_slug = $menu_item[2];

if ( preg_match( '/\.php($|\?)/', $menu_slug ) || wp_http_validate_url( $menu_slug ) ) {
$menu_url = $menu_slug;
} elseif ( ! empty( menu_page_url( $menu_slug, false ) ) ) {
$menu_url = menu_page_url( $menu_slug, false );
}

if ( $menu_url ) {
$menu_commands[] = array(
'label' => $menu_label,
'url' => $menu_url,
'name' => $menu_slug,
);
}

if ( array_key_exists( $menu_slug, $submenu ) ) {
foreach ( $submenu[ $menu_slug ] as $submenu_item ) {
if ( empty( $submenu_item[0] ) || ! empty( $submenu_item[1] ) && ! current_user_can( $submenu_item[1] ) ) {
continue;
}

// Remove all HTML tags and their contents.
$submenu_label = $submenu_item[0];
while ( preg_match( '/<[^>]*>/', $submenu_label ) ) {
$submenu_label = preg_replace( '/<[^>]*>.*?<\/[^>]*>|<[^>]*\/>|<[^>]*>/s', '', $submenu_label );
}
$submenu_label = trim( $submenu_label );
$submenu_url = '';
$submenu_slug = $submenu_item[2];

if ( preg_match( '/\.php($|\?)/', $submenu_slug ) || wp_http_validate_url( $submenu_slug ) ) {
$submenu_url = $submenu_slug;
} elseif ( ! empty( menu_page_url( $submenu_slug, false ) ) ) {
$submenu_url = menu_page_url( $submenu_slug, false );
}

if ( $submenu_url ) {
$menu_commands[] = array(
'label' => sprintf(
/* translators: 1: Menu label, 2: Submenu label. */
__( '%1$s > %2$s' ),
$menu_label,
$submenu_label
),
'url' => $submenu_url,
'name' => $menu_slug . '-' . $submenu_item[2],
);
}
}
}
}
$command_palette_settings['menu_commands'] = $menu_commands;
}

wp_enqueue_script( 'wp-commands' );
wp_enqueue_style( 'wp-commands' );
wp_enqueue_script( 'wp-core-commands' );

wp_add_inline_script(
'wp-core-commands',
sprintf(
'wp.coreCommands.initializeCommandPalette( %s );',
wp_json_encode( $command_palette_settings, JSON_HEX_TAG | JSON_UNESCAPED_SLASHES )
)
);
}

/**
* Removes leading and trailing _empty_ script tags.
*
Expand Down
Loading