Skip to content

Commit

Permalink
Synchronize wp_is_block_theme and block-templates block support with …
Browse files Browse the repository at this point in the history
…Core (#37218)

Co-authored-by: Ari Stathopoulos <[email protected]>
  • Loading branch information
2 people authored and noisysocks committed Dec 13, 2021
1 parent 12b5bea commit 0b79caf
Show file tree
Hide file tree
Showing 17 changed files with 49 additions and 71 deletions.
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' ),
'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() ) {
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 @@ -68,8 +41,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 @@ -104,6 +104,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

0 comments on commit 0b79caf

Please sign in to comment.