BlockTemplateUtils cleanup (#44256)

* Remove 'get_block_template' backwards compatibility check

* Add back get_block_template as a deprecated function

* Deprecate BlockTemplateUtils::filter_block_templates_by_feature_flag()

* Replace 'default template' with 'fallback template'

* Only load BlockTemplatesController in block themes

* Add changefile(s) from automation for the following project(s): woocommerce-blocks, woocommerce

* Remove unnecessary WP version comparison

* Revert "Only load BlockTemplatesController in block themes"

This reverts commit 8e1ed347d61b7ae5e8b8b96fa73b410565570f01.

* Remove deprecated functions, as they are marked as internal

---------

Co-authored-by: github-actions <github-actions@github.com>
This commit is contained in:
Albert Juhé Lluveras 2024-02-09 18:50:08 +01:00 committed by GitHub
parent c8f8299f02
commit 2f5eaa1f4c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 15 additions and 72 deletions

View File

@ -63,7 +63,7 @@ CUSTOMIZABLE_WC_TEMPLATES.forEach( ( testData ) => {
editorUtils,
page,
} ) => {
// Edit default template and verify changes are visible.
// Edit fallback template and verify changes are visible.
await admin.visitSiteEditor( {
postId: `${ WC_TEMPLATES_SLUG }//${
testData.fallbackTemplate?.templatePath || ''

View File

@ -0,0 +1,4 @@
Significance: patch
Type: update
Comment: Clean up some methods of `BlockTemplateUtils` and make `BlockTemplatesController` only load in themes that support templates/template parts.

View File

@ -283,7 +283,7 @@ class WC_Admin_Meta_Boxes {
* @return string[] Templates array excluding block-based templates.
*/
public function remove_block_templates( $templates ) {
if ( count( $templates ) === 0 || ! wc_current_theme_is_fse_theme() || ( ! function_exists( 'gutenberg_get_block_template' ) && ! function_exists( 'get_block_template' ) ) ) {
if ( count( $templates ) === 0 || ! wc_current_theme_is_fse_theme() ) {
return $templates;
}
@ -296,9 +296,7 @@ class WC_Admin_Meta_Boxes {
continue;
}
$block_template = function_exists( 'gutenberg_get_block_template' ) ?
gutenberg_get_block_template( $theme . '//' . $template_key ) :
get_block_template( $theme . '//' . $template_key );
$block_template = get_block_template( $theme . '//' . $template_key );
// If the block template has the product post type specified, include it.
if ( $block_template && is_array( $block_template->post_types ) && in_array( 'product', $block_template->post_types ) ) {

View File

@ -160,7 +160,7 @@ class BlockTemplatesController {
*/
public function render_woocommerce_template_part( $attributes ) {
if ( isset( $attributes['theme'] ) && 'woocommerce/woocommerce' === $attributes['theme'] ) {
$template_part = BlockTemplateUtils::get_block_template( $attributes['theme'] . '//' . $attributes['slug'], 'wp_template_part' );
$template_part = get_block_template( $attributes['theme'] . '//' . $attributes['slug'], 'wp_template_part' );
if ( $template_part && ! empty( $template_part->content ) ) {
return do_blocks( $template_part->content );
@ -315,7 +315,7 @@ class BlockTemplatesController {
if ( BlockTemplateUtils::DEPRECATED_PLUGIN_SLUG === strtolower( $template_id ) ) {
// Because we are using get_block_templates we have to unhook this method to prevent a recursive loop where this filter is applied.
remove_filter( 'pre_get_block_file_template', array( $this, 'get_block_file_template' ), 10, 3 );
$template_with_deprecated_id = BlockTemplateUtils::get_block_template( $id, $template_type );
$template_with_deprecated_id = get_block_template( $id, $template_type );
// Let's hook this method back now that we have used the function.
add_filter( 'pre_get_block_file_template', array( $this, 'get_block_file_template' ), 10, 3 );
@ -596,9 +596,8 @@ class BlockTemplatesController {
public function get_block_templates( $slugs = array(), $template_type = 'wp_template' ) {
$templates_from_db = BlockTemplateUtils::get_block_templates_from_db( $slugs, $template_type );
$templates_from_woo = $this->get_block_templates_from_woocommerce( $slugs, $templates_from_db, $template_type );
$templates = array_merge( $templates_from_db, $templates_from_woo );
return BlockTemplateUtils::filter_block_templates_by_feature_flag( $templates );
return array_merge( $templates_from_db, $templates_from_woo );
}
/**

View File

@ -476,7 +476,7 @@ class MiniCart extends AbstractBlock {
$theme_has_mini_cart = BlockTemplateUtils::theme_has_template_part( 'mini-cart' );
$template_slug_to_load = $theme_has_mini_cart ? get_stylesheet() : BlockTemplateUtils::PLUGIN_SLUG;
}
$template_part = BlockTemplateUtils::get_block_template( $template_slug_to_load . '//mini-cart', 'wp_template_part' );
$template_part = get_block_template( $template_slug_to_load . '//mini-cart', 'wp_template_part' );
if ( $template_part && ! empty( $template_part->content ) ) {
$template_part_contents = do_blocks( $template_part->content );

View File

@ -70,7 +70,7 @@ class Migration {
* Rename `checkout` template to `page-checkout`.
*/
public static function wc_blocks_update_1120_rename_checkout_template() {
$template = BlockTemplateUtils::get_block_template( BlockTemplateUtils::PLUGIN_SLUG . '//checkout', 'wp_template' );
$template = get_block_template( BlockTemplateUtils::PLUGIN_SLUG . '//checkout', 'wp_template' );
if ( $template && ! empty( $template->wp_id ) ) {
if ( ! defined( 'WP_POST_REVISIONS' ) ) {
@ -90,7 +90,7 @@ class Migration {
* Rename `cart` template to `page-cart`.
*/
public static function wc_blocks_update_1120_rename_cart_template() {
$template = BlockTemplateUtils::get_block_template( BlockTemplateUtils::PLUGIN_SLUG . '//cart', 'wp_template' );
$template = get_block_template( BlockTemplateUtils::PLUGIN_SLUG . '//cart', 'wp_template' );
if ( $template && ! empty( $template->wp_id ) ) {
if ( ! defined( 'WP_POST_REVISIONS' ) ) {

View File

@ -492,28 +492,6 @@ class BlockTemplateUtils {
return false;
}
/**
* Retrieves a single unified template object using its id.
*
* @param string $id Template unique identifier (example: theme_slug//template_slug).
* @param string $template_type Optional. Template type: `wp_template` or 'wp_template_part`.
* Default `wp_template`.
*
* @return WP_Block_Template|null Template.
*/
public static function get_block_template( $id, $template_type ) {
if ( function_exists( 'get_block_template' ) ) {
return get_block_template( $id, $template_type );
}
if ( function_exists( 'gutenberg_get_block_template' ) ) {
return gutenberg_get_block_template( $id, $template_type );
}
return null;
}
/**
* Checks if we can fall back to the `archive-product` template for a given slug.
*
@ -621,35 +599,6 @@ class BlockTemplateUtils {
return false;
}
/**
* Filter block templates by feature flag.
*
* @param WP_Block_Template[] $block_templates An array of block template objects.
*
* @return WP_Block_Template[] An array of block template objects.
*/
public static function filter_block_templates_by_feature_flag( $block_templates ) {
$feature_gating = new FeatureGating();
$flag = $feature_gating->get_flag();
/**
* An array of block templates with slug as key and flag as value.
*
* @var array
*/
$block_templates_with_feature_gate = array();
return array_filter(
$block_templates,
function( $block_template ) use ( $flag, $block_templates_with_feature_gate ) {
if ( isset( $block_templates_with_feature_gate[ $block_template->slug ] ) ) {
return $block_templates_with_feature_gate[ $block_template->slug ] <= $flag;
}
return true;
}
);
}
/**
* Removes templates that were added to a theme's block-templates directory, but already had a customised version saved in the database.
*
@ -727,18 +676,11 @@ class BlockTemplateUtils {
/**
* Returns whether the blockified templates should be used or not.
* First, we need to make sure WordPress version is higher than 6.1 (lowest that supports Products block).
* Then, if the option is not stored on the db, we need to check if the current theme is a block one or not.
* If the option is not stored on the db, we need to check if the current theme is a block one or not.
*
* @return boolean
*/
public static function should_use_blockified_product_grid_templates() {
$minimum_wp_version = '6.1';
if ( version_compare( $GLOBALS['wp_version'], $minimum_wp_version, '<' ) ) {
return false;
}
$use_blockified_templates = get_option( Options::WC_BLOCK_USE_BLOCKIFIED_PRODUCT_GRID_BLOCK_AS_TEMPLATE );
if ( false === $use_blockified_templates ) {
@ -820,7 +762,7 @@ class BlockTemplateUtils {
$theme_has_template = self::theme_has_template_part( $slug );
$template_slug_to_load = $theme_has_template ? get_stylesheet() : self::PLUGIN_SLUG;
}
$template_part = self::get_block_template( $template_slug_to_load . '//' . $slug, 'wp_template_part' );
$template_part = get_block_template( $template_slug_to_load . '//' . $slug, 'wp_template_part' );
if ( $template_part && ! empty( $template_part->content ) ) {
return $template_part->content;