Skip to content

Commit 8b09509

Browse files
committed
Command Palette: Use WP_HTML_Processor and WP_HTML_Decoder to generate menu label and menu URL
1 parent 98b12d1 commit 8b09509

File tree

1 file changed

+28
-11
lines changed

1 file changed

+28
-11
lines changed

src/wp-includes/script-loader.php

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3441,19 +3441,28 @@ function wp_enqueue_command_palette_assets() {
34413441
continue;
34423442
}
34433443

3444-
// Remove all HTML tags and their contents.
3444+
// Only collect text nodes at root level (not wrapped in any HTML tags).
34453445
$menu_label = $menu_item[0];
3446-
while ( preg_match( '/<[^>]*>/', $menu_label ) ) {
3447-
$menu_label = preg_replace( '/<[^>]*>.*?<\/[^>]*>|<[^>]*\/>|<[^>]*>/s', '', $menu_label );
3446+
$processor = WP_HTML_Processor::create_fragment( $menu_label );
3447+
$text_parts = array();
3448+
3449+
if ( $processor->next_token() ) {
3450+
$root_depth = $processor->get_current_depth();
3451+
do {
3452+
if ( '#text' === $processor->get_token_type() && $root_depth === $processor->get_current_depth() ) {
3453+
$text_parts[] = $processor->get_modifiable_text();
3454+
}
3455+
} while ( $processor->next_token() );
34483456
}
3449-
$menu_label = trim( $menu_label );
3457+
3458+
$menu_label = trim( implode( '', $text_parts ) );
34503459
$menu_url = '';
34513460
$menu_slug = $menu_item[2];
34523461

34533462
if ( preg_match( '/\.php($|\?)/', $menu_slug ) || wp_http_validate_url( $menu_slug ) ) {
34543463
$menu_url = $menu_slug;
34553464
} elseif ( ! empty( menu_page_url( $menu_slug, false ) ) ) {
3456-
$menu_url = html_entity_decode( menu_page_url( $menu_slug, false ), ENT_QUOTES, get_bloginfo( 'charset' ) );
3465+
$menu_url = WP_HTML_Decoder::decode_attribute( menu_page_url( $menu_slug, false ) );
34573466
}
34583467

34593468
if ( $menu_url ) {
@@ -3470,21 +3479,29 @@ function wp_enqueue_command_palette_assets() {
34703479
continue;
34713480
}
34723481

3473-
// Remove all HTML tags and their contents.
3482+
// Only collect text nodes at root level (not wrapped in any HTML tags).
34743483
$submenu_label = $submenu_item[0];
3475-
while ( preg_match( '/<[^>]*>/', $submenu_label ) ) {
3476-
$submenu_label = preg_replace( '/<[^>]*>.*?<\/[^>]*>|<[^>]*\/>|<[^>]*>/s', '', $submenu_label );
3484+
$processor = WP_HTML_Processor::create_fragment( $submenu_label );
3485+
$text_parts = array();
3486+
3487+
if ( $processor->next_token() ) {
3488+
$root_depth = $processor->get_current_depth();
3489+
do {
3490+
if ( '#text' === $processor->get_token_type() && $root_depth === $processor->get_current_depth() ) {
3491+
$text_parts[] = $processor->get_modifiable_text();
3492+
}
3493+
} while ( $processor->next_token() );
34773494
}
3478-
$submenu_label = trim( $submenu_label );
3495+
3496+
$submenu_label = trim( implode( '', $text_parts ) );
34793497
$submenu_url = '';
34803498
$submenu_slug = $submenu_item[2];
34813499

34823500
if ( preg_match( '/\.php($|\?)/', $submenu_slug ) || wp_http_validate_url( $submenu_slug ) ) {
34833501
$submenu_url = $submenu_slug;
34843502
} elseif ( ! empty( menu_page_url( $submenu_slug, false ) ) ) {
3485-
$submenu_url = html_entity_decode( menu_page_url( $submenu_slug, false ), ENT_QUOTES, get_bloginfo( 'charset' ) );
3503+
$submenu_url = WP_HTML_Decoder::decode_attribute( menu_page_url( $submenu_slug, false ) );
34863504
}
3487-
34883505
if ( $submenu_url ) {
34893506
$menu_commands[] = array(
34903507
'label' => sprintf(

0 commit comments

Comments
 (0)