| 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 |
$is_apple_os = (bool) preg_match( '/Macintosh|Mac OS X|Mac_PowerPC/i', $_SERVER['HTTP_USER_AGENT'] ?? '' ); |
| 13 |
$shortcut_label = $is_apple_os |
| 14 |
? _x( '⌘K', 'keyboard shortcut to open the command palette' ) |
| 15 |
: _x( 'Ctrl+K', 'keyboard shortcut to open the command palette' ); |
| 16 |
$title = sprintf( |
| 17 |
'<span class="ab-icon" aria-hidden="true"></span><span class="ab-label"><kbd>%s</kbd><span class="screen-reader-text"> %s</span></span>', |
| 18 |
$shortcut_label, |
| 19 |
/* translators: Hidden accessibility text. */ |
| 20 |
__( 'Open command palette' ), |
| 21 |
); |
| 22 |
$wp_admin_bar->add_node( |
| 23 |
array( |
| 24 |
'id' => 'command-palette', |
| 25 |
'title' => $title, |
| 26 |
'href' => '#', |
| 27 |
'meta' => array( |
| 28 |
'class' => 'hide-if-no-js', |
| 29 |
'onclick' => 'wp.data.dispatch( "core/commands" ).open(); return false;', |
| 30 |
), |
| 31 |
) |
| 32 |
); |
| 33 |
} |
| 34 |
if ( has_action( 'admin_bar_menu', 'wp_admin_bar_command_palette_menu' ) ) { |
| 35 |
remove_action( 'admin_bar_menu', 'wp_admin_bar_command_palette_menu', 55 ); |
| 36 |
} |
| 37 |
add_action( 'admin_bar_menu', 'gutenberg_admin_bar_command_palette_menu', 55 ); |
| 38 |
|
| 39 |
function gutenberg_add_admin_bar_styles() { |
| 40 |
if ( ! is_admin_bar_showing() ) { |
| 41 |
return; |
| 42 |
} |
| 43 |
$css = <<<CSS |
| 44 |
#wpadminbar #wp-admin-bar-command-palette .ab-icon { |
| 45 |
display: none; /* Icon displayed only on mobile */ |
| 46 |
} |
| 47 |
#wpadminbar #wp-admin-bar-command-palette .ab-icon:before { |
| 48 |
content: "\\f179"; |
| 49 |
content: "\\f179" / ''; |
| 50 |
} |
| 51 |
#wpadminbar #wp-admin-bar-command-palette kbd { |
| 52 |
background: transparent; |
| 53 |
} |
| 54 |
@media screen and (max-width: 782px) { |
| 55 |
#wpadminbar #wp-admin-bar-command-palette .ab-icon { |
| 56 |
display: block; /* Icon is only shown on mobile, while the keyboard shortcut is hidden */ |
| 57 |
} |
| 58 |
#wpadminbar #wp-admin-bar-command-palette .ab-icon:before { |
| 59 |
text-indent: 0; |
| 60 |
font: normal 32px/1 dashicons; |
| 61 |
width: 52px; |
| 62 |
text-align: center; |
| 63 |
-webkit-font-smoothing: antialiased; |
| 64 |
-moz-osx-font-smoothing: grayscale; |
| 65 |
display: block; |
| 66 |
font-size: 34px; |
| 67 |
height: 46px; |
| 68 |
line-height: 1.38235294; |
| 69 |
top: 0; |
| 70 |
} |
| 71 |
#wpadminbar li#wp-admin-bar-command-palette { |
| 72 |
display: block; |
| 73 |
} |
| 74 |
#wpadminbar #wp-admin-bar-command-palette { |
| 75 |
position: static; |
| 76 |
} |
| 77 |
} |
| 78 |
CSS; |
| 79 |
wp_add_inline_style( 'admin-bar', $css ); |
| 80 |
} |
| 81 |
add_action( 'admin_bar_init', 'gutenberg_add_admin_bar_styles' ); |
| 82 |
|