Automattic\WooCommerce\Blocks\Utils

BlockTemplateUtils::remove_duplicate_customized_templatespublic staticWC 1.0

Removes customized templates that shouldn't be available. That means customized templates based on the WooCommerce default template when there is a customized template based on the theme template.

Method of the class: BlockTemplateUtils{}

No Hooks.

Returns

Array. Filtered list of templates with only relevant templates available.

Usage

$result = BlockTemplateUtils::remove_duplicate_customized_templates( $templates );
$templates(WP_Block_Template[]|\stdClass[]) (required)
List of templates to run the filter on.

BlockTemplateUtils::remove_duplicate_customized_templates() code WC 11.0.1

public static function remove_duplicate_customized_templates( $templates ) {
	$theme_slug = get_stylesheet();

	$customized_theme_template_slugs = array_column(
		array_filter(
			$templates,
			function ( $template ) use ( $theme_slug ) {
				// This template has been customised and saved as a post.
				return 'custom' === $template->source && $theme_slug === $template->theme;
			}
		),
		'slug'
	);

	return array_filter(
		$templates,
		function ( $template ) use ( $theme_slug, $customized_theme_template_slugs ) {
			if ( $template->theme === $theme_slug ) {
				// This is a customized template based on the theme template, so it should be returned.
				return true;
			}
			// Customized from the WooCommerce default template: keep only if there is no customized theme template with same slug.
			if ( 'custom' === $template->source ) {
				return ! in_array( $template->slug, $customized_theme_template_slugs, true );
			}
			return true;
		}
	);
}