PluginProbe
Gutenberg / 23.5.1
Gutenberg v23.5.1
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.5.1, at lib/compat/wordpress-7.0/command-palette.php

108 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 * 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 {
71 display: none; /* Icon displayed only on mobile */
72 }
73 #wpadminbar #wp-admin-bar-command-palette .ab-icon:before {
74 content: "\\f179";
75 content: "\\f179" / '';
76 }
77 #wpadminbar #wp-admin-bar-command-palette kbd {
78 background: transparent;
79 }
80 @media screen and (max-width: 782px) {
81 #wpadminbar #wp-admin-bar-command-palette .ab-icon {
82 display: block; /* Icon is only shown on mobile, while the keyboard shortcut is hidden */
83 }
84 #wpadminbar #wp-admin-bar-command-palette .ab-icon:before {
85 text-indent: 0;
86 font: normal 32px/1 dashicons;
87 width: 52px;
88 text-align: center;
89 -webkit-font-smoothing: antialiased;
90 -moz-osx-font-smoothing: grayscale;
91 display: block;
92 font-size: 34px;
93 height: 46px;
94 line-height: 1.38235294;
95 top: 0;
96 }
97 #wpadminbar li#wp-admin-bar-command-palette {
98 display: block;
99 }
100 #wpadminbar #wp-admin-bar-command-palette {
101 position: static;
102 }
103 }
104 CSS;
105 wp_add_inline_style( 'admin-bar', $css );
106 }
107 add_action( 'admin_bar_init', 'gutenberg_add_admin_bar_styles' );
108