PluginProbe
Gutenberg / 23.4.0
Gutenberg v23.4.0
23.9.1 23.9.0 23.8.0 23.7.2 23.7.1 23.7.0 23.6.1 23.6.2 23.6.0 23.5.3 23.5.2 23.5.1 23.5.0 23.4.0 23.3.2 23.3.1 23.3.0 23.2.0 23.2.1 23.2.2 23.1.1 23.1.0 23.0.1 12.6.0 7.4.0 All 402 releases
gutenberg / lib / compat / wordpress-6.9 / command-palette.php

command-palette.php in Gutenberg 23.4.0, at lib/compat/wordpress-6.9/command-palette.php

136 lines 3.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Enqueues the assets required for the Command Palette.
4 *
5 * @global array $menu
6 * @global array $submenu
7 */
8 function gutenberg_enqueue_command_palette_assets() {
9 global $menu, $submenu;
10
11 $command_palette_settings = array(
12 'is_network_admin' => is_network_admin(),
13 );
14
15 /**
16 * Extracts root-level text nodes from HTML string.
17 *
18 * @ignore
19 * @param string $label HTML string to extract text from.
20 * @return string Extracted text content, trimmed.
21 */
22 $extract_root_text = static function ( string $label ): string {
23 if ( '' === $label ) {
24 return '';
25 }
26
27 $processor = new WP_HTML_Tag_Processor( $label );
28 $text_parts = array();
29 $depth = 0;
30
31 while ( $processor->next_token() ) {
32 $token_type = $processor->get_token_type();
33
34 if ( '#text' === $token_type ) {
35 if ( 0 === $depth ) {
36 $text_parts[] = $processor->get_modifiable_text();
37 }
38 continue;
39 }
40
41 if ( '#tag' !== $token_type ) {
42 continue;
43 }
44
45 if ( $processor->is_tag_closer() ) {
46 if ( $depth > 0 ) {
47 --$depth;
48 }
49 continue;
50 }
51
52 $token_name = $processor->get_tag();
53 if ( $token_name && ! WP_HTML_Processor::is_void( $token_name ) ) {
54 ++$depth;
55 }
56 }
57
58 return trim( implode( '', $text_parts ) );
59 };
60
61 if ( $menu ) {
62 $menu_commands = array();
63 foreach ( $menu as $menu_item ) {
64 if ( empty( $menu_item[0] ) || ! is_string( $menu_item[0] ) || ! empty( $menu_item[1] ) && ! current_user_can( $menu_item[1] ) ) {
65 continue;
66 }
67
68 $menu_label = $extract_root_text( $menu_item[0] );
69 $menu_url = '';
70 $menu_slug = $menu_item[2];
71
72 if ( preg_match( '/\.php($|\?)/', $menu_slug ) || wp_http_validate_url( $menu_slug ) ) {
73 $menu_url = $menu_slug;
74 } elseif ( ! empty( menu_page_url( $menu_slug, false ) ) ) {
75 $menu_url = WP_HTML_Decoder::decode_attribute( menu_page_url( $menu_slug, false ) );
76 }
77
78 if ( $menu_url ) {
79 $menu_commands[] = array(
80 'label' => $menu_label,
81 'url' => $menu_url,
82 'name' => $menu_slug,
83 );
84 }
85
86 if ( array_key_exists( $menu_slug, $submenu ) ) {
87 foreach ( $submenu[ $menu_slug ] as $submenu_item ) {
88 if ( empty( $submenu_item[0] ) || ! empty( $submenu_item[1] ) && ! current_user_can( $submenu_item[1] ) ) {
89 continue;
90 }
91
92 $submenu_label = $extract_root_text( $submenu_item[0] );
93 $submenu_url = '';
94 $submenu_slug = $submenu_item[2];
95
96 if ( preg_match( '/\.php($|\?)/', $submenu_slug ) || wp_http_validate_url( $submenu_slug ) ) {
97 $submenu_url = $submenu_slug;
98 } elseif ( ! empty( menu_page_url( $submenu_slug, false ) ) ) {
99 $submenu_url = WP_HTML_Decoder::decode_attribute( menu_page_url( $submenu_slug, false ) );
100 }
101 if ( $submenu_url ) {
102 $menu_commands[] = array(
103 'label' => sprintf(
104 /* translators: 1: Menu label, 2: Submenu label. */
105 __( '%1$s > %2$s' ),
106 $menu_label,
107 $submenu_label
108 ),
109 'url' => $submenu_url,
110 'name' => $menu_slug . '-' . $submenu_item[2],
111 );
112 }
113 }
114 }
115 }
116 $command_palette_settings['menu_commands'] = $menu_commands;
117 }
118
119 wp_enqueue_script( 'wp-commands' );
120 wp_enqueue_style( 'wp-commands' );
121 wp_enqueue_script( 'wp-core-commands' );
122
123 wp_add_inline_script(
124 'wp-core-commands',
125 sprintf(
126 'wp.coreCommands.initializeCommandPalette( %s );',
127 wp_json_encode( $command_palette_settings, JSON_HEX_TAG | JSON_UNESCAPED_SLASHES )
128 )
129 );
130 }
131
132 if ( has_filter( 'admin_enqueue_scripts', 'wp_enqueue_command_palette_assets' ) ) {
133 remove_filter( 'admin_enqueue_scripts', 'wp_enqueue_command_palette_assets' );
134 }
135 add_filter( 'admin_enqueue_scripts', 'gutenberg_enqueue_command_palette_assets', 9 );
136