Automattic\WooCommerce\Blocks\Utils

BlockTemplateUtils::get_theme_template_path()public staticWC 1.0

Gets the first matching template part within themes directories

Since Gutenberg 12.1.0, the conventions for block templates and parts directory has changed from block-templates and block-templates-parts to templates and parts respectively.

This function traverses all possible combinations of directory paths where a template or part could be located and returns the first one which is readable, prioritizing the new convention over the deprecated one, but maintaining that one for backwards compatibility.

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

Хуков нет.

Возвращает

Строку|null. The matched path or null if no match was found.

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

$result = BlockTemplateUtils::get_theme_template_path( $template_slug, $template_type );
$template_slug(строка) (обязательный)
The slug of the template (i.e. without the file extension).
$template_type(строка)
Either wp_template or wp_template_part.
По умолчанию: 'wp_template'

Код BlockTemplateUtils::get_theme_template_path() WC 8.7.0

public static function get_theme_template_path( $template_slug, $template_type = 'wp_template' ) {
	$template_filename      = $template_slug . '.html';
	$possible_templates_dir = 'wp_template' === $template_type ? array(
		self::DIRECTORY_NAMES['TEMPLATES'],
		self::DIRECTORY_NAMES['DEPRECATED_TEMPLATES'],
	) : array(
		self::DIRECTORY_NAMES['TEMPLATE_PARTS'],
		self::DIRECTORY_NAMES['DEPRECATED_TEMPLATE_PARTS'],
	);

	// Combine the possible root directory names with either the template directory
	// or the stylesheet directory for child themes.
	$possible_paths = array_reduce(
		$possible_templates_dir,
		function( $carry, $item ) use ( $template_filename ) {
			$filepath = DIRECTORY_SEPARATOR . $item . DIRECTORY_SEPARATOR . $template_filename;

			$carry[] = get_stylesheet_directory() . $filepath;
			$carry[] = get_template_directory() . $filepath;

			return $carry;
		},
		array()
	);

	// Return the first matching.
	foreach ( $possible_paths as $path ) {
		if ( is_readable( $path ) ) {
			return $path;
		}
	}

	return null;
}