Automattic\WooCommerce\Blocks\Utils

BlockTemplateUtils::build_template_result_from_file()public staticWC 1.0

Build a unified template object based on a theme file.

Метод класса: BlockTemplateUtils{}

Хуков нет.

Возвращает

\WP_Block_Template. Template.

Использование

$result = BlockTemplateUtils::build_template_result_from_file( $template_file, $template_type );
$template_file(массив|объект) (обязательный)
Theme file.
$template_type(строка) (обязательный)
wp_template or wp_template_part.

Код BlockTemplateUtils::build_template_result_from_file() WC 8.7.0

public static function build_template_result_from_file( $template_file, $template_type ) {
	$template_file = (object) $template_file;

	// If the theme has an archive-products.html template but does not have product taxonomy templates
	// then we will load in the archive-product.html template from the theme to use for product taxonomies on the frontend.
	$template_is_from_theme = 'theme' === $template_file->source;
	$theme_name             = wp_get_theme()->get( 'TextDomain' );

	// phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
	$template_content  = file_get_contents( $template_file->path );
	$template          = new \WP_Block_Template();
	$template->id      = $template_is_from_theme ? $theme_name . '//' . $template_file->slug : self::PLUGIN_SLUG . '//' . $template_file->slug;
	$template->theme   = $template_is_from_theme ? $theme_name : self::PLUGIN_SLUG;
	$template->content = self::inject_theme_attribute_in_content( $template_content );
	// Remove the term description block from the archive-product template
	// as the Product Catalog/Shop page doesn't have a description.
	if ( 'archive-product' === $template_file->slug ) {
		$template->content = str_replace( '<!-- wp:term-description {"align":"wide"} /-->', '', $template->content );
	}
	// Plugin was agreed as a valid source value despite existing inline docs at the time of creating: https://github.com/WordPress/gutenberg/issues/36597#issuecomment-976232909.
	$template->source         = $template_file->source ? $template_file->source : 'plugin';
	$template->slug           = $template_file->slug;
	$template->type           = $template_type;
	$template->title          = ! empty( $template_file->title ) ? $template_file->title : self::get_block_template_title( $template_file->slug );
	$template->description    = ! empty( $template_file->description ) ? $template_file->description : self::get_block_template_description( $template_file->slug );
	$template->status         = 'publish';
	$template->has_theme_file = true;
	$template->origin         = $template_file->source;
	$template->is_custom      = false; // Templates loaded from the filesystem aren't custom, ones that have been edited and loaded from the DB are.
	$template->post_types     = array(); // Don't appear in any Edit Post template selector dropdown.
	$template->area           = 'uncategorized';

	// Force the Mini-Cart template part to be in the Mini-Cart template part area.
	// @todo When this class is refactored, move title, description, and area definition to the template classes (CheckoutHeaderTemplate, MiniCartTemplate, etc).
	if ( 'wp_template_part' === $template_type ) {
		switch ( $template_file->slug ) {
			case 'mini-cart':
				$template->area = 'mini-cart';
				break;
			case 'checkout-header':
				$template->area = 'header';
				break;
		}
	}
	return $template;
}