Blockbase: Disable the Site Editor on WPCOM (#4206)

* Blockbase: Disable the Site Editor on WPCOM

* extract to a function

* Also hide the admin bar link to the Site Editor

* fix notice

* remove confusing references to disabling site editor

* move the condition to show the site editor links to the toggle file for simplicity and clarity

* Also hide the site editor from the API menu

* check has_blog_sticker exists before calling it
This commit is contained in:
Ben Dwyer 2021-07-15 12:13:04 +01:00 committed by GitHub
parent 6935bfd92f
commit ae2d449610
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 130 additions and 98 deletions

View file

@ -102,5 +102,5 @@ require get_template_directory() . '/inc/customizer/wp-customize-colors.php';
require get_template_directory() . '/inc/customizer/wp-customize-color-palettes.php';
require get_template_directory() . '/inc/customizer/wp-customize-fonts.php';
/** Add a checkbox to hide the Site Editor */
require get_template_directory() . '/inc/disable-site-editor.php';
/** Add a checkbox to show/hide the Site Editor */
require get_template_directory() . '/inc/toggle-site-editor.php';

View file

@ -1,94 +0,0 @@
<?php
/**
* Adds a setting to the Gutenberg experiments page to disable the Site Editor.
*/
function add_disable_site_editor_setting() {
if ( ! is_readable( get_stylesheet_directory() . '/block-templates/index.html' ) ) {
return;
}
add_settings_field(
'universal-theme-disable-site-editor',
__( 'Site Editor', 'gutenberg' ),
'gutenberg_display_experiment_field',
'gutenberg-experiments',
'gutenberg_experiments_section',
array(
'label' => __( 'Enable Site Editor', 'gutenberg' ),
'id' => 'universal-theme-disable-site-editor',
)
);
readd_legacy_admin_links();
if ( ! site_editor_enabled() ) {
remove_site_editor_admin_link();
}
}
function site_editor_enabled() {
return get_option( 'gutenberg-experiments' ) && array_key_exists( 'universal-theme-disable-site-editor', get_option( 'gutenberg-experiments' ) );
}
/**
* Adds the Customizer and Widgets menu links back to the Dashboard under themes.
*/
function readd_legacy_admin_links() {
global $submenu;
if ( isset( $submenu['themes.php'] ) ) {
// Add Customize back to the admin menu.
$customize_url = add_query_arg( 'return', urlencode( remove_query_arg( wp_removable_query_args(), wp_unslash( $_SERVER['REQUEST_URI'] ) ) ), 'customize.php' );
$submenu['themes.php'][6] = array( __( 'Customize', 'blockbase' ), 'customize', esc_url( $customize_url ), '', 'hide-if-no-customize' );
if (
function_exists( 'gutenberg_use_widgets_block_editor' ) &&
gutenberg_use_widgets_block_editor() &&
! function_exists( 'wp_use_widgets_block_editor' ) &&
current_theme_supports( 'widgets' )
) {
// Find Widgets menu
$has_widgets_menu = false;
foreach ( $submenu['themes.php'] as $index => $menu_item ) {
if (
! empty( $menu_item[2] ) &&
( false !== strpos( $menu_item[2], 'gutenberg-widgets' ) ||
false !== strpos( $menu_item[2], 'widgets.php' ) )
) {
$has_widgets_menu = true;
}
}
// Add Widgets back to the admin menu.
if ( ! $has_widgets_menu ) {
add_theme_page(
__( 'Widgets', 'gutenberg' ),
__( 'Widgets', 'gutenberg' ),
'edit_theme_options',
'gutenberg-widgets',
'the_gutenberg_widgets',
2
);
}
}
ksort( $submenu['themes.php'] );
}
}
/**
* Removes the Site Editor link from the admin.
*/
function remove_site_editor_admin_link() {
global $menu;
// Remove Site Editor.
foreach ( $menu as $index => $menu_item ) {
if ( ! empty( $menu_item[5] ) && false !== strpos( $menu_item[5], 'toplevel_page_gutenberg-edit-site' ) ) {
$site_editor_index = $index;
}
}
unset( $menu[ $site_editor_index ] );
}
add_action( 'admin_init', 'add_disable_site_editor_setting' );

View file

@ -0,0 +1,128 @@
<?php
/**
* Adds a setting to the Gutenberg experiments page to enable the Site Editor.
*/
function blockbase_toggle_site_editor() {
if ( ! is_readable( get_stylesheet_directory() . '/block-templates/index.html' ) ) {
return;
}
add_action( 'admin_init', 'blockbase_add_settings_field' );
add_action( 'admin_init', 'blockbase_readd_legacy_admin_links' );
// For WPCOM
add_action( 'admin_menu_rest_api', 'blockbase_readd_legacy_admin_links' );
if ( ! site_editor_enabled() ) {
add_action( 'admin_init', 'blockbase_remove_site_editor_admin_link' );
add_action( 'admin_bar_menu', 'blockbase_remove_site_editor_link', 50 );
// For WPCOM
add_action( 'admin_menu_rest_api', 'blockbase_remove_site_editor_admin_link' );
}
}
function blockbase_add_settings_field() {
add_settings_field(
'universal-theme-enable-site-editor',
__( 'Site Editor', 'gutenberg' ),
'gutenberg_display_experiment_field',
'gutenberg-experiments',
'gutenberg_experiments_section',
array(
'label' => __( 'Enable Site Editor', 'gutenberg' ),
'id' => 'universal-theme-enable-site-editor',
)
);
}
/**
* Adds the Customizer and Widgets menu links back to the Dashboard under themes.
*/
function blockbase_readd_legacy_admin_links() {
global $submenu;
if ( isset( $submenu['themes.php'] ) ) {
// Add Customize back to the admin menu.
$customize_url = add_query_arg( 'return', urlencode( remove_query_arg( wp_removable_query_args(), wp_unslash( $_SERVER['REQUEST_URI'] ) ) ), 'customize.php' );
$customizer_key = 6;
if ( defined( 'IS_WPCOM' ) ) {
$customizer_key = 1;
}
$submenu['themes.php'][ $customizer_key ] = array( __( 'Customize' ), 'customize', esc_url( $customize_url ), '', 'hide-if-no-customize' );
if (
function_exists( 'gutenberg_use_widgets_block_editor' ) &&
gutenberg_use_widgets_block_editor() &&
! function_exists( 'wp_use_widgets_block_editor' ) &&
current_theme_supports( 'widgets' )
) {
// Find Widgets menu
$has_widgets_menu = false;
foreach ( $submenu['themes.php'] as $index => $menu_item ) {
if (
! empty( $menu_item[ 2 ] ) &&
( false !== strpos( $menu_item[ 2 ], 'gutenberg-widgets' ) ||
false !== strpos( $menu_item[ 2 ], 'widgets.php' ) )
) {
$has_widgets_menu = true;
}
}
// Add Widgets back to the admin menu.
if ( ! $has_widgets_menu ) {
add_theme_page(
__( 'Widgets', 'gutenberg' ),
__( 'Widgets', 'gutenberg' ),
'edit_theme_options',
'gutenberg-widgets',
'the_gutenberg_widgets',
2
);
}
}
ksort( $submenu['themes.php'] );
}
}
function is_site_editor_menu_item( $menu_item ) {
if ( ! empty( $menu_item[ 2 ] ) ) {
return false !== strpos( $menu_item[ 2 ], 'gutenberg-edit-site' ) || false !== strpos( $menu_item[ 2 ], 'site-editor' );
}
}
function site_editor_enabled() {
// For WPCOM
if ( function_exists('has_blog_sticker') && has_blog_sticker( 'core-site-editor-enabled', get_current_blog_id() ) ) {
return true;
}
return get_option( 'gutenberg-experiments' ) && array_key_exists( 'universal-theme-enable-site-editor', get_option( 'gutenberg-experiments' ) );
}
/**
* Removes the Site Editor link from the admin.
*/
function blockbase_remove_site_editor_admin_link() {
global $menu;
// Remove Site Editor.
foreach ( $menu as $index => $menu_item ) {
if ( is_site_editor_menu_item( $menu_item ) ) {
$site_editor_index = $index;
}
}
if ( ! empty( $site_editor_index ) ) {
unset( $menu[ $site_editor_index ] );
}
}
/**
* Removes Site Editor adminbar item.
*
* @param WP_Admin_Bar $wp_admin_bar The admin-bar instance.
*/
function blockbase_remove_site_editor_link( $wp_admin_bar ) {
$wp_admin_bar->remove_node( 'site-editor' );
}
add_action( 'init', 'blockbase_toggle_site_editor' );

View file

@ -7,7 +7,6 @@
* @package Blockbase
*/
/**
* Enqueue our WP.com styles for front-end.
* Loads after theme styles so we can add overrides.
@ -16,4 +15,3 @@ function blockbase_wpcom_scripts() {
wp_enqueue_style( 'blockbase-wpcom-style', get_template_directory_uri() . '/inc/wpcom-style.css', array( 'blockbase-ponyfill' ) );
}
add_action( 'wp_enqueue_scripts', 'blockbase_wpcom_scripts' );