disable-site-editor.php 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. /**
  3. * Adds a setting to the Gutenberg experiments page to disable the Site Editor.
  4. */
  5. function add_disable_site_editor_setting() {
  6. if ( ! is_readable( get_stylesheet_directory() . '/block-templates/index.html' ) ) {
  7. return;
  8. }
  9. add_settings_field(
  10. 'universal-theme-disable-site-editor',
  11. __( 'Site Editor', 'gutenberg' ),
  12. 'gutenberg_display_experiment_field',
  13. 'gutenberg-experiments',
  14. 'gutenberg_experiments_section',
  15. array(
  16. 'label' => __( 'Enable Site Editor', 'gutenberg' ),
  17. 'id' => 'universal-theme-disable-site-editor',
  18. )
  19. );
  20. readd_legacy_admin_links();
  21. if ( ! site_editor_enabled() ) {
  22. remove_site_editor_admin_link();
  23. }
  24. }
  25. function site_editor_enabled() {
  26. return get_option( 'gutenberg-experiments' ) && array_key_exists( 'universal-theme-disable-site-editor', get_option( 'gutenberg-experiments' ) );
  27. }
  28. /**
  29. * Adds the Customizer and Widgets menu links back to the Dashboard under themes.
  30. */
  31. function readd_legacy_admin_links() {
  32. global $submenu;
  33. if ( isset( $submenu['themes.php'] ) ) {
  34. // Add Customize back to the admin menu.
  35. $customize_url = add_query_arg( 'return', urlencode( remove_query_arg( wp_removable_query_args(), wp_unslash( $_SERVER['REQUEST_URI'] ) ) ), 'customize.php' );
  36. $submenu['themes.php'][6] = array( __( 'Customize', 'blockbase' ), 'customize', esc_url( $customize_url ), '', 'hide-if-no-customize' );
  37. if (
  38. function_exists( 'gutenberg_use_widgets_block_editor' ) &&
  39. gutenberg_use_widgets_block_editor() &&
  40. ! function_exists( 'wp_use_widgets_block_editor' ) &&
  41. current_theme_supports( 'widgets' )
  42. ) {
  43. // Find Widgets menu
  44. $has_widgets_menu = false;
  45. foreach ( $submenu['themes.php'] as $index => $menu_item ) {
  46. if (
  47. ! empty( $menu_item[2] ) &&
  48. ( false !== strpos( $menu_item[2], 'gutenberg-widgets' ) ||
  49. false !== strpos( $menu_item[2], 'widgets.php' ) )
  50. ) {
  51. $has_widgets_menu = true;
  52. }
  53. }
  54. // Add Widgets back to the admin menu.
  55. if ( ! $has_widgets_menu ) {
  56. add_theme_page(
  57. __( 'Widgets', 'gutenberg' ),
  58. __( 'Widgets', 'gutenberg' ),
  59. 'edit_theme_options',
  60. 'gutenberg-widgets',
  61. 'the_gutenberg_widgets',
  62. 2
  63. );
  64. }
  65. }
  66. ksort( $submenu['themes.php'] );
  67. }
  68. }
  69. /**
  70. * Removes the Site Editor link from the admin.
  71. */
  72. function remove_site_editor_admin_link() {
  73. global $menu;
  74. // Remove Site Editor.
  75. foreach ( $menu as $index => $menu_item ) {
  76. if ( ! empty( $menu_item[5] ) && false !== strpos( $menu_item[5], 'toplevel_page_gutenberg-edit-site' ) ) {
  77. $site_editor_index = $index;
  78. }
  79. }
  80. unset( $menu[ $site_editor_index ] );
  81. }
  82. add_action( 'admin_init', 'add_disable_site_editor_setting' );