-
Notifications
You must be signed in to change notification settings - Fork 4k
/
full-site-editing.php
118 lines (102 loc) · 3.31 KB
/
full-site-editing.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
<?php
/**
* Full Site Editing Utils
*
* @package gutenberg
*/
/**
* Returns whether the current theme is an FSE theme or not.
*
* @return boolean Whether the current theme is an FSE theme or not.
*/
function gutenberg_is_fse_theme() {
return is_readable( get_theme_file_path( '/block-templates/index.html' ) ) ||
is_readable( get_theme_file_path( '/templates/index.html' ) );
}
/**
* Returns whether the current theme is FSE-enabled or not.
*
* @return boolean Whether the current theme is FSE-enabled or not.
*/
function gutenberg_supports_block_templates() {
return gutenberg_is_fse_theme() || current_theme_supports( 'block-templates' );
}
/**
* Removes legacy pages from FSE themes.
*/
function gutenberg_remove_legacy_pages() {
if ( ! gutenberg_is_fse_theme() ) {
return;
}
global $submenu;
if ( isset( $submenu['themes.php'] ) ) {
$indexes_to_remove = array();
foreach ( $submenu['themes.php'] as $index => $menu_item ) {
if ( false !== strpos( $menu_item[2], 'customize.php' ) && ! gutenberg_site_requires_customizer() ) {
$indexes_to_remove[] = $index;
}
if ( false !== strpos( $menu_item[2], 'site-editor.php' ) ) {
$indexes_to_remove[] = $index;
}
if ( false !== strpos( $menu_item[2], 'gutenberg-widgets' ) ) {
$indexes_to_remove[] = $index;
}
}
foreach ( $indexes_to_remove as $index ) {
unset( $submenu['themes.php'][ $index ] );
}
}
}
add_action( 'admin_menu', 'gutenberg_remove_legacy_pages' );
/**
* Removes legacy adminbar items from FSE themes.
*
* @param WP_Admin_Bar $wp_admin_bar The admin-bar instance.
*/
function gutenberg_adminbar_items( $wp_admin_bar ) {
// Early exit if not an FSE theme.
if ( ! gutenberg_is_fse_theme() ) {
return;
}
// Remove customizer link, if this site does not rely on them for plugins or theme options.
if ( ! gutenberg_site_requires_customizer() ) {
$wp_admin_bar->remove_node( 'customize' );
$wp_admin_bar->remove_node( 'customize-background' );
$wp_admin_bar->remove_node( 'customize-header' );
$wp_admin_bar->remove_node( 'widgets' );
}
// Add site-editor link.
if ( ! is_admin() && current_user_can( 'edit_theme_options' ) ) {
$wp_admin_bar->add_node(
array(
'id' => 'site-editor',
'title' => __( 'Edit site', 'gutenberg' ),
'href' => admin_url( 'themes.php?page=gutenberg-edit-site' ),
)
);
}
}
add_action( 'admin_bar_menu', 'gutenberg_adminbar_items', 50 );
/**
* Check if any plugin, or theme features, are using the Customizer.
*
* @return bool A boolean value indicating if Customizer support is needed.
*/
function gutenberg_site_requires_customizer() {
if ( has_action( 'customize_register' ) ) {
return true;
}
return false;
}
/**
* Tells the script loader to load the scripts and styles of custom block on site editor screen.
*
* @param bool $is_block_editor_screen Current decision about loading block assets.
* @return bool Filtered decision about loading block assets.
*/
function gutenberg_site_editor_load_block_editor_scripts_and_styles( $is_block_editor_screen ) {
return ( is_callable( 'get_current_screen' ) && get_current_screen() && 'appearance_page_gutenberg-edit-site' === get_current_screen()->base )
? true
: $is_block_editor_screen;
}
add_filter( 'should_load_block_editor_scripts_and_styles', 'gutenberg_site_editor_load_block_editor_scripts_and_styles' );