disable-site-editor.php 2.6 KB

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