Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Synchronize wp_is_block_theme and block-templates block support with Core #37218

Merged
merged 3 commits into from
Dec 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/client-assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -693,7 +693,7 @@ function gutenberg_extend_block_editor_styles( $settings ) {
* @return array Filtered editor settings.
*/
function gutenberg_extend_block_editor_settings_with_fse_theme_flag( $settings ) {
$settings['supportsTemplateMode'] = gutenberg_supports_block_templates();
$settings['supportsTemplateMode'] = current_theme_supports( 'block-templates' );

// Enable the new layout options for themes with a theme.json file.
$settings['supportsLayout'] = WP_Theme_JSON_Resolver_Gutenberg::theme_has_support();
Expand Down
2 changes: 1 addition & 1 deletion lib/compat.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ function wp_should_load_separate_core_block_assets() {
add_filter(
'separate_core_block_assets',
function( $load_separate_styles ) {
if ( function_exists( 'gutenberg_is_fse_theme' ) && gutenberg_is_fse_theme() ) {
if ( function_exists( 'wp_is_block_theme' ) && wp_is_block_theme() ) {
return true;
}
return $load_separate_styles;
Expand Down
2 changes: 1 addition & 1 deletion lib/compat/wordpress-5.8/block-editor.php
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ function gutenberg_get_default_block_editor_settings() {
}

$editor_settings = array(
'__unstableEnableFullSiteEditingBlocks' => gutenberg_supports_block_templates(),
'__unstableEnableFullSiteEditingBlocks' => current_theme_supports( 'block-templates' ),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I noticed that this __unstableEnableFullSiteEditingBlocks flag is not set on Core at all, I'm not entirely sure what's the impact here but potentially we have a missing backport as well cc @noisysocks @gziolo

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh yes there was a ticket for that.

https://core.trac.wordpress.org/ticket/54583

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's actually another flag.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok I just saw that all the usage of __unstableEnableFullSiteEditingBlocks is actually inside process.env.GUTENBERG_PHASE === 2 meaning it's not necessary at all to backport this to Core.

'alignWide' => get_theme_support( 'align-wide' ),
'allowedBlockTypes' => true,
'allowedMimeTypes' => get_allowed_mime_types(),
Expand Down
2 changes: 1 addition & 1 deletion lib/compat/wordpress-5.9/block-template.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* Adds necessary filters to use 'wp_template' posts instead of theme template files.
*/
function gutenberg_add_template_loader_filters() {
if ( ! gutenberg_supports_block_templates() ) {
if ( ! current_theme_supports( 'block-templates' ) ) {
return;
}
$template_type_slugs = array_keys( get_default_block_template_types() );
Expand Down
2 changes: 1 addition & 1 deletion lib/compat/wordpress-5.9/default-theme-supports.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* @package gutenberg
*/

if ( gutenberg_is_fse_theme() ) {
if ( wp_is_block_theme() ) {
add_theme_support( 'post-thumbnails' );
add_theme_support( 'responsive-embeds' );
add_theme_support( 'editor-styles' );
Expand Down
4 changes: 2 additions & 2 deletions lib/compat/wordpress-5.9/move-theme-editor-menu-item.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
*/

/**
* Moves the "theme editor" under "tools" in FSE themes.
* Moves the "theme editor" under "tools" in block themes.
*/
function gutenberg_move_theme_editor_in_block_themes() {
if ( ! gutenberg_is_fse_theme() || is_multisite() ) {
if ( ! wp_is_block_theme() || is_multisite() ) {
return;
}
remove_submenu_page( 'themes.php', 'theme-editor.php' );
Expand Down
8 changes: 0 additions & 8 deletions lib/compat/wordpress-5.9/template-parts.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,6 @@
* Registers block editor 'wp_template_part' post type.
*/
function gutenberg_register_template_part_post_type() {
if ( ! gutenberg_supports_block_templates() ) {
return;
}

$labels = array(
'name' => __( 'Template Parts', 'gutenberg' ),
'singular_name' => __( 'Template Part', 'gutenberg' ),
Expand Down Expand Up @@ -75,10 +71,6 @@ function gutenberg_register_template_part_post_type() {
* Registers the 'wp_template_part_area' taxonomy.
*/
function gutenberg_register_wp_template_part_area_taxonomy() {
if ( ! gutenberg_supports_block_templates() ) {
return;
}

register_taxonomy(
'wp_template_part_area',
array( 'wp_template_part' ),
Expand Down
10 changes: 1 addition & 9 deletions lib/compat/wordpress-5.9/templates.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,6 @@
* Registers block editor 'wp_template' post type.
*/
function gutenberg_register_template_post_type() {
if ( ! gutenberg_supports_block_templates() ) {
return;
}

$labels = array(
'name' => __( 'Templates', 'gutenberg' ),
'singular_name' => __( 'Template', 'gutenberg' ),
Expand Down Expand Up @@ -76,10 +72,6 @@ function gutenberg_register_template_post_type() {
* Registers block editor 'wp_theme' taxonomy.
*/
function gutenberg_register_wp_theme_taxonomy() {
if ( ! gutenberg_supports_block_templates() && ! WP_Theme_JSON_Resolver_Gutenberg::theme_has_support() ) {
return;
}

register_taxonomy(
'wp_theme',
array( 'wp_template', 'wp_template_part', 'wp_global_styles' ),
Expand Down Expand Up @@ -165,7 +157,7 @@ function set_unique_slug_on_create_template( $post_id ) {
*/
function gutenberg_the_skip_link() {
// Early exit if not a block theme.
if ( ! gutenberg_supports_block_templates() ) {
if ( ! current_theme_supports( 'block-templates' ) ) {
return;
}

Expand Down
13 changes: 13 additions & 0 deletions lib/compat/wordpress-5.9/theme-templates.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,16 @@ function gutenberg_filter_wp_template_unique_post_slug( $override_slug, $slug, $
// Remove 5.8 filter if existant.
remove_filter( 'pre_wp_unique_post_slug', 'wp_filter_wp_template_unique_post_slug' );
add_filter( 'pre_wp_unique_post_slug', 'gutenberg_filter_wp_template_unique_post_slug', 10, 5 );

/**
* Enable block templates (editor mode) for themes with theme.json.
*/
function gutenberg_enable_block_templates() {
if ( wp_is_block_theme() || WP_Theme_JSON_Resolver_Gutenberg::theme_has_support() ) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this function I added wp_is_block_theme to auto-enable block-templates for block themes. It was already the case in the Gutenberg plugin because before this PR, the plugin used gutenberg_supports_block_templates to check support but for core, if a block theme didn't have a "theme.json" file, block templates support would have been disabled. So this change here need to be backported into Core.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like you already made this change in Core so I'll do nothing 😀

WordPress/wordpress-develop@f7d2a2e

add_theme_support( 'block-templates' );
}
}

// Remove 5.8 filter if existant.
remove_action( 'setup_theme', 'wp_enable_block_templates' );
add_action( 'setup_theme', 'gutenberg_enable_block_templates' );
18 changes: 18 additions & 0 deletions lib/compat/wordpress-5.9/theme.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php
/**
* Additions to the WordPress theme.php file
*
* @package gutenberg
*/

if ( ! function_exists( 'wp_is_block_theme' ) ) {
/**
* Returns whether the current theme is a block theme or not.
*
* @return boolean Whether the current theme is a block theme or not.
*/
function wp_is_block_theme() {
return is_readable( get_theme_file_path( '/block-templates/index.html' ) ) ||
is_readable( get_theme_file_path( '/templates/index.html' ) );
}
}
2 changes: 1 addition & 1 deletion lib/compat/wordpress-5.9/wp-theme-get-post-templates.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* @return array (Maybe) modified page templates array.
*/
function gutenberg_load_block_page_templates( $templates, $theme, $post, $post_type ) {
if ( ! gutenberg_supports_block_templates() ) {
if ( ! current_theme_supports( 'block-templates' ) ) {
return $templates;
}

Expand Down
4 changes: 2 additions & 2 deletions lib/editor-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ function gutenberg_extend_post_editor_settings( $settings ) {
$image_sizes = wp_list_pluck( $settings['imageSizes'], 'slug' );

$settings['imageDefaultSize'] = in_array( $image_default_size, $image_sizes, true ) ? $image_default_size : 'large';
$settings['__unstableEnableFullSiteEditingBlocks'] = gutenberg_supports_block_templates();
$settings['__unstableEnableFullSiteEditingBlocks'] = current_theme_supports( 'block-templates' );

if ( gutenberg_is_fse_theme() ) {
if ( wp_is_block_theme() ) {
$settings['defaultTemplatePartAreas'] = get_allowed_block_template_part_areas();
}

Expand Down
33 changes: 3 additions & 30 deletions lib/full-site-editing/full-site-editing.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,38 +5,11 @@
* @package gutenberg
*/

/**
* Returns whether the current theme is an FSE theme or not.
*
* Note: once 5.9 is the minimum supported WordPress version for the Gutenberg
* plugin, we must deprecate this function and
* use wp_is_block_theme instead.
*
* @return boolean Whether the current theme is an FSE theme or not.
*/
function gutenberg_is_fse_theme() {
if ( function_exists( 'wp_is_block_theme' ) ) {
return wp_is_block_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() ) {
if ( ! wp_is_block_theme() ) {
return;
}

Expand Down Expand Up @@ -80,8 +53,8 @@ function gutenberg_remove_legacy_pages() {
*/
function gutenberg_adminbar_items( $wp_admin_bar ) {

// Early exit if not an FSE theme.
if ( ! gutenberg_is_fse_theme() ) {
// Early exit if not a block theme.
if ( ! wp_is_block_theme() ) {
return;
}

Expand Down
2 changes: 1 addition & 1 deletion lib/global-styles.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ function_exists( 'gutenberg_is_edit_site_page' ) &&
* @return boolean
*/
function gutenberg_experimental_is_site_editor_available() {
return gutenberg_is_fse_theme();
return wp_is_block_theme();
}

/**
Expand Down
11 changes: 0 additions & 11 deletions lib/init.php
Original file line number Diff line number Diff line change
Expand Up @@ -215,14 +215,3 @@ function register_site_logo_to_rest_index( $response ) {
add_filter( 'rest_index', 'register_site_logo_to_rest_index' );

add_theme_support( 'widgets-block-editor' );

/**
* Enable block templates (editor mode) for themes with theme.json.
*/
function gutenberg_enable_block_templates() {
if ( WP_Theme_JSON_Resolver_Gutenberg::theme_has_support() ) {
add_theme_support( 'block-templates' );
}
}

add_action( 'setup_theme', 'gutenberg_enable_block_templates' );
1 change: 1 addition & 0 deletions lib/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ function gutenberg_is_experiment_enabled( $name ) {
require __DIR__ . '/compat/wordpress-5.9/class-wp-theme-json-schema-gutenberg.php';
require __DIR__ . '/compat/wordpress-5.9/class-wp-theme-json-gutenberg.php';
require __DIR__ . '/compat/wordpress-5.9/class-wp-theme-json-resolver-gutenberg.php';
require __DIR__ . '/compat/wordpress-5.9/theme.php';
require __DIR__ . '/full-site-editing/full-site-editing.php';
require __DIR__ . '/full-site-editing/edit-site-page.php';
require __DIR__ . '/compat/wordpress-5.9/block-template.php';
Expand Down
4 changes: 2 additions & 2 deletions lib/navigation.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ function gutenberg_register_navigation_post_type() {
'description' => __( 'Navigation menus.', 'gutenberg' ),
'public' => false,
'has_archive' => false,
// We should disable UI for non-FSE themes.
'show_ui' => gutenberg_is_fse_theme(),
// We should disable UI for non-block themes.
'show_ui' => wp_is_block_theme(),
'show_in_menu' => false,
'show_in_admin_bar' => false,
'show_in_rest' => true,
Expand Down