PluginProbe
Gutenberg / 23.6.2
Gutenberg v23.6.2
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-7.0 / command-palette.php

command-palette.php in Gutenberg 23.6.2, at lib/compat/wordpress-7.0/command-palette.php

103 lines 3.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Adds the Command Palette trigger button to the admin bar.
4 *
5 * @param WP_Admin_Bar $wp_admin_bar The WP_Admin_Bar instance.
6 */
7 function gutenberg_admin_bar_command_palette_menu( WP_Admin_Bar $wp_admin_bar ): void {
8 if ( ! is_admin() || ! wp_script_is( 'wp-core-commands', 'enqueued' ) ) {
9 return;
10 }
11
12 $shortcut_labels = array(
13 'appleOS' => _x( '⌘K', 'keyboard shortcut to open the command palette' ),
14 'default' => _x( 'Ctrl+K', 'keyboard shortcut to open the command palette' ),
15 );
16 $apple_pattern = 'Macintosh|Mac OS X|Mac_PowerPC';
17 $is_apple_os = (bool) preg_match( "/{$apple_pattern}/i", $_SERVER['HTTP_USER_AGENT'] ?? '' );
18 $shortcut_label = $is_apple_os ? $shortcut_labels['appleOS'] : $shortcut_labels['default'];
19 $title = sprintf(
20 '<span class="ab-icon" aria-hidden="true"></span><span class="ab-label"><kbd>%s</kbd><span class="screen-reader-text"> %s</span></span>',
21 $shortcut_label,
22 /* translators: Hidden accessibility text. */
23 __( 'Open command palette' ),
24 );
25
26 /*
27 * Detect Apple OS via JavaScript for sites behind a CDN blocking the UA header.
28 *
29 * Running the script as the admin bar is rendered avoids a flash of incorrect content
30 * for users with Apple OS when the UA header is blocked. It also prevents the need for
31 * wp-i18n to be loaded as a dependency.
32 */
33 $function = <<<'JS'
34 ( applePattern, appleOSLabel ) => {
35 if ( ( new RegExp( applePattern ) ).test( navigator.userAgent ) ) {
36 document.querySelector( '#wp-admin-bar-command-palette .ab-label kbd' ).textContent = appleOSLabel;
37 }
38 }
39 JS;
40 $script = sprintf(
41 '( %s )( %s, %s );',
42 $function,
43 wp_json_encode( $apple_pattern, JSON_HEX_TAG | JSON_UNESCAPED_SLASHES ),
44 wp_json_encode( $shortcut_labels['appleOS'], JSON_HEX_TAG | JSON_UNESCAPED_SLASHES )
45 );
46 $script .= "\n//# sourceURL=" . rawurlencode( __FUNCTION__ );
47 $wp_admin_bar->add_node(
48 array(
49 'id' => 'command-palette',
50 'title' => $title,
51 'href' => '#',
52 'meta' => array(
53 'class' => 'hide-if-no-js',
54 'onclick' => 'wp.data.dispatch( "core/commands" ).open(); return false;',
55 'html' => wp_get_inline_script_tag( $script ),
56 ),
57 )
58 );
59 }
60 if ( has_action( 'admin_bar_menu', 'wp_admin_bar_command_palette_menu' ) ) {
61 remove_action( 'admin_bar_menu', 'wp_admin_bar_command_palette_menu', 55 );
62 }
63 add_action( 'admin_bar_menu', 'gutenberg_admin_bar_command_palette_menu', 55 );
64
65 function gutenberg_add_admin_bar_styles() {
66 if ( ! is_admin_bar_showing() ) {
67 return;
68 }
69 $css = <<<CSS
70 #wpadminbar #wp-admin-bar-command-palette .ab-icon:before {
71 content: "\\f179";
72 content: "\\f179" / '';
73 top: 3px;
74 }
75 #wpadminbar #wp-admin-bar-command-palette kbd {
76 background: transparent;
77 }
78 @media screen and (max-width: 782px) {
79 #wpadminbar #wp-admin-bar-command-palette .ab-icon:before {
80 text-indent: 0;
81 font: normal 32px/1 dashicons;
82 width: 52px;
83 text-align: center;
84 -webkit-font-smoothing: antialiased;
85 -moz-osx-font-smoothing: grayscale;
86 display: block;
87 font-size: 34px;
88 height: 46px;
89 line-height: 1.38235294;
90 top: 0;
91 }
92 #wpadminbar li#wp-admin-bar-command-palette {
93 display: block;
94 }
95 #wpadminbar #wp-admin-bar-command-palette {
96 position: static;
97 }
98 }
99 CSS;
100 wp_add_inline_style( 'admin-bar', $css );
101 }
102 add_action( 'admin_bar_init', 'gutenberg_add_admin_bar_styles' );
103