| 1 |
<?php |
| 2 |
/** |
| 3 |
* Admin bar in editor experiment. |
| 4 |
* |
| 5 |
* @package gutenberg |
| 6 |
*/ |
| 7 |
|
| 8 |
/** |
| 9 |
* Enables the admin bar in editor experiment. |
| 10 |
*/ |
| 11 |
function gutenberg_enable_admin_bar_in_editor_experiment() { |
| 12 |
$screen = get_current_screen(); |
| 13 |
if ( |
| 14 |
! $screen || |
| 15 |
! is_admin_bar_showing() || |
| 16 |
! gutenberg_is_experiment_enabled( 'gutenberg-admin-bar-in-editor' ) |
| 17 |
) { |
| 18 |
return; |
| 19 |
} |
| 20 |
|
| 21 |
$is_post_editor = 'post' === $screen->base && $screen->is_block_editor(); |
| 22 |
$is_site_editor = 'site-editor' === $screen->id; |
| 23 |
if ( ! $is_post_editor && ! $is_site_editor ) { |
| 24 |
return; |
| 25 |
} |
| 26 |
|
| 27 |
wp_add_inline_script( |
| 28 |
'wp-block-editor', |
| 29 |
'window.__experimentalAdminBarInEditor = true', |
| 30 |
'before' |
| 31 |
); |
| 32 |
} |
| 33 |
|
| 34 |
add_action( 'admin_enqueue_scripts', 'gutenberg_enable_admin_bar_in_editor_experiment' ); |
| 35 |
|
| 36 |
/** |
| 37 |
* Adds a body class when the admin bar in editor experiment is enabled. |
| 38 |
* |
| 39 |
* Applied on every admin page where the admin bar is shown, so that |
| 40 |
* pages that use wp-build (such as `font-library-wp-admin`) |
| 41 |
* will get the experiment treatment. |
| 42 |
* |
| 43 |
* @param string $classes Space-separated list of admin body classes. |
| 44 |
* @return string Filtered list of admin body classes. |
| 45 |
*/ |
| 46 |
function gutenberg_admin_bar_in_editor_body_class( $classes ) { |
| 47 |
if ( |
| 48 |
! is_admin_bar_showing() || |
| 49 |
! gutenberg_is_experiment_enabled( 'gutenberg-admin-bar-in-editor' ) |
| 50 |
) { |
| 51 |
return $classes; |
| 52 |
} |
| 53 |
|
| 54 |
return $classes . ' has-admin-bar-in-editor'; |
| 55 |
} |
| 56 |
|
| 57 |
add_filter( 'admin_body_class', 'gutenberg_admin_bar_in_editor_body_class' ); |
| 58 |
|
| 59 |
/** |
| 60 |
* Enables the admin bar on the site-editor-v2 page. |
| 61 |
*/ |
| 62 |
function gutenberg_enable_admin_bar_in_site_editor_v2() { |
| 63 |
if ( |
| 64 |
! is_admin_bar_showing() || |
| 65 |
! gutenberg_is_experiment_enabled( 'gutenberg-admin-bar-in-editor' ) |
| 66 |
) { |
| 67 |
return; |
| 68 |
} |
| 69 |
|
| 70 |
add_action( 'admin_head', 'wp_admin_bar_header' ); |
| 71 |
remove_action( 'admin_bar_menu', 'wp_admin_bar_sidebar_toggle', 0 ); |
| 72 |
add_action( 'admin_footer-site-editor-v2', 'wp_admin_bar_render' ); |
| 73 |
|
| 74 |
$admin_color = get_user_option( 'admin_color' ); |
| 75 |
if ( empty( $admin_color ) ) { |
| 76 |
$admin_color = 'fresh'; |
| 77 |
} |
| 78 |
$admin_color_class = 'admin-color-' . sanitize_html_class( $admin_color ); |
| 79 |
|
| 80 |
add_action( |
| 81 |
'admin_head-site-editor-v2', |
| 82 |
static function () use ( $admin_color_class ) { |
| 83 |
echo '<script>' |
| 84 |
. 'window.__experimentalAdminBarInEditor = true;' |
| 85 |
. 'document.addEventListener("DOMContentLoaded", function () { document.body.classList.add("has-admin-bar-in-editor", ' . wp_json_encode( $admin_color_class ) . '); });' |
| 86 |
. '</script>'; |
| 87 |
} |
| 88 |
); |
| 89 |
|
| 90 |
wp_enqueue_style( 'admin-bar' ); |
| 91 |
wp_enqueue_style( 'colors' ); |
| 92 |
} |
| 93 |
|
| 94 |
add_action( 'site-editor-v2_init', 'gutenberg_enable_admin_bar_in_site_editor_v2' ); |
| 95 |
|