Automattic\WooCommerce\Blocks\Utils

BlockTemplateUtils::build_template_result_from_file()public staticWC 1.0

Build a unified template object based on a theme file. Important: This method is an almost identical duplicate from wp-includes/block-template-utils.php as it was not intended for public use. It has been modified to build templates from plugins rather than themes.

{} Это метод класса: 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 6.8.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 );
	// 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';
	return $template;
}